JSCSSStyleDeclarationCustom.cpp [plain text]
#include "config.h"
#include "JSCSSStyleDeclaration.h"
#include "AtomicString.h"
#include "CSSPrimitiveValue.h"
#include "CSSStyleDeclaration.h"
#include "CSSValue.h"
#include "DeprecatedString.h"
#include "PlatformString.h"
#include <kjs/string_object.h>
namespace WebCore {
using namespace KJS;
static String cssPropertyName(const Identifier& propertyName, bool* hadPixelOrPosPrefix = 0)
{
DeprecatedString prop = propertyName;
int i = prop.length();
if (!i)
return prop;
while (--i) {
::UChar c = prop[i].unicode();
if (c >= 'A' && c <= 'Z')
prop.insert(i, '-');
}
prop = prop.lower();
if (hadPixelOrPosPrefix)
*hadPixelOrPosPrefix = false;
if (prop.startsWith("css-"))
prop = prop.mid(4);
else if (prop.startsWith("pixel-")) {
prop = prop.mid(6);
if (hadPixelOrPosPrefix)
*hadPixelOrPosPrefix = true;
} else if (prop.startsWith("pos-")) {
prop = prop.mid(4);
if (hadPixelOrPosPrefix)
*hadPixelOrPosPrefix = true;
} else if (prop.startsWith("khtml-") || prop.startsWith("apple-") || prop.startsWith("webkit-"))
prop.insert(0, '-');
return prop;
}
static bool isCSSPropertyName(const Identifier& propertyName)
{
return CSSStyleDeclaration::isPropertyName(cssPropertyName(propertyName));
}
bool JSCSSStyleDeclaration::canGetItemsForName(ExecState*, CSSStyleDeclaration*, const Identifier& propertyName)
{
return isCSSPropertyName(propertyName);
}
JSValue* JSCSSStyleDeclaration::nameGetter(ExecState* exec, JSObject* originalObject, const Identifier& propertyName, const PropertySlot& slot)
{
JSCSSStyleDeclaration* thisObj = static_cast<JSCSSStyleDeclaration*>(slot.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(v->cssText());
}
if (propertyName == "filter")
return new StringInstanceThatMasqueradesAsUndefined(exec->lexicalGlobalObject()->stringPrototype(), thisObj->impl()->getPropertyValue(prop));
return jsString(thisObj->impl()->getPropertyValue(prop));
}
bool JSCSSStyleDeclaration::customPut(ExecState* exec, const Identifier& propertyName, JSValue* value, int )
{
if (!isCSSPropertyName(propertyName))
return false;
DOMExceptionTranslator exception(exec);
bool pixelOrPos;
String prop = cssPropertyName(propertyName, &pixelOrPos);
String propValue = valueToStringWithNullCheck(exec, value);
if (pixelOrPos)
propValue += "px";
impl()->setProperty(prop, propValue, exception);
return true;
}
}