// -*- c-basic-offset: 2 -*- /* * This file is part of the KDE libraries * Copyright (C) 2000 Harri Porten (porten@kde.org) * Copyright (C) 2001 Peter Kelly (pmk@post.com) * Copyright (C) 2004 Apple Computer, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "kjs_css.h" #include "kjs_css.lut.h" #include // for HTMLStyleElement #include #include "kjs_dom.h" using namespace KJS; #include static QString cssPropertyName(const Identifier &p, bool *hadPixelOrPosPrefix = 0) { QString prop = p.qstring(); int i = prop.length(); while (--i) { char c = prop[i].latin1(); 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.insert(0, '-'); } return prop; } /* @begin DOMCSSStyleDeclarationProtoTable 7 getPropertyValue DOMCSSStyleDeclaration::GetPropertyValue DontDelete|Function 1 getPropertyCSSValue DOMCSSStyleDeclaration::GetPropertyCSSValue DontDelete|Function 1 removeProperty DOMCSSStyleDeclaration::RemoveProperty DontDelete|Function 1 getPropertyPriority DOMCSSStyleDeclaration::GetPropertyPriority DontDelete|Function 1 setProperty DOMCSSStyleDeclaration::SetProperty DontDelete|Function 3 item DOMCSSStyleDeclaration::Item DontDelete|Function 1 @end @begin DOMCSSStyleDeclarationTable 3 cssText DOMCSSStyleDeclaration::CssText DontDelete length DOMCSSStyleDeclaration::Length DontDelete|ReadOnly parentRule DOMCSSStyleDeclaration::ParentRule DontDelete|ReadOnly @end */ DEFINE_PROTOTYPE("DOMCSSStyleDeclaration", DOMCSSStyleDeclarationProto) IMPLEMENT_PROTOFUNC(DOMCSSStyleDeclarationProtoFunc) IMPLEMENT_PROTOTYPE(DOMCSSStyleDeclarationProto, DOMCSSStyleDeclarationProtoFunc) const ClassInfo DOMCSSStyleDeclaration::info = { "CSSStyleDeclaration", 0, &DOMCSSStyleDeclarationTable, 0 }; DOMCSSStyleDeclaration::DOMCSSStyleDeclaration(ExecState *exec, DOM::CSSStyleDeclaration s) : DOMObject(DOMCSSStyleDeclarationProto::self(exec)), styleDecl(s) { } DOMCSSStyleDeclaration::~DOMCSSStyleDeclaration() { ScriptInterpreter::forgetDOMObject(styleDecl.handle()); } bool DOMCSSStyleDeclaration::hasProperty(ExecState *exec, const Identifier &p) const { if (p == "cssText") return true; QString prop = cssPropertyName(p); if (DOM::getPropertyID(prop.ascii(), prop.length())) return true; return ObjectImp::hasProperty(exec, p); } Value DOMCSSStyleDeclaration::tryGet(ExecState *exec, const Identifier &propertyName) const { #ifdef KJS_VERBOSE kdDebug(6070) << "DOMCSSStyleDeclaration::tryGet " << propertyName.qstring() << endl; #endif const HashEntry* entry = Lookup::findEntry(&DOMCSSStyleDeclarationTable, propertyName); if (entry) switch (entry->value) { case CssText: return getStringOrNull(styleDecl.cssText()); case Length: return Number(styleDecl.length()); case ParentRule: return getDOMCSSRule(exec,styleDecl.parentRule()); default: break; } // Look in the prototype (for functions) before assuming it's a name Object proto = Object::dynamicCast(prototype()); if (!proto.isNull() && proto.hasProperty(exec,propertyName)) return proto.get(exec,propertyName); bool ok; long unsigned int u = propertyName.toULong(&ok); if (ok) return getStringOrNull(DOM::CSSStyleDeclaration(styleDecl).item(u)); #ifdef KJS_VERBOSE kdDebug(6070) << "DOMCSSStyleDeclaration: converting to css property name: " << cssPropertyName(propertyName) << endl; #endif DOM::CSSStyleDeclaration styleDecl2 = styleDecl; // Set up pixelOrPos boolean to handle the fact that // pixelTop returns "CSS Top" as number value in unit pixels // posTop returns "CSS top" as number value in unit pixels _if_ its a // positioned element. if it is not a positioned element, return 0 // from MSIE documentation ### IMPLEMENT THAT (Dirk) bool pixelOrPos; DOM::DOMString p = cssPropertyName(propertyName, &pixelOrPos); DOM::CSSValue v = styleDecl2.getPropertyCSSValue(p); if (!v.isNull()) { if (pixelOrPos && v.cssValueType() == DOM::CSSValue::CSS_PRIMITIVE_VALUE) return Number(static_cast(v).getFloatValue(DOM::CSSPrimitiveValue::CSS_PX)); return getStringOrNull(v.cssText()); } // see if we know this css property, return empty then QCString prop = p.string().latin1(); if (DOM::getPropertyID(prop.data(), prop.length())) return getStringOrNull(DOM::DOMString("")); return DOMObject::tryGet(exec, propertyName); } void DOMCSSStyleDeclaration::tryPut(ExecState *exec, const Identifier &propertyName, const Value& value, int attr ) { #ifdef KJS_VERBOSE kdDebug(6070) << "DOMCSSStyleDeclaration::tryPut " << propertyName.qstring() << endl; #endif if (propertyName == "cssText") { styleDecl.setCssText(value.toString(exec).string()); } else { bool pixelOrPos; QString prop = cssPropertyName(propertyName, &pixelOrPos); if (prop.isAllASCII() && DOM::getPropertyID(prop.ascii(), prop.length())) { QString propvalue = value.toString(exec).qstring(); if (pixelOrPos) propvalue += "px"; #ifdef KJS_VERBOSE kdDebug(6070) << "DOMCSSStyleDeclaration: prop=" << prop << " propvalue=" << propvalue << endl; #endif styleDecl.removeProperty(prop); if(!propvalue.isEmpty()) styleDecl.setProperty(prop,DOM::DOMString(propvalue),""); // ### is "" ok for priority? } else { DOMObject::tryPut(exec, propertyName, value, attr); } } } Value DOMCSSStyleDeclarationProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args) { if (!thisObj.inherits(&KJS::DOMCSSStyleDeclaration::info)) { Object err = Error::create(exec,TypeError); exec->setException(err); return err; } DOM::CSSStyleDeclaration styleDecl = static_cast(thisObj.imp())->toStyleDecl(); String str = args[0].toString(exec); DOM::DOMString s = str.value().string(); switch (id) { case DOMCSSStyleDeclaration::GetPropertyValue: return getStringOrNull(styleDecl.getPropertyValue(s)); case DOMCSSStyleDeclaration::GetPropertyCSSValue: return getDOMCSSValue(exec,styleDecl.getPropertyCSSValue(s)); case DOMCSSStyleDeclaration::RemoveProperty: return getStringOrNull(styleDecl.removeProperty(s)); case DOMCSSStyleDeclaration::GetPropertyPriority: return getStringOrNull(styleDecl.getPropertyPriority(s)); case DOMCSSStyleDeclaration::SetProperty: styleDecl.setProperty(args[0].toString(exec).string(), args[1].toString(exec).string(), args[2].toString(exec).string()); return Undefined(); case DOMCSSStyleDeclaration::Item: return getStringOrNull(styleDecl.item(args[0].toInt32(exec))); default: return Undefined(); } } Value KJS::getDOMCSSStyleDeclaration(ExecState *exec, DOM::CSSStyleDeclaration s) { return Value(cacheDOMObject(exec, s)); } // ------------------------------------------------------------------------- const ClassInfo DOMStyleSheet::info = { "StyleSheet", 0, &DOMStyleSheetTable, 0 }; /* @begin DOMStyleSheetTable 7 type DOMStyleSheet::Type DontDelete|ReadOnly disabled DOMStyleSheet::Disabled DontDelete ownerNode DOMStyleSheet::OwnerNode DontDelete|ReadOnly parentStyleSheet DOMStyleSheet::ParentStyleSheet DontDelete|ReadOnly href DOMStyleSheet::Href DontDelete|ReadOnly title DOMStyleSheet::Title DontDelete|ReadOnly media DOMStyleSheet::Media DontDelete|ReadOnly @end */ DOMStyleSheet::~DOMStyleSheet() { ScriptInterpreter::forgetDOMObject(styleSheet.handle()); } Value DOMStyleSheet::tryGet(ExecState *exec, const Identifier &propertyName) const { return DOMObjectLookupGetValue(exec,propertyName,&DOMStyleSheetTable,this); } Value DOMStyleSheet::getValueProperty(ExecState *exec, int token) const { switch (token) { case Type: return getStringOrNull(styleSheet.type()); case Disabled: return Boolean(styleSheet.disabled()); case OwnerNode: return getDOMNode(exec,styleSheet.ownerNode()); case ParentStyleSheet: return getDOMStyleSheet(exec,styleSheet.parentStyleSheet()); case Href: return getStringOrNull(styleSheet.href()); case Title: return getStringOrNull(styleSheet.title()); case Media: return getDOMMediaList(exec, styleSheet.media()); } return Value(); } void DOMStyleSheet::tryPut(ExecState *exec, const Identifier &propertyName, const Value& value, int attr) { if (propertyName == "disabled") { styleSheet.setDisabled(value.toBoolean(exec)); } else DOMObject::tryPut(exec, propertyName, value, attr); } Value KJS::getDOMStyleSheet(ExecState *exec, DOM::StyleSheet ss) { DOMObject *ret; if (ss.isNull()) return Null(); ScriptInterpreter* interp = static_cast(exec->dynamicInterpreter()); if ((ret = interp->getDOMObject(ss.handle()))) return Value(ret); else { if (ss.isCSSStyleSheet()) { DOM::CSSStyleSheet cs; cs = ss; ret = new DOMCSSStyleSheet(exec,cs); } else ret = new DOMStyleSheet(exec,ss); interp->putDOMObject(ss.handle(),ret); return Value(ret); } } // ------------------------------------------------------------------------- const ClassInfo DOMStyleSheetList::info = { "StyleSheetList", 0, &DOMStyleSheetListTable, 0 }; /* @begin DOMStyleSheetListTable 2 length DOMStyleSheetList::Length DontDelete|ReadOnly item DOMStyleSheetList::Item DontDelete|Function 1 @end */ IMPLEMENT_PROTOFUNC(DOMStyleSheetListFunc) // not really a proto, but doesn't matter DOMStyleSheetList::~DOMStyleSheetList() { ScriptInterpreter::forgetDOMObject(styleSheetList.handle()); } Value DOMStyleSheetList::tryGet(ExecState *exec, const Identifier &p) const { #ifdef KJS_VERBOSE kdDebug(6070) << "DOMStyleSheetList::tryGet " << p.qstring() << endl; #endif if (p == lengthPropertyName) return Number(styleSheetList.length()); else if (p == "item") return lookupOrCreateFunction(exec,p,this,DOMStyleSheetList::Item,1,DontDelete|Function); // Retrieve stylesheet by index bool ok; long unsigned int u = p.toULong(&ok); if (ok) return getDOMStyleSheet(exec, DOM::StyleSheetList(styleSheetList).item(u)); // IE also supports retrieving a stylesheet by name, using the name/id of the