HTMLFontElement.cpp [plain text]
#include "config.h"
#include "HTMLFontElement.h"
#include "Attribute.h"
#include "CSSPropertyNames.h"
#include "CSSValueKeywords.h"
#include "HTMLNames.h"
#include "HTMLParserIdioms.h"
using namespace WTF;
namespace WebCore {
using namespace HTMLNames;
HTMLFontElement::HTMLFontElement(const QualifiedName& tagName, Document* document)
: HTMLElement(tagName, document)
{
ASSERT(hasTagName(fontTag));
}
PassRefPtr<HTMLFontElement> HTMLFontElement::create(const QualifiedName& tagName, Document* document)
{
return adoptRef(new HTMLFontElement(tagName, document));
}
static bool parseFontSize(const String& input, int& size)
{
const UChar* position = input.characters();
const UChar* end = position + input.length();
while (position < end) {
if (!isHTMLSpace(*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;
}
Vector<UChar, 16> digits;
while (position < end) {
if (!isASCIIDigit(*position))
break;
digits.append(*position++);
}
if (digits.isEmpty())
return false;
int value = charactersToIntStrict(digits.data(), digits.size());
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;
}
bool HTMLFontElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
{
if (attrName == sizeAttr ||
attrName == colorAttr ||
attrName == faceAttr) {
result = eUniversal;
return false;
}
return HTMLElement::mapToEntry(attrName, result);
}
bool HTMLFontElement::cssValueFromFontSizeNumber(const String& s, int& 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;
}
void HTMLFontElement::parseMappedAttribute(Attribute* attr)
{
if (attr->name() == sizeAttr) {
int size = 0;
if (cssValueFromFontSizeNumber(attr->value(), size))
addCSSProperty(attr, CSSPropertyFontSize, size);
} else if (attr->name() == colorAttr) {
addCSSColor(attr, CSSPropertyColor, attr->value());
} else if (attr->name() == faceAttr) {
addCSSProperty(attr, CSSPropertyFontFamily, attr->value());
} else
HTMLElement::parseMappedAttribute(attr);
}
}