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 enum StyleSheetUpdateType {
     45     PartialRuleUpdate,
     46     EntireStyleSheetUpdate
     47 };
     48 
     49 class CSSStyleSheet : public StyleSheet {
     50 public:
     51     static PassRefPtr<CSSStyleSheet> create(PassRefPtr<StyleSheetContents>, CSSImportRule* ownerRule = 0);
     52     static PassRefPtr<CSSStyleSheet> create(PassRefPtr<StyleSheetContents>, Node* ownerNode);
     53     static PassRefPtr<CSSStyleSheet> createInline(Node*, const KURL&, const TextPosition& startPosition = TextPosition::minimumPosition(), const String& encoding = String());
     54 
     55     virtual ~CSSStyleSheet();
     56 
     57     virtual CSSStyleSheet* parentStyleSheet() const OVERRIDE;
     58     virtual Node* ownerNode() const OVERRIDE { return m_ownerNode; }
     59     virtual MediaList* media() const OVERRIDE;
     60     virtual String href() const OVERRIDE;
     61     virtual String title() const OVERRIDE { return m_title; }
     62     virtual bool disabled() const OVERRIDE { return m_isDisabled; }
     63     virtual void setDisabled(bool) OVERRIDE;
     64 
     65     PassRefPtr<CSSRuleList> cssRules();
     66     unsigned insertRule(const String& rule, unsigned index, ExceptionState&);
     67     unsigned insertRule(const String& rule, ExceptionState&); // Deprecated.
     68     void deleteRule(unsigned index, ExceptionState&);
     69 
     70     // IE Extensions
     71     PassRefPtr<CSSRuleList> rules();
     72     int addRule(const String& selector, const String& style, int index, ExceptionState&);
     73     int addRule(const String& selector, const String& style, ExceptionState&);
     74     void removeRule(unsigned index, ExceptionState& exceptionState) { deleteRule(index, exceptionState); }
     75 
     76     // For CSSRuleList.
     77     unsigned length() const;
     78     CSSRule* item(unsigned index);
     79 
     80     virtual void clearOwnerNode() OVERRIDE { didMutate(EntireStyleSheetUpdate); m_ownerNode = 0; }
     81     virtual CSSRule* ownerRule() const OVERRIDE { return m_ownerRule; }
     82     virtual KURL baseURL() const OVERRIDE;
     83     virtual bool isLoading() const OVERRIDE;
     84 
     85     void clearOwnerRule() { m_ownerRule = 0; }
     86     Document* ownerDocument() const;
     87     MediaQuerySet* mediaQueries() const { return m_mediaQueries.get(); }
     88     void setMediaQueries(PassRefPtr<MediaQuerySet>);
     89     void setTitle(const String& title) { m_title = title; }
     90 
     91     class RuleMutationScope {
     92         WTF_MAKE_NONCOPYABLE(RuleMutationScope);
     93     public:
     94         RuleMutationScope(CSSStyleSheet*);
     95         RuleMutationScope(CSSRule*);
     96         ~RuleMutationScope();
     97 
     98     private:
     99         CSSStyleSheet* m_styleSheet;
    100     };
    101 
    102     void willMutateRules();
    103     void didMutateRules();
    104     void didMutate(StyleSheetUpdateType = PartialRuleUpdate);
    105 
    106     void clearChildRuleCSSOMWrappers();
    107 
    108     void registerExtraChildRuleCSSOMWrapper(PassRefPtr<CSSRule>);
    109 
    110     StyleSheetContents* contents() const { return m_contents.get(); }
    111 
    112     bool isInline() const { return m_isInlineStylesheet; }
    113     TextPosition startPositionInSource() const { return m_startPosition; }
    114 
    115 private:
    116     CSSStyleSheet(PassRefPtr<StyleSheetContents>, CSSImportRule* ownerRule);
    117     CSSStyleSheet(PassRefPtr<StyleSheetContents>, Node* ownerNode, bool isInlineStylesheet, const TextPosition& startPosition);
    118 
    119     virtual bool isCSSStyleSheet() const { return true; }
    120     virtual String type() const { return "text/css"; }
    121 
    122     void extraCSSOMWrapperIndices(Vector<unsigned>& indices);
    123     void reattachChildRuleCSSOMWrappers(const Vector<unsigned>& extraCSSOMWrapperIndices);
    124 
    125     bool canAccessRules() const;
    126 
    127     RefPtr<StyleSheetContents> m_contents;
    128     bool m_isInlineStylesheet;
    129     bool m_isDisabled;
    130     String m_title;
    131     RefPtr<MediaQuerySet> m_mediaQueries;
    132 
    133     Node* m_ownerNode;
    134     CSSRule* m_ownerRule;
    135 
    136     TextPosition m_startPosition;
    137 
    138     mutable RefPtr<MediaList> m_mediaCSSOMWrapper;
    139     mutable Vector<RefPtr<CSSRule> > m_childRuleCSSOMWrappers;
    140     // These are CSSOMWrappers that come from getMatchedCSSRules and thus don't map 1-1 to
    141     // the StyleRules in the StyleSheetContents.
    142     mutable Vector<RefPtr<CSSRule> > m_extraChildRuleCSSOMWrappers;
    143     mutable OwnPtr<CSSRuleList> m_ruleListCSSOMWrapper;
    144 };
    145 
    146 inline CSSStyleSheet::RuleMutationScope::RuleMutationScope(CSSStyleSheet* sheet)
    147     : m_styleSheet(sheet)
    148 {
    149     if (m_styleSheet)
    150         m_styleSheet->willMutateRules();
    151 }
    152 
    153 inline CSSStyleSheet::RuleMutationScope::RuleMutationScope(CSSRule* rule)
    154     : m_styleSheet(rule ? rule->parentStyleSheet() : 0)
    155 {
    156     if (m_styleSheet)
    157         m_styleSheet->willMutateRules();
    158 }
    159 
    160 inline CSSStyleSheet::RuleMutationScope::~RuleMutationScope()
    161 {
    162     if (m_styleSheet)
    163         m_styleSheet->didMutateRules();
    164 }
    165 
    166 DEFINE_TYPE_CASTS(CSSStyleSheet, StyleSheet, sheet, sheet->isCSSStyleSheet(), sheet.isCSSStyleSheet());
    167 
    168 } // namespace
    169 
    170 #endif
    171