// -*- 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) 2003 Apple Computer, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "kjs_css.h" #include "kjs_css.lut.h" #include // for HTMLStyleElement #include #include "kjs_dom.h" #include namespace KJS { static TQString cssPropertyName( const Identifier &p, bool& hadPixelPrefix ) { TQString 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(); hadPixelPrefix = false; if (prop.startsWith("css-")) { prop = prop.mid(4); } else if (prop.startsWith("pixel-")) { prop = prop.mid(6); hadPixelPrefix = true; } else if (prop.startsWith("pos-")) { prop = prop.mid(4); hadPixelPrefix = true; } 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 # IE names for it (#36063) getAttribute DOMCSSStyleDeclaration::GetPropertyValue DontDelete|Function 1 removeAttribute DOMCSSStyleDeclaration::RemoveProperty DontDelete|Function 1 setAttribute DOMCSSStyleDeclaration::SetProperty DontDelete|Function 3 @end @begin DOMCSSStyleDeclarationTable 3 cssText DOMCSSStyleDeclaration::CssText DontDelete length DOMCSSStyleDeclaration::Length DontDelete|ReadOnly parentRule DOMCSSStyleDeclaration::ParentRule DontDelete|ReadOnly @end */ KJS_DEFINE_PROTOTYPE(DOMCSSStyleDeclarationProto) IMPLEMENT_PROTOFUNC_DOM(DOMCSSStyleDeclarationProtoFunc) KJS_IMPLEMENT_PROTOTYPE("DOMCSSStyleDeclaration", DOMCSSStyleDeclarationProto, DOMCSSStyleDeclarationProtoFunc) IMPLEMENT_PSEUDO_CONSTRUCTOR(CSSStyleDeclarationPseudoCtor, "DOMCSSStyleDeclaration",DOMCSSStyleDeclarationProto) const ClassInfo DOMCSSStyleDeclaration::info = { "CSSStyleDeclaration", 0, &DOMCSSStyleDeclarationTable, 0 }; DOMCSSStyleDeclaration::DOMCSSStyleDeclaration(ExecState *exec, const DOM::CSSStyleDeclaration& s) : DOMObject(DOMCSSStyleDeclarationProto::self(exec)), styleDecl(s) { } DOMCSSStyleDeclaration::~DOMCSSStyleDeclaration() { ScriptInterpreter::forgetDOMObject(styleDecl.handle()); } bool DOMCSSStyleDeclaration::hasProperty(ExecState *exec, const Identifier &p) const { bool hadPixelPrefix; TQString cssprop = cssPropertyName(p, hadPixelPrefix); if (DOM::getPropertyID(cssprop.latin1(), cssprop.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 String(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.isValid() && proto.hasProperty(exec,propertyName)) return proto.get(exec,propertyName); bool ok; long unsigned int u = propertyName.toULong(&ok); if (ok) return String(DOM::CSSStyleDeclaration(styleDecl).item(u)); // 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 asNumber; TQString p = cssPropertyName(propertyName, asNumber); #ifdef KJS_VERBOSE kdDebug(6070) << "DOMCSSStyleDeclaration: converting to css property name: " << p << ( asNumber ? "px" : "" ) << endl; #endif if (asNumber) { DOM::CSSValue v = styleDecl.getPropertyCSSValue(p); if ( !v.isNull() && v.cssValueType() == DOM::CSSValue::CSS_PRIMITIVE_VALUE) return Number(static_cast(v).getFloatValue(DOM::CSSPrimitiveValue::CSS_PX)); } DOM::DOMString str = const_cast( styleDecl ).getPropertyValue(p); if (!str.isNull()) return String(str); // see if we know this css property, return empty then if (DOM::getPropertyID(p.latin1(), p.length())) return String(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 pxSuffix; TQString prop = cssPropertyName(propertyName, pxSuffix); TQString propvalue = value.toString(exec).qstring(); if (pxSuffix) propvalue += "px"; #ifdef KJS_VERBOSE kdDebug(6070) << "DOMCSSStyleDeclaration: prop=" << prop << " propvalue=" << propvalue << endl; #endif // Look whether the property is known.d In that case add it as a CSS property. if (DOM::getPropertyID(prop.latin1(), prop.length())) { if (propvalue.isEmpty()) styleDecl.removeProperty(prop); else { int important = propvalue.find("!important", 0, false); if (important == -1) styleDecl.setProperty(prop, DOM::DOMString(propvalue), ""); else styleDecl.setProperty(prop, DOM::DOMString(propvalue.left(important - 1)), "important"); } } else // otherwise add it as a JS property DOMObject::tryPut( exec, propertyName, value, attr ); } } Value DOMCSSStyleDeclarationProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args) { KJS_CHECK_THIS( KJS::DOMCSSStyleDeclaration, thisObj ); 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 String(styleDecl.getPropertyValue(s)); case DOMCSSStyleDeclaration::GetPropertyCSSValue: return getDOMCSSValue(exec,styleDecl.getPropertyCSSValue(s)); case DOMCSSStyleDeclaration::RemoveProperty: return String(styleDecl.removeProperty(s)); case DOMCSSStyleDeclaration::GetPropertyPriority: return String(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 String(styleDecl.item(args[0].toInteger(exec))); default: return Undefined(); } } Value getDOMCSSStyleDeclaration(ExecState *exec, const DOM::CSSStyleDeclaration& s) { return 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(ExecState* exec, const DOM::StyleSheet& ss) : DOMObject(exec->interpreter()->builtinObjectPrototype()), styleSheet(ss) { } 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 String(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 String(styleSheet.href()); case Title: return String(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 getDOMStyleSheet(ExecState *exec, const DOM::StyleSheet& ss) { DOMObject *ret; if (ss.isNull()) return Null(); ScriptInterpreter* interp = static_cast(exec->interpreter()); 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_DOM(DOMStyleSheetListFunc) // not really a proto, but doesn't matter DOMStyleSheetList::DOMStyleSheetList(ExecState *exec, const DOM::StyleSheetList& ssl, const DOM::Document& doc) : DOMObject(exec->interpreter()->builtinObjectPrototype()), styleSheetList(ssl), m_doc(doc) { } 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