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.
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.
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.
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.
24 #include "core/html/HTMLFontElement.h"
26 #include "CSSPropertyNames.h"
27 #include "CSSValueKeywords.h"
28 #include "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"
39 using namespace HTMLNames;
41 HTMLFontElement::HTMLFontElement(Document& document)
42 : HTMLElement(fontTag, document)
44 ScriptWrappable::init(this);
47 PassRefPtr<HTMLFontElement> HTMLFontElement::create(Document& document)
49 return adoptRef(new HTMLFontElement(document));
52 // http://www.whatwg.org/specs/web-apps/current-work/multipage/rendering.html#fonts-and-colors
53 template <typename CharacterType>
54 static bool parseFontSize(const CharacterType* characters, unsigned length, int& size)
59 const CharacterType* position = characters;
60 const CharacterType* end = characters + length;
63 while (position < end) {
64 if (!isHTMLSpace<CharacterType>(*position))
72 ASSERT(position < end);
97 digits.reserveCapacity(16);
98 while (position < end) {
99 if (!isASCIIDigit(*position))
101 digits.append(*position++);
105 if (digits.isEmpty())
112 value = charactersToIntStrict(digits.characters8(), digits.length());
114 value = charactersToIntStrict(digits.characters16(), digits.length());
117 if (mode == RelativePlus)
119 else if (mode == RelativeMinus)
134 static bool parseFontSize(const String& input, int& size)
140 return parseFontSize(input.characters8(), input.length(), size);
142 return parseFontSize(input.characters16(), input.length(), size);
145 bool HTMLFontElement::cssValueFromFontSizeNumber(const String& s, CSSValueID& size)
148 if (!parseFontSize(s, num))
153 // FIXME: The spec says that we're supposed to use CSSValueXxSmall here.
154 size = CSSValueXSmall;
157 size = CSSValueSmall;
160 size = CSSValueMedium;
163 size = CSSValueLarge;
166 size = CSSValueXLarge;
169 size = CSSValueXxLarge;
172 size = CSSValueWebkitXxxLarge;
175 ASSERT_NOT_REACHED();
180 bool HTMLFontElement::isPresentationAttribute(const QualifiedName& name) const
182 if (name == sizeAttr || name == colorAttr || name == faceAttr)
184 return HTMLElement::isPresentationAttribute(name);
187 void HTMLFontElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
189 if (name == sizeAttr) {
190 CSSValueID size = CSSValueInvalid;
191 if (cssValueFromFontSizeNumber(value, size))
192 addPropertyToPresentationAttributeStyle(style, CSSPropertyFontSize, size);
193 } else if (name == colorAttr)
194 addHTMLColorToStyle(style, CSSPropertyColor, value);
195 else if (name == faceAttr) {
196 if (RefPtrWillBeRawPtr<CSSValueList> fontFaceValue = cssValuePool().createFontFaceValue(value))
197 style->setProperty(CSSProperty(CSSPropertyFontFamily, fontFaceValue.release()));
199 HTMLElement::collectStyleForPresentationAttribute(name, value, style);