diff options
Diffstat (limited to 'WebCore/css/CSSStyleSheet.cpp')
-rw-r--r-- | WebCore/css/CSSStyleSheet.cpp | 229 |
1 files changed, 229 insertions, 0 deletions
diff --git a/WebCore/css/CSSStyleSheet.cpp b/WebCore/css/CSSStyleSheet.cpp new file mode 100644 index 0000000..10f58e9 --- /dev/null +++ b/WebCore/css/CSSStyleSheet.cpp @@ -0,0 +1,229 @@ +/* + * (C) 1999-2003 Lars Knoll (knoll@kde.org) + * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. + * + * 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; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "config.h" +#include "CSSStyleSheet.h" + +#include "CSSImportRule.h" +#include "CSSNamespace.h" +#include "CSSParser.h" +#include "CSSRuleList.h" +#include "Document.h" +#include "ExceptionCode.h" +#include "Node.h" + +#ifdef ANDROID_INSTRUMENT +#include "TimeCounter.h" +#endif + +namespace WebCore { + +CSSStyleSheet::CSSStyleSheet(CSSStyleSheet* parentSheet, const String& href, const String& charset) + : StyleSheet(parentSheet, href) + , m_doc(parentSheet ? parentSheet->doc() : 0) + , m_namespaces(0) + , m_charset(charset) + , m_loadCompleted(false) +{ +} + +CSSStyleSheet::CSSStyleSheet(Node *parentNode, const String& href, const String& charset) + : StyleSheet(parentNode, href) + , m_doc(parentNode->document()) + , m_namespaces(0) + , m_charset(charset) + , m_loadCompleted(false) +{ +} + +CSSStyleSheet::CSSStyleSheet(CSSRule *ownerRule, const String& href, const String& charset) + : StyleSheet(ownerRule, href) + , m_namespaces(0) + , m_charset(charset) + , m_loadCompleted(false) +{ + CSSStyleSheet* parentSheet = ownerRule ? ownerRule->parentStyleSheet() : 0; + m_doc = parentSheet ? parentSheet->doc() : 0; +} + +CSSStyleSheet::~CSSStyleSheet() +{ + delete m_namespaces; +} + +CSSRule *CSSStyleSheet::ownerRule() const +{ + return (parent() && parent()->isRule()) ? static_cast<CSSRule*>(parent()) : 0; +} + +unsigned CSSStyleSheet::insertRule(const String& rule, unsigned index, ExceptionCode& ec) +{ + ec = 0; + if (index > length()) { + ec = INDEX_SIZE_ERR; + return 0; + } + CSSParser p(useStrictParsing()); + RefPtr<CSSRule> r = p.parseRule(this, rule); + + if (!r) { + ec = SYNTAX_ERR; + return 0; + } + + // ### + // HIERARCHY_REQUEST_ERR: Raised if the rule cannot be inserted at the specified index e.g. if an + //@import rule is inserted after a standard rule set or other at-rule. + insert(index, r.release()); + + styleSheetChanged(); + + return index; +} + +int CSSStyleSheet::addRule(const String& selector, const String& style, int index, ExceptionCode& ec) +{ + insertRule(selector + " { " + style + " }", index, ec); + + // As per Microsoft documentation, always return -1. + return -1; +} + +int CSSStyleSheet::addRule(const String& selector, const String& style, ExceptionCode& ec) +{ + return addRule(selector, style, length(), ec); +} + + +PassRefPtr<CSSRuleList> CSSStyleSheet::cssRules(bool omitCharsetRules) +{ + return CSSRuleList::create(this, omitCharsetRules); +} + +void CSSStyleSheet::deleteRule(unsigned index, ExceptionCode& ec) +{ + if (index >= length()) { + ec = INDEX_SIZE_ERR; + return; + } + + ec = 0; + remove(index); + styleSheetChanged(); +} + +void CSSStyleSheet::addNamespace(CSSParser* p, const AtomicString& prefix, const AtomicString& uri) +{ + if (uri.isEmpty()) + return; + + m_namespaces = new CSSNamespace(prefix, uri, m_namespaces); + + if (prefix.isEmpty()) + // Set the default namespace on the parser so that selectors that omit namespace info will + // be able to pick it up easily. + p->m_defaultNamespace = uri; +} + +const AtomicString& CSSStyleSheet::determineNamespace(const AtomicString& prefix) +{ + if (prefix.isEmpty()) + return nullAtom; // No namespace. If an element/attribute has a namespace, we won't match it. + else if (prefix == starAtom) + return starAtom; // We'll match any namespace. + else if (m_namespaces) { + CSSNamespace* ns = m_namespaces->namespaceForPrefix(prefix); + if (ns) + return ns->uri(); + } + return nullAtom; // Assume we wont match any namespaces. +} + +bool CSSStyleSheet::parseString(const String &string, bool strict) +{ +#ifdef ANDROID_INSTRUMENT + android::TimeCounter::start(android::TimeCounter::CSSTimeCounter); +#endif + setStrictParsing(strict); + CSSParser p(strict); + p.parseSheet(this, string); +#ifdef ANDROID_INSTRUMENT + android::TimeCounter::record(android::TimeCounter::CSSTimeCounter, __FUNCTION__); +#endif + return true; +} + +bool CSSStyleSheet::isLoading() +{ + unsigned len = length(); + for (unsigned i = 0; i < len; ++i) { + StyleBase* rule = item(i); + if (rule->isImportRule() && static_cast<CSSImportRule*>(rule)->isLoading()) + return true; + } + return false; +} + +void CSSStyleSheet::checkLoaded() +{ + if (isLoading()) + return; + if (parent()) + parent()->checkLoaded(); + m_loadCompleted = ownerNode() ? ownerNode()->sheetLoaded() : true; +} + +void CSSStyleSheet::styleSheetChanged() +{ + StyleBase* root = this; + while (StyleBase* parent = root->parent()) + root = parent; + Document* documentToUpdate = (root && root->isCSSStyleSheet()) ? static_cast<CSSStyleSheet*>(root)->doc() : 0; + + /* FIXME: We don't need to do everything updateStyleSelector does, + * basically we just need to recreate the document's selector with the + * already existing style sheets. + */ + if (documentToUpdate) + documentToUpdate->updateStyleSelector(); +} + +void CSSStyleSheet::addSubresourceURLStrings(HashSet<String>& urls, const String& base) const +{ + RefPtr<CSSRuleList> ruleList = const_cast<CSSStyleSheet*>(this)->cssRules(); + + // Add the URLs for each child import rule, and recurse for the stylesheet belonging to each of those rules. + for (unsigned i = 0; i < ruleList->length(); ++i) { + CSSRule* rule = ruleList->item(i); + if (rule->type() != CSSRule::IMPORT_RULE) + continue; + + CSSImportRule* importRule = static_cast<CSSImportRule*>(rule); + CSSStyleSheet* ruleSheet = importRule->styleSheet(); + if (!ruleSheet) + continue; + + KURL fullURL(KURL(base), importRule->href()); + urls.add(fullURL.string()); + ruleSheet->addSubresourceURLStrings(urls, fullURL.string()); + } +} + +} |