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 "core/html/HTMLFontElement.h"
25
26 #include "core/CSSPropertyNames.h"
27 #include "core/CSSValueKeywords.h"
28 #include "core/HTMLNames.h"
29 #include "core/css/CSSValueList.h"
30 #include "core/css/CSSValuePool.h"
31 #include "core/css/StylePropertySet.h"
32 #include "core/html/parser/HTMLParserIdioms.h"
33 #include "wtf/text/StringBuilder.h"
34
35 using namespace WTF;
36
37 namespace WebCore {
38
39 using namespace HTMLNames;
40
HTMLFontElement(Document & document)41 inline HTMLFontElement::HTMLFontElement(Document& document)
42 : HTMLElement(fontTag, document)
43 {
44 ScriptWrappable::init(this);
45 }
46
DEFINE_NODE_FACTORY(HTMLFontElement)47 DEFINE_NODE_FACTORY(HTMLFontElement)
48
49 // http://www.whatwg.org/specs/web-apps/current-work/multipage/rendering.html#fonts-and-colors
50 template <typename CharacterType>
51 static bool parseFontSize(const CharacterType* characters, unsigned length, int& size)
52 {
53
54 // Step 1
55 // Step 2
56 const CharacterType* position = characters;
57 const CharacterType* end = characters + length;
58
59 // Step 3
60 while (position < end) {
61 if (!isHTMLSpace<CharacterType>(*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;
107
108 if (digits.is8Bit())
109 value = charactersToIntStrict(digits.characters8(), digits.length());
110 else
111 value = charactersToIntStrict(digits.characters16(), digits.length());
112
113 // Step 9
114 if (mode == RelativePlus)
115 value += 3;
116 else if (mode == RelativeMinus)
117 value = 3 - value;
118
119 // Step 10
120 if (value > 7)
121 value = 7;
122
123 // Step 11
124 if (value < 1)
125 value = 1;
126
127 size = value;
128 return true;
129 }
130
parseFontSize(const String & input,int & size)131 static bool parseFontSize(const String& input, int& size)
132 {
133 if (input.isEmpty())
134 return false;
135
136 if (input.is8Bit())
137 return parseFontSize(input.characters8(), input.length(), size);
138
139 return parseFontSize(input.characters16(), input.length(), size);
140 }
141
cssValueFromFontSizeNumber(const String & s,CSSValueID & size)142 bool HTMLFontElement::cssValueFromFontSizeNumber(const String& s, CSSValueID& size)
143 {
144 int num = 0;
145 if (!parseFontSize(s, num))
146 return false;
147
148 switch (num) {
149 case 1:
150 // FIXME: The spec says that we're supposed to use CSSValueXxSmall here.
151 size = CSSValueXSmall;
152 break;
153 case 2:
154 size = CSSValueSmall;
155 break;
156 case 3:
157 size = CSSValueMedium;
158 break;
159 case 4:
160 size = CSSValueLarge;
161 break;
162 case 5:
163 size = CSSValueXLarge;
164 break;
165 case 6:
166 size = CSSValueXxLarge;
167 break;
168 case 7:
169 size = CSSValueWebkitXxxLarge;
170 break;
171 default:
172 ASSERT_NOT_REACHED();
173 }
174 return true;
175 }
176
isPresentationAttribute(const QualifiedName & name) const177 bool HTMLFontElement::isPresentationAttribute(const QualifiedName& name) const
178 {
179 if (name == sizeAttr || name == colorAttr || name == faceAttr)
180 return true;
181 return HTMLElement::isPresentationAttribute(name);
182 }
183
collectStyleForPresentationAttribute(const QualifiedName & name,const AtomicString & value,MutableStylePropertySet * style)184 void HTMLFontElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
185 {
186 if (name == sizeAttr) {
187 CSSValueID size = CSSValueInvalid;
188 if (cssValueFromFontSizeNumber(value, size))
189 addPropertyToPresentationAttributeStyle(style, CSSPropertyFontSize, size);
190 } else if (name == colorAttr)
191 addHTMLColorToStyle(style, CSSPropertyColor, value);
192 else if (name == faceAttr) {
193 if (RefPtrWillBeRawPtr<CSSValueList> fontFaceValue = cssValuePool().createFontFaceValue(value))
194 style->setProperty(CSSProperty(CSSPropertyFontFamily, fontFaceValue.release()));
195 } else
196 HTMLElement::collectStyleForPresentationAttribute(name, value, style);
197 }
198
199 }
200