webengine/osswebengine/WebCore/css/CSSStyleSheet.cpp
changeset 0 dd21522fd290
child 68 92a765b5b3e7
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * (C) 1999-2003 Lars Knoll (knoll@kde.org)
       
     3  * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
       
     4  *
       
     5  * This library is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU Library General Public
       
     7  * License as published by the Free Software Foundation; either
       
     8  * version 2 of the License, or (at your option) any later version.
       
     9  *
       
    10  * This library is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  * Library General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU Library General Public License
       
    16  * along with this library; see the file COPYING.LIB.  If not, write to
       
    17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    18  * Boston, MA 02110-1301, USA.
       
    19  */
       
    20 
       
    21 #include "config.h"
       
    22 #include "CSSStyleSheet.h"
       
    23 
       
    24 #include "CSSImportRule.h"
       
    25 #include "CSSNamespace.h"
       
    26 #include "CSSParser.h"
       
    27 #include "CSSRuleList.h"
       
    28 #include "Document.h"
       
    29 #include "ExceptionCode.h"
       
    30 #include "Node.h"
       
    31 #if PLATFORM(SYMBIAN)
       
    32 #include "oom.h"
       
    33 #endif
       
    34 
       
    35 namespace WebCore {
       
    36 
       
    37 CSSStyleSheet::CSSStyleSheet(CSSStyleSheet* parentSheet, const String& href, const String& charset)
       
    38     : StyleSheet(parentSheet, href)
       
    39     , m_doc(parentSheet ? parentSheet->doc() : 0)
       
    40     , m_namespaces(0)
       
    41     , m_charset(charset)
       
    42     , m_loadCompleted(false)
       
    43 {
       
    44 }
       
    45 
       
    46 CSSStyleSheet::CSSStyleSheet(Node *parentNode, const String& href, const String& charset)
       
    47     : StyleSheet(parentNode, href)
       
    48     , m_doc(parentNode->document())
       
    49     , m_namespaces(0)
       
    50     , m_charset(charset)
       
    51     , m_loadCompleted(false)
       
    52 {
       
    53 }
       
    54 
       
    55 CSSStyleSheet::CSSStyleSheet(CSSRule *ownerRule, const String& href, const String& charset)
       
    56     : StyleSheet(ownerRule, href)
       
    57     , m_doc(0)
       
    58     , m_namespaces(0)
       
    59     , m_charset(charset)
       
    60     , m_loadCompleted(false)
       
    61 {
       
    62 }
       
    63 
       
    64 CSSStyleSheet::~CSSStyleSheet()
       
    65 {
       
    66     delete m_namespaces;
       
    67 }
       
    68 
       
    69 CSSRule *CSSStyleSheet::ownerRule() const
       
    70 {
       
    71     return (parent() && parent()->isRule()) ? static_cast<CSSRule*>(parent()) : 0;
       
    72 }
       
    73 
       
    74 unsigned CSSStyleSheet::insertRule(const String& rule, unsigned index, ExceptionCode& ec)
       
    75 {
       
    76     ec = 0;
       
    77     if (index > length()) {
       
    78         ec = INDEX_SIZE_ERR;
       
    79         return 0;
       
    80     }
       
    81     CSSParser p(useStrictParsing());
       
    82     RefPtr<CSSRule> r = p.parseRule(this, rule);
       
    83 
       
    84     if (!r) {
       
    85         ec = SYNTAX_ERR;
       
    86         return 0;
       
    87     }
       
    88 
       
    89     // ###
       
    90     // HIERARCHY_REQUEST_ERR: Raised if the rule cannot be inserted at the specified index e.g. if an
       
    91     //@import rule is inserted after a standard rule set or other at-rule.
       
    92     insert(index, r.release());
       
    93     
       
    94     styleSheetChanged();
       
    95     
       
    96     return index;
       
    97 }
       
    98 
       
    99 int CSSStyleSheet::addRule(const String& selector, const String& style, int index, ExceptionCode& ec)
       
   100 {
       
   101     insertRule(selector + " { " + style + " }", index, ec);
       
   102 
       
   103     // As per Microsoft documentation, always return -1.
       
   104     return -1;
       
   105 }
       
   106 
       
   107 int CSSStyleSheet::addRule(const String& selector, const String& style, ExceptionCode& ec)
       
   108 {
       
   109     return addRule(selector, style, length(), ec);
       
   110 }
       
   111 
       
   112 
       
   113 CSSRuleList* CSSStyleSheet::cssRules(bool omitCharsetRules)
       
   114 {
       
   115     return new CSSRuleList(this, omitCharsetRules);
       
   116 }
       
   117 
       
   118 void CSSStyleSheet::deleteRule(unsigned index, ExceptionCode& ec)
       
   119 {
       
   120     if (index >= length()) {
       
   121         ec = INDEX_SIZE_ERR;
       
   122         return;
       
   123     }
       
   124 
       
   125     ec = 0;
       
   126     remove(index);
       
   127     styleSheetChanged();
       
   128 }
       
   129 
       
   130 void CSSStyleSheet::addNamespace(CSSParser* p, const AtomicString& prefix, const AtomicString& uri)
       
   131 {
       
   132     if (uri.isEmpty())
       
   133         return;
       
   134 
       
   135     m_namespaces = new CSSNamespace(prefix, uri, m_namespaces);
       
   136     
       
   137     if (prefix.isEmpty())
       
   138         // Set the default namespace on the parser so that selectors that omit namespace info will
       
   139         // be able to pick it up easily.
       
   140         p->defaultNamespace = uri;
       
   141 }
       
   142 
       
   143 const AtomicString& CSSStyleSheet::determineNamespace(const AtomicString& prefix)
       
   144 {
       
   145     if (prefix.isEmpty())
       
   146         return nullAtom; // No namespace. If an element/attribute has a namespace, we won't match it.
       
   147     else if (prefix == starAtom)
       
   148         return starAtom; // We'll match any namespace.
       
   149     else if (m_namespaces) {
       
   150         CSSNamespace* ns = m_namespaces->namespaceForPrefix(prefix);
       
   151         if (ns)
       
   152             return ns->uri();
       
   153     }
       
   154     return nullAtom; // Assume we wont match any namespaces.
       
   155 }
       
   156 
       
   157 bool CSSStyleSheet::parseString(const String &string, bool strict)
       
   158 {
       
   159 #if PLATFORM(SYMBIAN)	
       
   160     OOM_PRE_CHECK(string.length() * 7, 0, "CSSStyleSheet::parseString" )
       
   161 #endif
       
   162 
       
   163     setStrictParsing(strict);
       
   164     CSSParser p(strict);
       
   165     p.parseSheet(this, string);
       
   166 #if PLATFORM(SYMBIAN)	
       
   167     OOM_POST_CHECK_FAILED(return false;)
       
   168 #endif
       
   169     return true;
       
   170 }
       
   171 
       
   172 bool CSSStyleSheet::isLoading()
       
   173 {
       
   174     unsigned len = length();
       
   175     for (unsigned i = 0; i < len; ++i) {
       
   176         StyleBase* rule = item(i);
       
   177         if (rule->isImportRule() && static_cast<CSSImportRule*>(rule)->isLoading())
       
   178             return true;
       
   179     }
       
   180     return false;
       
   181 }
       
   182 
       
   183 void CSSStyleSheet::checkLoaded()
       
   184 {
       
   185     if (isLoading())
       
   186         return;
       
   187     if (parent())
       
   188         parent()->checkLoaded();
       
   189     m_loadCompleted = m_parentNode ? m_parentNode->sheetLoaded() : true;
       
   190 }
       
   191 
       
   192 DocLoader *CSSStyleSheet::docLoader()
       
   193 {
       
   194     if (!m_doc) // doc is 0 for the user- and default-sheet!
       
   195         return 0;
       
   196 
       
   197     // ### remove? (clients just use sheet->doc()->docLoader())
       
   198     return m_doc->docLoader();
       
   199 }
       
   200 
       
   201 void CSSStyleSheet::styleSheetChanged()
       
   202 {
       
   203     StyleBase* root = this;
       
   204     while (StyleBase* parent = root->parent())
       
   205         root = parent;
       
   206     Document* documentToUpdate = (root && root->isCSSStyleSheet()) ? static_cast<CSSStyleSheet*>(root)->doc() : 0;
       
   207     
       
   208     /* FIXME: We don't need to do everything updateStyleSelector does,
       
   209      * basically we just need to recreate the document's selector with the
       
   210      * already existing style sheets.
       
   211      */
       
   212     if (documentToUpdate)
       
   213         documentToUpdate->updateStyleSelector();
       
   214 }
       
   215 
       
   216 }