📄 kjs_css.cpp
字号:
/* * This file is part of the KDE libraries * Copyright (C) 2000 Harri Porten (porten@kde.org) * Copyright (C) 2001 Peter Kelly (pmk@post.com) * * 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 <qptrdict.h>#include <qregexp.h>#include <xml/dom_nodeimpl.h>#include <dom/html_element.h>#include <dom/dom_string.h>#include <html/html_elementimpl.h>#include <rendering/render_style.h>#include <kjs/types.h>#include <css/cssproperties.h>#include <css/cssparser.h>#include "kjs_binding.h"#include "kjs_dom.h"using namespace KJS;#include <kdebug.h>QPtrDict<DOMCSSStyleDeclaration> domCSSStyleDeclarations;QPtrDict<DOMStyleSheet> styleSheets;QPtrDict<DOMStyleSheetList> styleSheetLists;QPtrDict<DOMMediaList> mediaLists;QPtrDict<DOMCSSRuleList> cssRuleLists;QPtrDict<DOMCSSRule> cssRules;QPtrDict<DOMCSSValue> cssValues;QPtrDict<DOMRect> rects;QPtrDict<DOMCounter> counters;static QString jsNameToProp( const UString &p ){ QString prop = p.qstring(); int i = prop.length(); while( --i ) { char c = prop[i].latin1(); if ( c < 'A' || c > 'Z' ) continue; prop.insert( i, '-' ); } return prop.lower();}DOMCSSStyleDeclaration::~DOMCSSStyleDeclaration(){ domCSSStyleDeclarations.remove(styleDecl.handle());}const TypeInfo DOMCSSStyleDeclaration::info = { "CSSStyleDeclaration", HostType, 0, 0, 0 };bool DOMCSSStyleDeclaration::hasProperty(const UString &p, bool recursive) const{ if (p == "cssText" || p == "getPropertyValue" || p == "getPropertyCSSValue" || p == "removeProperty" || p == "getPropertyPriority" || p == "setProperty" || p == "length" || p == "item") return true; DOM::DOMString cssprop = jsNameToProp(p); if (DOM::getPropertyID(cssprop.string().ascii(), cssprop.length())) return true; return (recursive && HostImp::hasProperty(p, true));}KJSO DOMCSSStyleDeclaration::tryGet(const UString &p) const{ if (p == "cssText" ) return getString(styleDecl.cssText()); else if (p == "getPropertyValue") return new DOMCSSStyleDeclarationFunc(styleDecl,DOMCSSStyleDeclarationFunc::GetPropertyValue); else if (p == "getPropertyCSSValue") return new DOMCSSStyleDeclarationFunc(styleDecl,DOMCSSStyleDeclarationFunc::GetPropertyCSSValue); else if (p == "removeProperty") return new DOMCSSStyleDeclarationFunc(styleDecl,DOMCSSStyleDeclarationFunc::RemoveProperty); else if (p == "getPropertyPriority") return new DOMCSSStyleDeclarationFunc(styleDecl,DOMCSSStyleDeclarationFunc::GetPropertyPriority); else if (p == "setProperty") return new DOMCSSStyleDeclarationFunc(styleDecl,DOMCSSStyleDeclarationFunc::SetProperty); else if (p == "length" ) return Number(styleDecl.length()); else if (p == "item") return new DOMCSSStyleDeclarationFunc(styleDecl,DOMCSSStyleDeclarationFunc::Item); else if (p == "parentRule" ) return Undefined(); // ### else { bool ok; long unsigned int u = p.toULong(&ok); if (ok) return getString(DOM::CSSStyleDeclaration(styleDecl).item(u)); DOM::CSSStyleDeclaration styleDecl2 = styleDecl; DOM::DOMString v = styleDecl2.getPropertyValue(DOM::DOMString(jsNameToProp(p))); if (!v.isNull()) return getString(v); } return DOMObject::tryGet(p);}void DOMCSSStyleDeclaration::tryPut(const UString &p, const KJSO& v){ if (p == "cssText") { styleDecl.setCssText(v.toString().value().string()); } else { QString prop = jsNameToProp(p); QString propvalue = v.toString().value().qstring(); if(prop.startsWith( "pixel-") || prop.startsWith( "pos-" ) ) { prop = prop.mid(prop.find( '-' )+1); propvalue += "px"; } styleDecl.removeProperty(prop); if(!propvalue.isEmpty()) styleDecl.setProperty(prop,DOM::DOMString(propvalue),""); // ### is "" ok for priority? }}Completion DOMCSSStyleDeclarationFunc::tryExecute(const List &args){ KJSO result; String str = args[0].toString(); DOM::DOMString s = str.value().string(); switch (id) { case GetPropertyValue: result = getString(styleDecl.getPropertyValue(s)); break; case GetPropertyCSSValue: result = Undefined(); // ### break; case RemoveProperty: result = getString(styleDecl.removeProperty(s)); break; case GetPropertyPriority: result = getString(styleDecl.getPropertyPriority(s)); break; case SetProperty: styleDecl.setProperty(args[0].toString().value().string(), args[1].toString().value().string(), args[2].toString().value().string()); result = Undefined(); break; case Item: result = getString(styleDecl.item(args[0].toNumber().intValue())); break; default: result = Undefined(); } return Completion(ReturnValue, result);}KJSO KJS::getDOMCSSStyleDeclaration(DOM::CSSStyleDeclaration s){ DOMCSSStyleDeclaration *ret; if (s.isNull()) return Null(); else if ((ret = domCSSStyleDeclarations[s.handle()])) return ret; else { ret = new DOMCSSStyleDeclaration(s); domCSSStyleDeclarations.insert(s.handle(),ret); return ret; }}// -------------------------------------------------------------------------const TypeInfo DOMStyleSheet::info = { "StyleSheet", HostType, 0, 0, 0 };DOMStyleSheet::~DOMStyleSheet(){ styleSheets.remove(styleSheet.handle());}KJSO DOMStyleSheet::tryGet(const UString &p) const{ KJSO result; if (p == "type") return getString(styleSheet.type()); else if (p == "disabled") return Boolean(styleSheet.disabled()); else if (p == "ownerNode") return getDOMNode(styleSheet.ownerNode()); else if (p == "parentStyleSheet") return getDOMStyleSheet(styleSheet.parentStyleSheet()); else if (p == "href") return getString(styleSheet.href()); else if (p == "title") return getString(styleSheet.title());// else if ( p == "media") ###// return getDOMMediaList(styleSheet.media()); return DOMObject::tryGet(p);}void DOMStyleSheet::tryPut(const UString &p, const KJSO& v){ if (p == "disabled") { styleSheet.setDisabled(v.toBoolean().value()); } else DOMObject::tryPut(p, v);}KJSO KJS::getDOMStyleSheet(DOM::StyleSheet ss){ DOMStyleSheet *ret; if (ss.isNull()) return Null(); else if ((ret = styleSheets[ss.handle()])) return ret; else { if (ss.isCSSStyleSheet()) { DOM::CSSStyleSheet cs; cs = ss; ret = new DOMCSSStyleSheet(cs); } else ret = new DOMStyleSheet(ss); styleSheets.insert(ss.handle(),ret); return ret; }}// -------------------------------------------------------------------------const TypeInfo DOMStyleSheetList::info = { "StyleSheetList", HostType, 0, 0, 0 };DOMStyleSheetList::~DOMStyleSheetList(){ styleSheetLists.remove(styleSheetList.handle());}KJSO DOMStyleSheetList::tryGet(const UString &p) const{ if (p == "length") return Number(styleSheetList.length()); else if (p == "item") return new DOMStyleSheetListFunc(styleSheetList,DOMStyleSheetListFunc::Item); bool ok; long unsigned int u = p.toULong(&ok); if (ok) return getDOMStyleSheet(DOM::StyleSheetList(styleSheetList).item(u)); return DOMObject::tryGet(p);}KJSO KJS::getDOMStyleSheetList(DOM::StyleSheetList ssl){ DOMStyleSheetList *ret; if (ssl.isNull()) return Null(); else if ((ret = styleSheetLists[ssl.handle()])) return ret; else { ret = new DOMStyleSheetList(ssl); styleSheetLists.insert(ssl.handle(),ret); return ret; }}Completion DOMStyleSheetListFunc::tryExecute(const List &args){ KJSO result; if (id == Item) result = getDOMStyleSheet(styleSheetList.item(args[0].toNumber().intValue())); return Completion(ReturnValue, result);}// -------------------------------------------------------------------------const TypeInfo DOMMediaList::info = { "MediaList", HostType, 0, 0, 0 };DOMMediaList::~DOMMediaList(){ mediaLists.remove(mediaList.handle());}KJSO DOMMediaList::tryGet(const UString &p) const{ DOM::MediaList list = DOM::MediaList(mediaList.handle());// DOM::MediaListImpl *handle = mediaList.handle(); if (p == "mediaText") return getString(list.mediaText()); else if (p == "length") return Number(list.length()); else if (p == "item") return new DOMMediaListFunc(list,DOMMediaListFunc::Item); else if (p == "deleteMedium") return new DOMMediaListFunc(list,DOMMediaListFunc::DeleteMedium); else if (p == "appendMedium") return new DOMMediaListFunc(list,DOMMediaListFunc::AppendMedium); bool ok; long unsigned int u = p.toULong(&ok); if (ok) return getString(list.item(u)); return DOMObject::tryGet(p);}void DOMMediaList::tryPut(const UString &p, const KJSO& v){ if (p == "mediaText") mediaList.setMediaText(v.toString().value().string()); else DOMObject::tryPut(p, v);}KJSO KJS::getDOMMediaList(DOM::MediaList ml){ DOMMediaList *ret; if (ml.isNull()) return Null(); else if ((ret = mediaLists[ml.handle()])) return ret; else { ret = new DOMMediaList(ml); mediaLists.insert(ml.handle(),ret); return ret; }}Completion DOMMediaListFunc::tryExecute(const List &args){ KJSO result; switch (id) { case Item: result = getString(mediaList.item(args[0].toNumber().intValue())); break; case DeleteMedium: mediaList.deleteMedium(args[0].toString().value().string()); result = Undefined(); break; case AppendMedium: mediaList.appendMedium(args[0].toString().value().string()); result = Undefined(); break; default: break; } return Completion(ReturnValue, result);}// -------------------------------------------------------------------------const TypeInfo DOMCSSStyleSheet::info = { "CSSStyleSheet", HostType, 0, 0, 0 };DOMCSSStyleSheet::~DOMCSSStyleSheet(){}KJSO DOMCSSStyleSheet::tryGet(const UString &p) const{ KJSO result; DOM::CSSStyleSheet cssStyleSheet = static_cast<DOM::CSSStyleSheet>(styleSheet); if (p == "ownerRule") return getDOMCSSRule(cssStyleSheet.ownerRule()); else if (p == "cssRules") return getDOMCSSRuleList(cssStyleSheet.cssRules()); else if (p == "insertRule") return new DOMCSSStyleSheetFunc(cssStyleSheet,DOMCSSStyleSheetFunc::InsertRule); else if (p == "deleteRule") return new DOMCSSStyleSheetFunc(cssStyleSheet,DOMCSSStyleSheetFunc::DeleteRule); return DOMStyleSheet::tryGet(p);}Completion DOMCSSStyleSheetFunc::tryExecute(const List &args){ KJSO result; String str = args[0].toString(); DOM::DOMString s = str.value().string(); switch (id) { case InsertRule: result = Number(styleSheet.insertRule(args[0].toString().value().string(),(long unsigned int)args[1].toNumber().intValue())); break; case DeleteRule: styleSheet.deleteRule(args[0].toNumber().intValue()); break; default: result = Undefined(); } return Completion(ReturnValue, result);}// -------------------------------------------------------------------------const TypeInfo DOMCSSRuleList::info = { "CSSRuleList", HostType, 0, 0, 0 };DOMCSSRuleList::~DOMCSSRuleList(){ cssRuleLists.remove(cssRuleList.handle());}KJSO DOMCSSRuleList::tryGet(const UString &p) const{ KJSO result; if (p == "length") return Number(cssRuleList.length()); else if (p == "item") return new DOMCSSRuleListFunc(cssRuleList,DOMCSSRuleListFunc::Item); bool ok; long unsigned int u = p.toULong(&ok); if (ok) return getDOMCSSRule(DOM::CSSRuleList(cssRuleList).item(u)); return DOMObject::tryGet(p);}Completion DOMCSSRuleListFunc::tryExecute(const List &args){ KJSO result; switch (id) { case Item: result = getDOMCSSRule(cssRuleList.item(args[0].toNumber().intValue())); break; default: result = Undefined(); } return Completion(ReturnValue, result);}KJSO KJS::getDOMCSSRuleList(DOM::CSSRuleList rl){ DOMCSSRuleList *ret; if (rl.isNull()) return Null(); else if ((ret = cssRuleLists[rl.handle()])) return ret; else { ret = new DOMCSSRuleList(rl); cssRuleLists.insert(rl.handle(),ret); return ret; }}// -------------------------------------------------------------------------const TypeInfo DOMCSSRule::info = { "CSSRule", HostType, 0, 0, 0 };DOMCSSRule::~DOMCSSRule(){ cssRules.remove(cssRule.handle());}KJSO DOMCSSRule::tryGet(const UString &p) const{ KJSO result; switch (cssRule.type()) { case DOM::CSSRule::STYLE_RULE: { DOM::CSSStyleRule rule = static_cast<DOM::CSSStyleRule>(cssRule); if (p == "selectorText") return getString(rule.selectorText()); if (p == "style") return getDOMCSSStyleDeclaration(rule.style()); break; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -