WebCore/html/HTMLFontElement.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
       
     3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
       
     4  *           (C) 2000 Simon Hausmann <hausmann@kde.org>
       
     5  * Copyright (C) 2003, 2006, 2008, 2010 Apple Inc. All rights reserved.
       
     6  *
       
     7  * This library is free software; you can redistribute it and/or
       
     8  * modify it under the terms of the GNU Library General Public
       
     9  * License as published by the Free Software Foundation; either
       
    10  * version 2 of the License, or (at your option) any later version.
       
    11  *
       
    12  * This library is distributed in the hope that it will be useful,
       
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    15  * Library General Public License for more details.
       
    16  *
       
    17  * You should have received a copy of the GNU Library General Public License
       
    18  * along with this library; see the file COPYING.LIB.  If not, write to
       
    19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    20  * Boston, MA 02110-1301, USA.
       
    21  */
       
    22 
       
    23 #include "config.h"
       
    24 #include "HTMLFontElement.h"
       
    25 
       
    26 #include "Attribute.h"
       
    27 #include "CSSPropertyNames.h"
       
    28 #include "CSSValueKeywords.h"
       
    29 #include "HTMLNames.h"
       
    30 
       
    31 using namespace WTF;
       
    32 
       
    33 namespace WebCore {
       
    34 
       
    35 using namespace HTMLNames;
       
    36 
       
    37 HTMLFontElement::HTMLFontElement(const QualifiedName& tagName, Document* document)
       
    38     : HTMLElement(tagName, document)
       
    39 {
       
    40     ASSERT(hasTagName(fontTag));
       
    41 }
       
    42 
       
    43 PassRefPtr<HTMLFontElement> HTMLFontElement::create(const QualifiedName& tagName, Document* document)
       
    44 {
       
    45     return adoptRef(new HTMLFontElement(tagName, document));
       
    46 }
       
    47 
       
    48 // Allows leading spaces.
       
    49 // Allows trailing nonnumeric characters.
       
    50 // Returns 10 for any size greater than 9.
       
    51 static bool parseFontSizeNumber(const String& s, int& size)
       
    52 {
       
    53     unsigned pos = 0;
       
    54     
       
    55     // Skip leading spaces.
       
    56     while (isSpaceOrNewline(s[pos]))
       
    57         ++pos;
       
    58     
       
    59     // Skip a plus or minus.
       
    60     bool sawPlus = false;
       
    61     bool sawMinus = false;
       
    62     if (s[pos] == '+') {
       
    63         ++pos;
       
    64         sawPlus = true;
       
    65     } else if (s[pos] == '-') {
       
    66         ++pos;
       
    67         sawMinus = true;
       
    68     }
       
    69     
       
    70     // Parse a single digit.
       
    71     if (!isASCIIDigit(s[pos]))
       
    72         return false;
       
    73     int num = s[pos++] - '0';
       
    74     
       
    75     // Check for an additional digit.
       
    76     if (isASCIIDigit(s[pos]))
       
    77         num = 10;
       
    78     
       
    79     if (sawPlus) {
       
    80         size = num + 3;
       
    81         return true;
       
    82     }
       
    83     
       
    84     // Don't return 0 (which means 3) or a negative number (which means the same as 1).
       
    85     if (sawMinus) {
       
    86         size = num == 1 ? 2 : 1;
       
    87         return true;
       
    88     }
       
    89     
       
    90     size = num;
       
    91     return true;
       
    92 }
       
    93 
       
    94 bool HTMLFontElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
       
    95 {
       
    96     if (attrName == sizeAttr ||
       
    97         attrName == colorAttr ||
       
    98         attrName == faceAttr) {
       
    99         result = eUniversal;
       
   100         return false;
       
   101     }
       
   102     
       
   103     return HTMLElement::mapToEntry(attrName, result);
       
   104 }
       
   105 
       
   106 bool HTMLFontElement::cssValueFromFontSizeNumber(const String& s, int& size)
       
   107 {
       
   108     int num;
       
   109     if (!parseFontSizeNumber(s, num))
       
   110         return false;
       
   111         
       
   112     switch (num) {
       
   113         case 2: 
       
   114             size = CSSValueSmall; 
       
   115             break;
       
   116         case 0: // treat 0 the same as 3, because people expect it to be between -1 and +1
       
   117         case 3: 
       
   118             size = CSSValueMedium; 
       
   119             break;
       
   120         case 4: 
       
   121             size = CSSValueLarge; 
       
   122             break;
       
   123         case 5: 
       
   124             size = CSSValueXLarge; 
       
   125             break;
       
   126         case 6: 
       
   127             size = CSSValueXxLarge; 
       
   128             break;
       
   129         default:
       
   130             if (num > 6)
       
   131                 size = CSSValueWebkitXxxLarge;
       
   132             else
       
   133                 size = CSSValueXSmall;
       
   134     }
       
   135     return true;
       
   136 }
       
   137 
       
   138 void HTMLFontElement::parseMappedAttribute(Attribute* attr)
       
   139 {
       
   140     if (attr->name() == sizeAttr) {
       
   141         int size;
       
   142         if (cssValueFromFontSizeNumber(attr->value(), size))
       
   143             addCSSProperty(attr, CSSPropertyFontSize, size);
       
   144     } else if (attr->name() == colorAttr) {
       
   145         addCSSColor(attr, CSSPropertyColor, attr->value());
       
   146     } else if (attr->name() == faceAttr) {
       
   147         addCSSProperty(attr, CSSPropertyFontFamily, attr->value());
       
   148     } else
       
   149         HTMLElement::parseMappedAttribute(attr);
       
   150 }
       
   151 
       
   152 }