Home | History | Annotate | Download | only in css
      1 /*
      2  * (C) 1999-2003 Lars Knoll (knoll (at) kde.org)
      3  * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
      4  *
      5  * This library is free software; you can redistribute it and/or
      6  * modify it under the terms of the GNU Library General Public
      7  * License as published by the Free Software Foundation; either
      8  * version 2 of the License, or (at your option) any later version.
      9  *
     10  * This library is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13  * Library General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU Library General Public License
     16  * along with this library; see the file COPYING.LIB.  If not, write to
     17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18  * Boston, MA 02110-1301, USA.
     19  */
     20 
     21 #include "config.h"
     22 #include "FontFamilyValue.h"
     23 
     24 namespace WebCore {
     25 
     26 // FIXME: This appears identical to isCSSTokenizerIdentifier from CSSPrimitiveValue.cpp, we should use a single function.
     27 static bool isValidCSSIdentifier(const String& string)
     28 {
     29     unsigned length = string.length();
     30     if (!length)
     31         return false;
     32 
     33     const UChar* characters = string.characters();
     34     UChar c = characters[0];
     35     if (!(c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '-' || c >= 0x80))
     36         return false;
     37 
     38     for (unsigned i = 1; i < length; ++i) {
     39         c = characters[i];
     40         if (!(c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c >= 0x80))
     41             return false;
     42     }
     43 
     44     return true;
     45 }
     46 
     47 // Quotes the string if it needs quoting.
     48 // We use single quotes because serialization code uses double quotes, and it's nice to
     49 // avoid having to turn all the quote marks into &quot; as we would have to.
     50 static String quoteStringIfNeeded(const String& string)
     51 {
     52     if (isValidCSSIdentifier(string))
     53         return string;
     54 
     55     // FIXME: Also need to transform control characters (00-1F) into \ sequences.
     56     // FIXME: This is inefficient -- should use a Vector<UChar> instead.
     57     String quotedString = string;
     58     quotedString.replace('\\', "\\\\");
     59     quotedString.replace('\'', "\\'");
     60     return "'" + quotedString + "'";
     61 }
     62 
     63 FontFamilyValue::FontFamilyValue(const String& familyName)
     64     : CSSPrimitiveValue(String(), CSS_STRING)
     65     , m_familyName(familyName)
     66 {
     67     // If there is anything in parentheses or square brackets at the end, delete it.
     68     // FIXME: Do we really need this? The original code mentioned "a language tag in
     69     // braces at the end" and "[Xft] qualifiers", but it's not clear either of those
     70     // is in active use on the web.
     71     unsigned length = m_familyName.length();
     72     while (length >= 3) {
     73         UChar startCharacter = 0;
     74         switch (m_familyName[length - 1]) {
     75             case ']':
     76                 startCharacter = '[';
     77                 break;
     78             case ')':
     79                 startCharacter = '(';
     80                 break;
     81         }
     82         if (!startCharacter)
     83             break;
     84         unsigned first = 0;
     85         for (unsigned i = length - 2; i > 0; --i) {
     86             if (m_familyName[i - 1] == ' ' && m_familyName[i] == startCharacter)
     87                 first = i - 1;
     88         }
     89         if (!first)
     90             break;
     91         length = first;
     92     }
     93     m_familyName.truncate(length);
     94 }
     95 
     96 void FontFamilyValue::appendSpaceSeparated(const UChar* characters, unsigned length)
     97 {
     98     m_familyName.append(' ');
     99     m_familyName.append(characters, length);
    100 }
    101 
    102 String FontFamilyValue::cssText() const
    103 {
    104     return quoteStringIfNeeded(m_familyName);
    105 }
    106 
    107 }
    108