2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
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.
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.
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.
21 #ifndef CSSStyleSheet_h
22 #define CSSStyleSheet_h
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"
39 class StyleSheetContents;
41 enum StyleSheetUpdateType {
43 EntireStyleSheetUpdate
46 class CSSStyleSheet final : public StyleSheet {
47 DEFINE_WRAPPERTYPEINFO();
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());
54 virtual ~CSSStyleSheet();
56 virtual CSSStyleSheet* parentStyleSheet() const override;
57 virtual Node* ownerNode() const override { return m_ownerNode; }
58 virtual MediaList* media() const override;
59 virtual String href() const override;
60 virtual String title() const override { return m_title; }
61 virtual bool disabled() const override { return m_isDisabled; }
62 virtual void setDisabled(bool) override;
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&);
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&);
73 void removeRule(unsigned index, ExceptionState& exceptionState) { deleteRule(index, exceptionState); }
76 unsigned length() const;
77 CSSRule* item(unsigned index);
79 virtual void clearOwnerNode() override;
81 virtual CSSRule* ownerRule() const override { return m_ownerRule; }
82 virtual KURL baseURL() const override;
83 virtual bool isLoading() const override;
85 void clearOwnerRule() { m_ownerRule = nullptr; }
86 Document* ownerDocument() const;
87 MediaQuerySet* mediaQueries() const { return m_mediaQueries.get(); }
88 void setMediaQueries(PassRefPtrWillBeRawPtr<MediaQuerySet>);
89 void setTitle(const String& title) { m_title = title; }
91 class RuleMutationScope {
92 WTF_MAKE_NONCOPYABLE(RuleMutationScope);
95 explicit RuleMutationScope(CSSStyleSheet*);
96 explicit RuleMutationScope(CSSRule*);
100 RawPtrWillBeMember<CSSStyleSheet> m_styleSheet;
103 void willMutateRules();
104 void didMutateRules();
105 void didMutate(StyleSheetUpdateType = PartialRuleUpdate);
107 void clearChildRuleCSSOMWrappers();
109 StyleSheetContents* contents() const { return m_contents.get(); }
111 bool isInline() const { return m_isInlineStylesheet; }
112 TextPosition startPositionInSource() const { return m_startPosition; }
115 bool loadCompleted() const { return m_loadCompleted; }
116 void startLoadingDynamicSheet();
118 virtual void trace(Visitor*) override;
121 CSSStyleSheet(PassRefPtrWillBeRawPtr<StyleSheetContents>, CSSImportRule* ownerRule);
122 CSSStyleSheet(PassRefPtrWillBeRawPtr<StyleSheetContents>, Node* ownerNode, bool isInlineStylesheet, const TextPosition& startPosition);
124 virtual bool isCSSStyleSheet() const override { return true; }
125 virtual String type() const override { return "text/css"; }
127 void reattachChildRuleCSSOMWrappers();
129 bool canAccessRules() const;
131 void setLoadCompleted(bool);
133 RefPtrWillBeMember<StyleSheetContents> m_contents;
134 bool m_isInlineStylesheet;
137 RefPtrWillBeMember<MediaQuerySet> m_mediaQueries;
139 RawPtrWillBeMember<Node> m_ownerNode;
140 RawPtrWillBeMember<CSSRule> m_ownerRule;
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;
149 inline CSSStyleSheet::RuleMutationScope::RuleMutationScope(CSSStyleSheet* sheet)
150 : m_styleSheet(sheet)
153 m_styleSheet->willMutateRules();
156 inline CSSStyleSheet::RuleMutationScope::RuleMutationScope(CSSRule* rule)
157 : m_styleSheet(rule ? rule->parentStyleSheet() : 0)
160 m_styleSheet->willMutateRules();
163 inline CSSStyleSheet::RuleMutationScope::~RuleMutationScope()
166 m_styleSheet->didMutateRules();
169 DEFINE_TYPE_CASTS(CSSStyleSheet, StyleSheet, sheet, sheet->isCSSStyleSheet(), sheet.isCSSStyleSheet());