JSCSSStyleDeclarationCustom.cpp [plain text]
#include "config.h"
#include "JSCSSStyleDeclarationCustom.h"
#include "CSSMutableStyleDeclaration.h"
#include "CSSPrimitiveValue.h"
#include "CSSValue.h"
#include "JSCSSValue.h"
#include "JSNode.h"
#include "PlatformString.h"
#include <runtime/StringPrototype.h>
#include <wtf/ASCIICType.h>
#include <wtf/text/AtomicString.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/text/StringConcatenate.h>
using namespace JSC;
using namespace WTF;
namespace WebCore {
void JSCSSStyleDeclaration::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
JSCSSStyleDeclaration* thisObject = jsCast<JSCSSStyleDeclaration*>(cell);
ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
Base::visitChildren(thisObject, visitor);
visitor.addOpaqueRoot(root(thisObject->impl()));
}
static bool hasCSSPropertyNamePrefix(const Identifier& propertyName, const char* prefix)
{
#ifndef NDEBUG
ASSERT(*prefix);
for (const char* p = prefix; *p; ++p)
ASSERT(isASCIILower(*p));
ASSERT(propertyName.length());
#endif
if (toASCIILower(propertyName.characters()[0]) != prefix[0])
return false;
unsigned length = propertyName.length();
for (unsigned i = 1; i < length; ++i) {
if (!prefix[i])
return isASCIIUpper(propertyName.characters()[i]);
if (propertyName.characters()[i] != prefix[i])
return false;
}
return false;
}
static String cssPropertyName(const Identifier& propertyName, bool* hadPixelOrPosPrefix = 0)
{
if (hadPixelOrPosPrefix)
*hadPixelOrPosPrefix = false;
unsigned length = propertyName.length();
if (!length)
return String();
StringBuilder builder;
builder.reserveCapacity(length);
unsigned i = 0;
if (hasCSSPropertyNamePrefix(propertyName, "css"))
i += 3;
else if (hasCSSPropertyNamePrefix(propertyName, "pixel")) {
i += 5;
if (hadPixelOrPosPrefix)
*hadPixelOrPosPrefix = true;
} else if (hasCSSPropertyNamePrefix(propertyName, "pos")) {
i += 3;
if (hadPixelOrPosPrefix)
*hadPixelOrPosPrefix = true;
} else if (hasCSSPropertyNamePrefix(propertyName, "webkit")
|| hasCSSPropertyNamePrefix(propertyName, "khtml")
|| hasCSSPropertyNamePrefix(propertyName, "apple")
|| hasCSSPropertyNamePrefix(propertyName, "epub"))
builder.append('-');
else {
if (isASCIIUpper(propertyName.characters()[0]))
return String();
}
builder.append(toASCIILower(propertyName.characters()[i++]));
for (; i < length; ++i) {
UChar c = propertyName.characters()[i];
if (!isASCIIUpper(c))
builder.append(c);
else
builder.append(makeString('-', toASCIILower(c)));
}
return builder.toString();
}
static bool isCSSPropertyName(const Identifier& propertyIdentifier)
{
return CSSStyleDeclaration::isPropertyName(cssPropertyName(propertyIdentifier));
}
bool JSCSSStyleDeclaration::canGetItemsForName(ExecState*, CSSStyleDeclaration*, const Identifier& propertyName)
{
return isCSSPropertyName(propertyName);
}
JSValue JSCSSStyleDeclaration::nameGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
{
JSCSSStyleDeclaration* thisObj = static_cast<JSCSSStyleDeclaration*>(asObject(slotBase));
bool pixelOrPos;
String prop = cssPropertyName(propertyName, &pixelOrPos);
RefPtr<CSSValue> v = thisObj->impl()->getPropertyCSSValue(prop);
if (v) {
if (pixelOrPos && v->cssValueType() == CSSValue::CSS_PRIMITIVE_VALUE)
return jsNumber(static_pointer_cast<CSSPrimitiveValue>(v)->getFloatValue(CSSPrimitiveValue::CSS_PX));
return jsStringOrNull(exec, v->cssText());
}
return jsString(exec, thisObj->impl()->getPropertyValue(prop));
}
bool JSCSSStyleDeclaration::putDelegate(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot&)
{
bool pixelOrPos;
String prop = cssPropertyName(propertyName, &pixelOrPos);
if (!CSSStyleDeclaration::isPropertyName(prop))
return false;
String propValue = valueToStringWithNullCheck(exec, value);
if (pixelOrPos)
propValue += "px";
ExceptionCode ec = 0;
impl()->setProperty(prop, propValue, ec);
setDOMException(exec, ec);
return true;
}
JSValue JSCSSStyleDeclaration::getPropertyCSSValue(ExecState* exec)
{
const String& propertyName(ustringToString(exec->argument(0).toString(exec)));
if (exec->hadException())
return jsUndefined();
RefPtr<CSSValue> cssValue = impl()->getPropertyCSSValue(propertyName);
if (!cssValue)
return jsNull();
currentWorld(exec)->m_cssValueRoots.add(cssValue.get(), root(impl())); return toJS(exec, globalObject(), WTF::getPtr(cssValue));
}
}