Home | History | Annotate | Download | only in css
      1 /*
      2  * (C) 1999-2003 Lars Knoll (knoll (at) kde.org)
      3  * Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010, 2012 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 #ifndef CSSStyleSheet_h
     22 #define CSSStyleSheet_h
     23 
     24 #include "core/css/CSSRule.h"
     25 #include "core/css/StyleSheet.h"
     26 #include "wtf/Noncopyable.h"
     27 #include "wtf/text/TextPosition.h"
     28 
     29 namespace WebCore {
     30 
     31 class CSSCharsetRule;
     32 class CSSImportRule;
     33 class CSSParser;
     34 class CSSRule;
     35 class CSSRuleList;
     36 class CSSStyleSheet;
     37 class CSSStyleSheetResource;
     38 class Document;
     39 class ExceptionState;
     40 class MediaQuerySet;
     41 class SecurityOrigin;
     42 class StyleSheetContents;
     43 
     44 class CSSStyleSheet : public StyleSheet {
     45 public:
     46     static PassRefPtr<CSSStyleSheet> create(PassRefPtr<StyleSheetContents>, CSSImportRule* ownerRule = 0);
     47     static PassRefPtr<CSSStyleSheet> create(PassRefPtr<StyleSheetContents>, Node* ownerNode);
     48     static PassRefPtr<CSSStyleSheet> createInline(Node*, const KURL&, const TextPosition& startPosition = TextPosition::minimumPosition(), const String& encoding = String());
     49 
     50     virtual ~CSSStyleSheet();
     51 
     52     virtual CSSStyleSheet* parentStyleSheet() const OVERRIDE;
     53     virtual Node* ownerNode() const OVERRIDE { return m_ownerNode; }
     54     virtual MediaList* media() const OVERRIDE;
     55     virtual String href() const OVERRIDE;
     56     virtual String title() const OVERRIDE { return m_title; }
     57     virtual bool disabled() const OVERRIDE { return m_isDisabled; }
     58     virtual void setDisabled(bool) OVERRIDE;
     59 
     60     PassRefPtr<CSSRuleList> cssRules();
     61     unsigned insertRule(const String& rule, unsigned index, ExceptionState&);
     62     void deleteRule(unsigned index, ExceptionState&);
     63 
     64     // IE Extensions
     65     PassRefPtr<CSSRuleList> rules();
     66     int addRule(const String& selector, const String& style, int index, ExceptionState&);
     67     int addRule(const String& selector, const String& style, ExceptionState&);
     68     void removeRule(unsigned index, ExceptionState& es) { deleteRule(index, es); }
     69 
     70     // For CSSRuleList.
     71     unsigned length() const;
     72     CSSRule* item(unsigned index);
     73 
     74     virtual void clearOwnerNode() OVERRIDE { didMutate(); m_ownerNode = 0; }
     75     virtual CSSRule* ownerRule() const OVERRIDE { return m_ownerRule; }
     76     virtual KURL baseURL() const OVERRIDE;
     77     virtual bool isLoading() const OVERRIDE;
     78 
     79     void clearOwnerRule() { m_ownerRule = 0; }
     80     Document* ownerDocument() const;
     81     MediaQuerySet* mediaQueries() const { return m_mediaQueries.get(); }
     82     void setMediaQueries(PassRefPtr<MediaQuerySet>);
     83     void setTitle(const String& title) { m_title = title; }
     84 
     85     class RuleMutationScope {
     86         WTF_MAKE_NONCOPYABLE(RuleMutationScope);
     87     public:
     88         RuleMutationScope(CSSStyleSheet*);
     89         RuleMutationScope(CSSRule*);
     90         ~RuleMutationScope();
     91 
     92     private:
     93         CSSStyleSheet* m_styleSheet;
     94     };
     95 
     96     void willMutateRules();
     97     void didMutateRules();
     98     void didMutate();
     99 
    100     void clearChildRuleCSSOMWrappers();
    101     void reattachChildRuleCSSOMWrappers();
    102 
    103     StyleSheetContents* contents() const { return m_contents.get(); }
    104 
    105     bool isInline() const { return m_isInlineStylesheet; }
    106     TextPosition startPositionInSource() const { return m_startPosition; }
    107 
    108 private:
    109     CSSStyleSheet(PassRefPtr<StyleSheetContents>, CSSImportRule* ownerRule);
    110     CSSStyleSheet(PassRefPtr<StyleSheetContents>, Node* ownerNode, bool isInlineStylesheet, const TextPosition& startPosition);
    111 
    112     virtual bool isCSSStyleSheet() const { return true; }
    113     virtual String type() const { return "text/css"; }
    114 
    115     bool canAccessRules() const;
    116 
    117     RefPtr<StyleSheetContents> m_contents;
    118     bool m_isInlineStylesheet;
    119     bool m_isDisabled;
    120     String m_title;
    121     RefPtr<MediaQuerySet> m_mediaQueries;
    122 
    123     Node* m_ownerNode;
    124     CSSRule* m_ownerRule;
    125 
    126     TextPosition m_startPosition;
    127 
    128     mutable RefPtr<MediaList> m_mediaCSSOMWrapper;
    129     mutable Vector<RefPtr<CSSRule> > m_childRuleCSSOMWrappers;
    130     mutable OwnPtr<CSSRuleList> m_ruleListCSSOMWrapper;
    131 };
    132 
    133 inline CSSStyleSheet::RuleMutationScope::RuleMutationScope(CSSStyleSheet* sheet)
    134     : m_styleSheet(sheet)
    135 {
    136     if (m_styleSheet)
    137         m_styleSheet->willMutateRules();
    138 }
    139 
    140 inline CSSStyleSheet::RuleMutationScope::RuleMutationScope(CSSRule* rule)
    141     : m_styleSheet(rule ? rule->parentStyleSheet() : 0)
    142 {
    143     if (m_styleSheet)
    144         m_styleSheet->willMutateRules();
    145 }
    146 
    147 inline CSSStyleSheet::RuleMutationScope::~RuleMutationScope()
    148 {
    149     if (m_styleSheet)
    150         m_styleSheet->didMutateRules();
    151 }
    152 
    153 } // namespace
    154 
    155 #endif
    156