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