1 /*
2 * (C) 1999-2003 Lars Knoll (knoll@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 "platform/heap/Handle.h"
27 #include "wtf/Noncopyable.h"
28 #include "wtf/text/TextPosition.h"
29
30 namespace blink {
31
32 class CSSImportRule;
33 class CSSRule;
34 class CSSRuleList;
35 class CSSStyleSheet;
36 class Document;
37 class ExceptionState;
38 class MediaQuerySet;
39 class StyleSheetContents;
40
41 enum StyleSheetUpdateType {
42 PartialRuleUpdate,
43 EntireStyleSheetUpdate
44 };
45
46 class CSSStyleSheet FINAL : public StyleSheet {
47 DEFINE_WRAPPERTYPEINFO();
48 public:
49 static PassRefPtrWillBeRawPtr<CSSStyleSheet> create(PassRefPtrWillBeRawPtr<StyleSheetContents>, CSSImportRule* ownerRule = 0);
50 static PassRefPtrWillBeRawPtr<CSSStyleSheet> create(PassRefPtrWillBeRawPtr<StyleSheetContents>, Node* ownerNode);
51 static PassRefPtrWillBeRawPtr<CSSStyleSheet> createInline(Node*, const KURL&, const TextPosition& startPosition = TextPosition::minimumPosition(), const String& encoding = String());
52 static PassRefPtrWillBeRawPtr<CSSStyleSheet> createInline(PassRefPtrWillBeRawPtr<StyleSheetContents>, Node* ownerNode, const TextPosition& startPosition = TextPosition::minimumPosition());
53
54 virtual ~CSSStyleSheet();
55
56 virtual CSSStyleSheet* parentStyleSheet() const OVERRIDE;
ownerNode()57 virtual Node* ownerNode() const OVERRIDE { return m_ownerNode; }
58 virtual MediaList* media() const OVERRIDE;
59 virtual String href() const OVERRIDE;
title()60 virtual String title() const OVERRIDE { return m_title; }
disabled()61 virtual bool disabled() const OVERRIDE { return m_isDisabled; }
62 virtual void setDisabled(bool) OVERRIDE;
63
64 PassRefPtrWillBeRawPtr<CSSRuleList> cssRules();
65 unsigned insertRule(const String& rule, unsigned index, ExceptionState&);
66 unsigned insertRule(const String& rule, ExceptionState&); // Deprecated.
67 void deleteRule(unsigned index, ExceptionState&);
68
69 // IE Extensions
70 PassRefPtrWillBeRawPtr<CSSRuleList> rules();
71 int addRule(const String& selector, const String& style, int index, ExceptionState&);
72 int addRule(const String& selector, const String& style, ExceptionState&);
removeRule(unsigned index,ExceptionState & exceptionState)73 void removeRule(unsigned index, ExceptionState& exceptionState) { deleteRule(index, exceptionState); }
74
75 // For CSSRuleList.
76 unsigned length() const;
77 CSSRule* item(unsigned index);
78
79 virtual void clearOwnerNode() OVERRIDE;
80
ownerRule()81 virtual CSSRule* ownerRule() const OVERRIDE { return m_ownerRule; }
82 virtual KURL baseURL() const OVERRIDE;
83 virtual bool isLoading() const OVERRIDE;
84
clearOwnerRule()85 void clearOwnerRule() { m_ownerRule = nullptr; }
86 Document* ownerDocument() const;
mediaQueries()87 MediaQuerySet* mediaQueries() const { return m_mediaQueries.get(); }
88 void setMediaQueries(PassRefPtrWillBeRawPtr<MediaQuerySet>);
setTitle(const String & title)89 void setTitle(const String& title) { m_title = title; }
90
91 class RuleMutationScope {
92 WTF_MAKE_NONCOPYABLE(RuleMutationScope);
93 STACK_ALLOCATED();
94 public:
95 explicit RuleMutationScope(CSSStyleSheet*);
96 explicit RuleMutationScope(CSSRule*);
97 ~RuleMutationScope();
98
99 private:
100 RawPtrWillBeMember<CSSStyleSheet> m_styleSheet;
101 };
102
103 void willMutateRules();
104 void didMutateRules();
105 void didMutate(StyleSheetUpdateType = PartialRuleUpdate);
106
107 void clearChildRuleCSSOMWrappers();
108
contents()109 StyleSheetContents* contents() const { return m_contents.get(); }
110
isInline()111 bool isInline() const { return m_isInlineStylesheet; }
startPositionInSource()112 TextPosition startPositionInSource() const { return m_startPosition; }
113
114 bool sheetLoaded();
loadCompleted()115 bool loadCompleted() const { return m_loadCompleted; }
116 void startLoadingDynamicSheet();
117
118 virtual void trace(Visitor*) OVERRIDE;
119
120 private:
121 CSSStyleSheet(PassRefPtrWillBeRawPtr<StyleSheetContents>, CSSImportRule* ownerRule);
122 CSSStyleSheet(PassRefPtrWillBeRawPtr<StyleSheetContents>, Node* ownerNode, bool isInlineStylesheet, const TextPosition& startPosition);
123
isCSSStyleSheet()124 virtual bool isCSSStyleSheet() const OVERRIDE { return true; }
type()125 virtual String type() const OVERRIDE { return "text/css"; }
126
127 void reattachChildRuleCSSOMWrappers();
128
129 bool canAccessRules() const;
130
131 void setLoadCompleted(bool);
132
133 RefPtrWillBeMember<StyleSheetContents> m_contents;
134 bool m_isInlineStylesheet;
135 bool m_isDisabled;
136 String m_title;
137 RefPtrWillBeMember<MediaQuerySet> m_mediaQueries;
138
139 RawPtrWillBeMember<Node> m_ownerNode;
140 RawPtrWillBeMember<CSSRule> m_ownerRule;
141
142 TextPosition m_startPosition;
143 bool m_loadCompleted;
144 mutable RefPtrWillBeMember<MediaList> m_mediaCSSOMWrapper;
145 mutable WillBeHeapVector<RefPtrWillBeMember<CSSRule> > m_childRuleCSSOMWrappers;
146 mutable OwnPtrWillBeMember<CSSRuleList> m_ruleListCSSOMWrapper;
147 };
148
RuleMutationScope(CSSStyleSheet * sheet)149 inline CSSStyleSheet::RuleMutationScope::RuleMutationScope(CSSStyleSheet* sheet)
150 : m_styleSheet(sheet)
151 {
152 if (m_styleSheet)
153 m_styleSheet->willMutateRules();
154 }
155
RuleMutationScope(CSSRule * rule)156 inline CSSStyleSheet::RuleMutationScope::RuleMutationScope(CSSRule* rule)
157 : m_styleSheet(rule ? rule->parentStyleSheet() : 0)
158 {
159 if (m_styleSheet)
160 m_styleSheet->willMutateRules();
161 }
162
~RuleMutationScope()163 inline CSSStyleSheet::RuleMutationScope::~RuleMutationScope()
164 {
165 if (m_styleSheet)
166 m_styleSheet->didMutateRules();
167 }
168
169 DEFINE_TYPE_CASTS(CSSStyleSheet, StyleSheet, sheet, sheet->isCSSStyleSheet(), sheet.isCSSStyleSheet());
170
171 } // namespace blink
172
173 #endif
174