📄 kjs_css.cpp
字号:
// -*- 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 <dom/html_head.h> // for HTMLStyleElement#include <css/css_base.h>#include "kjs_dom.h"using namespace KJS;#include <kdebug.h>static QString cssPropertyName( const Identifier &p, bool& hadPixelPrefix ){ 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(); 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*/DEFINE_PROTOTYPE("DOMCSSStyleDeclaration", DOMCSSStyleDeclarationProto)IMPLEMENT_PROTOFUNC_DOM(DOMCSSStyleDeclarationProtoFunc)IMPLEMENT_PROTOTYPE(DOMCSSStyleDeclarationProto, DOMCSSStyleDeclarationProtoFunc)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; QString 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; QString 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<DOM::CSSPrimitiveValue>(v).getFloatValue(DOM::CSSPrimitiveValue::CSS_PX)); } DOM::DOMString str = const_cast<DOM::CSSStyleDeclaration &>( 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; QString prop = cssPropertyName(propertyName, pxSuffix); QString 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 styleDecl.setProperty(prop,DOM::DOMString(propvalue),""); // ### is "" ok for priority? } 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<DOMCSSStyleDeclaration *>(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 KJS::getDOMCSSStyleDeclaration(ExecState *exec, const DOM::CSSStyleDeclaration& s){ return cacheDOMObject<DOM::CSSStyleDeclaration, KJS::DOMCSSStyleDeclaration>(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<DOMStyleSheet,DOMObject>(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 KJS::getDOMStyleSheet(ExecState *exec, const DOM::StyleSheet& ss){ DOMObject *ret; if (ss.isNull()) return Null(); ScriptInterpreter* interp = static_cast<ScriptInterpreter *>(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 matterDOMStyleSheetList::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<DOMStyleSheetListFunc>(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 <style> tag // (this is consistent with all the other collections)#if 0 // Bad implementation because DOM::StyleSheet doesn't inherit DOM::Node // so we can't use DOMNamedNodesCollection..... // We could duplicate it for stylesheets though - worth it ? // Other problem of this implementation: it doesn't look for the ID attribute! DOM::NameNodeListImpl namedList( m_doc.documentElement().handle(), p.string() ); int len = namedList.length(); if ( len ) { QValueList<DOM::Node> styleSheets; for ( int i = 0 ; i < len ; ++i ) { DOM::HTMLStyleElement elem = DOM::Node(namedList.item(i)); if (!elem.isNull()) styleSheets.append(elem.sheet()); } if ( styleSheets.count() == 1 ) // single result return getDOMStyleSheet(exec, styleSheets[0]); else if ( styleSheets.count() > 1 ) { return new DOMNamedItemsCollection(exec,styleSheets); } }#endif // ### Bad implementation because returns a single element (are IDs always unique?) // and doesn't look for name attribute (see implementation above). // But unicity of stylesheet ids is good practice anyway ;) DOM::DOMString pstr = p.string(); DOM::HTMLStyleElement styleElem = m_doc.getElementById( pstr ); if (!styleElem.isNull()) return getDOMStyleSheet(exec, styleElem.sheet()); return DOMObject::tryGet(exec, p);}Value KJS::DOMStyleSheetList::call(ExecState *exec, Object &thisObj, const List &args){ // This code duplication is necessary, DOMStyleSheetList isn't a DOMFunction Value val; try { val = tryCall(exec, thisObj, args); } // pity there's no way to distinguish between these in JS code catch (...) { Object err = Error::create(exec, GeneralError, "Exception from DOMStyleSheetList"); exec->setException(err); } return val;}Value DOMStyleSheetList::tryCall(ExecState *exec, Object & /*thisObj*/, const List &args){ if (args.size() == 1) { // support for styleSheets(<index>) and styleSheets(<name>) return tryGet( exec, Identifier(args[0].toString(exec)) ); } return Undefined();}Value KJS::getDOMStyleSheetList(ExecState *exec, const DOM::StyleSheetList& ssl, const DOM::Document& doc){ // Can't use the cacheDOMObject macro because of the doc argument DOMObject *ret; if (ssl.isNull()) return Null(); ScriptInterpreter* interp = static_cast<ScriptInterpreter *>(exec->interpreter()); if ((ret = interp->getDOMObject(ssl.handle()))) return Value(ret); else { ret = new DOMStyleSheetList(exec, ssl, doc); interp->putDOMObject(ssl.handle(),ret); return Value(ret); }}Value DOMStyleSheetListFunc::tryCall(ExecState *exec, Object &thisObj, const List &args){ KJS_CHECK_THIS( KJS::DOMStyleSheetList, thisObj ); DOM::StyleSheetList styleSheetList = static_cast<DOMStyleSheetList *>(thisObj.imp())->toStyleSheetList(); if (id == DOMStyleSheetList::Item) return getDOMStyleSheet(exec, styleSheetList.item(args[0].toInteger(exec))); return Undefined();}// -------------------------------------------------------------------------const ClassInfo DOMMediaList::info = { "MediaList", 0, &DOMMediaListTable, 0 };
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -