This source file includes following definitions.
- create
- parseFontSize
- parseFontSize
- cssValueFromFontSizeNumber
- isPresentationAttribute
- collectStyleForPresentationAttribute
#include "config.h"
#include "core/html/HTMLFontElement.h"
#include "CSSPropertyNames.h"
#include "CSSValueKeywords.h"
#include "HTMLNames.h"
#include "core/css/CSSValueList.h"
#include "core/css/CSSValuePool.h"
#include "core/css/StylePropertySet.h"
#include "core/html/parser/HTMLParserIdioms.h"
#include "wtf/text/StringBuilder.h"
using namespace WTF;
namespace WebCore {
using namespace HTMLNames;
HTMLFontElement::HTMLFontElement(Document& document)
: HTMLElement(fontTag, document)
{
ScriptWrappable::init(this);
}
PassRefPtr<HTMLFontElement> HTMLFontElement::create(Document& document)
{
return adoptRef(new HTMLFontElement(document));
}
template <typename CharacterType>
static bool parseFontSize(const CharacterType* characters, unsigned length, int& size)
{
const CharacterType* position = characters;
const CharacterType* end = characters + length;
while (position < end) {
if (!isHTMLSpace<CharacterType>(*position))
break;
++position;
}
if (position == end)
return false;
ASSERT(position < end);
enum {
RelativePlus,
RelativeMinus,
Absolute
} mode;
switch (*position) {
case '+':
mode = RelativePlus;
++position;
break;
case '-':
mode = RelativeMinus;
++position;
break;
default:
mode = Absolute;
break;
}
StringBuilder digits;
digits.reserveCapacity(16);
while (position < end) {
if (!isASCIIDigit(*position))
break;
digits.append(*position++);
}
if (digits.isEmpty())
return false;
int value;
if (digits.is8Bit())
value = charactersToIntStrict(digits.characters8(), digits.length());
else
value = charactersToIntStrict(digits.characters16(), digits.length());
if (mode == RelativePlus)
value += 3;
else if (mode == RelativeMinus)
value = 3 - value;
if (value > 7)
value = 7;
if (value < 1)
value = 1;
size = value;
return true;
}
static bool parseFontSize(const String& input, int& size)
{
if (input.isEmpty())
return false;
if (input.is8Bit())
return parseFontSize(input.characters8(), input.length(), size);
return parseFontSize(input.characters16(), input.length(), size);
}
bool HTMLFontElement::cssValueFromFontSizeNumber(const String& s, CSSValueID& size)
{
int num = 0;
if (!parseFontSize(s, num))
return false;
switch (num) {
case 1:
size = CSSValueXSmall;
break;
case 2:
size = CSSValueSmall;
break;
case 3:
size = CSSValueMedium;
break;
case 4:
size = CSSValueLarge;
break;
case 5:
size = CSSValueXLarge;
break;
case 6:
size = CSSValueXxLarge;
break;
case 7:
size = CSSValueWebkitXxxLarge;
break;
default:
ASSERT_NOT_REACHED();
}
return true;
}
bool HTMLFontElement::isPresentationAttribute(const QualifiedName& name) const
{
if (name == sizeAttr || name == colorAttr || name == faceAttr)
return true;
return HTMLElement::isPresentationAttribute(name);
}
void HTMLFontElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
{
if (name == sizeAttr) {
CSSValueID size = CSSValueInvalid;
if (cssValueFromFontSizeNumber(value, size))
addPropertyToPresentationAttributeStyle(style, CSSPropertyFontSize, size);
} else if (name == colorAttr)
addHTMLColorToStyle(style, CSSPropertyColor, value);
else if (name == faceAttr) {
if (RefPtrWillBeRawPtr<CSSValueList> fontFaceValue = cssValuePool().createFontFaceValue(value))
style->setProperty(CSSProperty(CSSPropertyFontFamily, fontFaceValue.release()));
} else
HTMLElement::collectStyleForPresentationAttribute(name, value, style);
}
}