initial import
[vuplus_webkit] / Source / WebCore / html / HTMLFontElement.cpp
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 #include "HTMLParserIdioms.h"
31 #include <wtf/text/StringBuilder.h>
32
33 using namespace WTF;
34
35 namespace WebCore {
36
37 using namespace HTMLNames;
38
39 HTMLFontElement::HTMLFontElement(const QualifiedName& tagName, Document* document)
40     : HTMLElement(tagName, document)
41 {
42     ASSERT(hasTagName(fontTag));
43 }
44
45 PassRefPtr<HTMLFontElement> HTMLFontElement::create(const QualifiedName& tagName, Document* document)
46 {
47     return adoptRef(new HTMLFontElement(tagName, document));
48 }
49
50 // http://www.whatwg.org/specs/web-apps/current-work/multipage/rendering.html#fonts-and-colors
51 static bool parseFontSize(const String& input, int& size)
52 {
53
54     // Step 1
55     // Step 2
56     const UChar* position = input.characters();
57     const UChar* end = position + input.length();
58
59     // Step 3
60     while (position < end) {
61         if (!isHTMLSpace(*position))
62             break;
63         ++position;
64     }
65
66     // Step 4
67     if (position == end)
68         return false;
69     ASSERT(position < end);
70
71     // Step 5
72     enum {
73         RelativePlus,
74         RelativeMinus,
75         Absolute
76     } mode;
77
78     switch (*position) {
79     case '+':
80         mode = RelativePlus;
81         ++position;
82         break;
83     case '-':
84         mode = RelativeMinus;
85         ++position;
86         break;
87     default:
88         mode = Absolute;
89         break;
90     }
91
92     // Step 6
93     StringBuilder digits;
94     digits.reserveCapacity(16);
95     while (position < end) {
96         if (!isASCIIDigit(*position))
97             break;
98         digits.append(*position++);
99     }
100
101     // Step 7
102     if (digits.isEmpty())
103         return false;
104
105     // Step 8
106     int value = charactersToIntStrict(digits.characters(), digits.length());
107
108     // Step 9
109     if (mode == RelativePlus)
110         value += 3;
111     else if (mode == RelativeMinus)
112         value = 3 - value;
113
114     // Step 10
115     if (value > 7)
116         value = 7;
117
118     // Step 11
119     if (value < 1)
120         value = 1;
121
122     size = value;
123     return true;
124 }
125
126 bool HTMLFontElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
127 {
128     if (attrName == sizeAttr ||
129         attrName == colorAttr ||
130         attrName == faceAttr) {
131         result = eUniversal;
132         return false;
133     }
134     
135     return HTMLElement::mapToEntry(attrName, result);
136 }
137
138 bool HTMLFontElement::cssValueFromFontSizeNumber(const String& s, int& size)
139 {
140     int num = 0;
141     if (!parseFontSize(s, num))
142         return false;
143
144     switch (num) {
145     case 1:
146         // FIXME: The spec says that we're supposed to use CSSValueXxSmall here.
147         size = CSSValueXSmall;
148         break;
149     case 2: 
150         size = CSSValueSmall;
151         break;
152     case 3: 
153         size = CSSValueMedium;
154         break;
155     case 4: 
156         size = CSSValueLarge;
157         break;
158     case 5: 
159         size = CSSValueXLarge;
160         break;
161     case 6: 
162         size = CSSValueXxLarge;
163         break;
164     case 7:
165         size = CSSValueWebkitXxxLarge;
166         break;
167     default:
168         ASSERT_NOT_REACHED();
169     }
170     return true;
171 }
172
173 void HTMLFontElement::parseMappedAttribute(Attribute* attr)
174 {
175     if (attr->name() == sizeAttr) {
176         int size = 0;
177         if (cssValueFromFontSizeNumber(attr->value(), size))
178             addCSSProperty(attr, CSSPropertyFontSize, size);
179     } else if (attr->name() == colorAttr) {
180         addCSSColor(attr, CSSPropertyColor, attr->value());
181     } else if (attr->name() == faceAttr) {
182         addCSSProperty(attr, CSSPropertyFontFamily, attr->value());
183     } else
184         HTMLElement::parseMappedAttribute(attr);
185 }
186
187 }