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 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 "CSSPropertyNames.h"
27 #include "CSSValueKeywords.h"
28 #include "HTMLNames.h"
29 #include "MappedAttribute.h"
30
31 using namespace WTF;
32
33 namespace WebCore {
34
35 using namespace HTMLNames;
36
HTMLFontElement(const QualifiedName & tagName,Document * document)37 HTMLFontElement::HTMLFontElement(const QualifiedName& tagName, Document* document)
38 : HTMLElement(tagName, document)
39 {
40 ASSERT(hasTagName(fontTag));
41 }
42
43 // Allows leading spaces.
44 // Allows trailing nonnumeric characters.
45 // Returns 10 for any size greater than 9.
parseFontSizeNumber(const String & s,int & size)46 static bool parseFontSizeNumber(const String& s, int& size)
47 {
48 unsigned pos = 0;
49
50 // Skip leading spaces.
51 while (isSpaceOrNewline(s[pos]))
52 ++pos;
53
54 // Skip a plus or minus.
55 bool sawPlus = false;
56 bool sawMinus = false;
57 if (s[pos] == '+') {
58 ++pos;
59 sawPlus = true;
60 } else if (s[pos] == '-') {
61 ++pos;
62 sawMinus = true;
63 }
64
65 // Parse a single digit.
66 if (!isASCIIDigit(s[pos]))
67 return false;
68 int num = s[pos++] - '0';
69
70 // Check for an additional digit.
71 if (isASCIIDigit(s[pos]))
72 num = 10;
73
74 if (sawPlus) {
75 size = num + 3;
76 return true;
77 }
78
79 // Don't return 0 (which means 3) or a negative number (which means the same as 1).
80 if (sawMinus) {
81 size = num == 1 ? 2 : 1;
82 return true;
83 }
84
85 size = num;
86 return true;
87 }
88
mapToEntry(const QualifiedName & attrName,MappedAttributeEntry & result) const89 bool HTMLFontElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
90 {
91 if (attrName == sizeAttr ||
92 attrName == colorAttr ||
93 attrName == faceAttr) {
94 result = eUniversal;
95 return false;
96 }
97
98 return HTMLElement::mapToEntry(attrName, result);
99 }
100
cssValueFromFontSizeNumber(const String & s,int & size)101 bool HTMLFontElement::cssValueFromFontSizeNumber(const String& s, int& size)
102 {
103 int num;
104 if (!parseFontSizeNumber(s, num))
105 return false;
106
107 switch (num) {
108 case 2:
109 size = CSSValueSmall;
110 break;
111 case 0: // treat 0 the same as 3, because people expect it to be between -1 and +1
112 case 3:
113 size = CSSValueMedium;
114 break;
115 case 4:
116 size = CSSValueLarge;
117 break;
118 case 5:
119 size = CSSValueXLarge;
120 break;
121 case 6:
122 size = CSSValueXxLarge;
123 break;
124 default:
125 if (num > 6)
126 size = CSSValueWebkitXxxLarge;
127 else
128 size = CSSValueXSmall;
129 }
130 return true;
131 }
132
parseMappedAttribute(MappedAttribute * attr)133 void HTMLFontElement::parseMappedAttribute(MappedAttribute *attr)
134 {
135 if (attr->name() == sizeAttr) {
136 int size;
137 if (cssValueFromFontSizeNumber(attr->value(), size))
138 addCSSProperty(attr, CSSPropertyFontSize, size);
139 } else if (attr->name() == colorAttr) {
140 addCSSColor(attr, CSSPropertyColor, attr->value());
141 } else if (attr->name() == faceAttr) {
142 addCSSProperty(attr, CSSPropertyFontFamily, attr->value());
143 } else
144 HTMLElement::parseMappedAttribute(attr);
145 }
146
color() const147 String HTMLFontElement::color() const
148 {
149 return getAttribute(colorAttr);
150 }
151
setColor(const String & value)152 void HTMLFontElement::setColor(const String& value)
153 {
154 setAttribute(colorAttr, value);
155 }
156
face() const157 String HTMLFontElement::face() const
158 {
159 return getAttribute(faceAttr);
160 }
161
setFace(const String & value)162 void HTMLFontElement::setFace(const String& value)
163 {
164 setAttribute(faceAttr, value);
165 }
166
size() const167 String HTMLFontElement::size() const
168 {
169 return getAttribute(sizeAttr);
170 }
171
setSize(const String & value)172 void HTMLFontElement::setSize(const String& value)
173 {
174 setAttribute(sizeAttr, value);
175 }
176
177 }
178