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 StyleSheetContents_h
22 #define StyleSheetContents_h
24 #include "core/css/CSSParserMode.h"
25 #include "core/css/RuleSet.h"
26 #include "platform/weborigin/KURL.h"
27 #include "wtf/HashMap.h"
28 #include "wtf/ListHashSet.h"
29 #include "wtf/RefCounted.h"
30 #include "wtf/Vector.h"
31 #include "wtf/text/AtomicStringHash.h"
32 #include "wtf/text/StringHash.h"
33 #include "wtf/text/TextPosition.h"
39 class CSSStyleSheetResource;
44 class StyleRuleImport;
46 class StyleSheetContents : public RefCounted<StyleSheetContents> {
48 static PassRefPtr<StyleSheetContents> create(const CSSParserContext& context = CSSParserContext(HTMLStandardMode))
50 return adoptRef(new StyleSheetContents(0, String(), context));
52 static PassRefPtr<StyleSheetContents> create(const String& originalURL, const CSSParserContext& context)
54 return adoptRef(new StyleSheetContents(0, originalURL, context));
56 static PassRefPtr<StyleSheetContents> create(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext& context)
58 return adoptRef(new StyleSheetContents(ownerRule, originalURL, context));
61 ~StyleSheetContents();
63 const CSSParserContext& parserContext() const { return m_parserContext; }
65 const AtomicString& determineNamespace(const AtomicString& prefix);
67 void parseAuthorStyleSheet(const CSSStyleSheetResource*, const SecurityOrigin*);
68 bool parseString(const String&);
69 bool parseStringAtPosition(const String&, const TextPosition&, bool);
71 bool isCacheable() const;
72 bool maybeCacheable() const;
74 bool isLoading() const;
77 void startLoadingDynamicSheet();
79 StyleSheetContents* rootStyleSheet() const;
80 bool hasSingleOwnerNode() const;
81 Node* singleOwnerNode() const;
82 Document* singleOwnerDocument() const;
84 const String& charset() const { return m_parserContext.charset(); }
86 bool loadCompleted() const;
87 bool hasFailedOrCanceledSubresources() const;
89 KURL completeURL(const String& url) const;
91 void setHasSyntacticallyValidCSSHeader(bool isValidCss);
92 bool hasSyntacticallyValidCSSHeader() const { return m_hasSyntacticallyValidCSSHeader; }
94 void setHasFontFaceRule(bool b) { m_hasFontFaceRule = b; }
95 bool hasFontFaceRule() const { return m_hasFontFaceRule; }
97 void parserAddNamespace(const AtomicString& prefix, const AtomicString& uri);
98 void parserAppendRule(PassRefPtr<StyleRuleBase>);
99 void parserSetEncodingFromCharsetRule(const String& encoding);
100 void parserSetUsesRemUnits(bool b) { m_usesRemUnits = b; }
104 bool hasCharsetRule() const { return !m_encodingFromCharsetRule.isNull(); }
105 String encodingFromCharsetRule() const { return m_encodingFromCharsetRule; }
106 // Rules other than @charset and @import.
107 const Vector<RefPtr<StyleRuleBase> >& childRules() const { return m_childRules; }
108 const Vector<RefPtr<StyleRuleImport> >& importRules() const { return m_importRules; }
110 void notifyLoadedSheet(const CSSStyleSheetResource*);
112 StyleSheetContents* parentStyleSheet() const;
113 StyleRuleImport* ownerRule() const { return m_ownerRule; }
114 void clearOwnerRule() { m_ownerRule = 0; }
116 // Note that href is the URL that started the redirect chain that led to
117 // this style sheet. This property probably isn't useful for much except
118 // the JavaScript binding (which needs to use this value for security).
119 String originalURL() const { return m_originalURL; }
120 const KURL& baseURL() const { return m_parserContext.baseURL(); }
122 unsigned ruleCount() const;
123 StyleRuleBase* ruleAt(unsigned index) const;
125 bool usesRemUnits() const { return m_usesRemUnits; }
127 unsigned estimatedSizeInBytes() const;
129 bool wrapperInsertRule(PassRefPtr<StyleRuleBase>, unsigned index);
130 void wrapperDeleteRule(unsigned index);
132 PassRefPtr<StyleSheetContents> copy() const { return adoptRef(new StyleSheetContents(*this)); }
134 void registerClient(CSSStyleSheet*);
135 void unregisterClient(CSSStyleSheet*);
136 bool hasOneClient() { return m_clients.size() == 1; }
138 bool isMutable() const { return m_isMutable; }
139 void setMutable() { m_isMutable = true; }
141 bool isInMemoryCache() const { return m_isInMemoryCache; }
142 void addedToMemoryCache();
143 void removedFromMemoryCache();
145 void setHasMediaQueries();
146 bool hasMediaQueries() { return m_hasMediaQueries; }
149 RuleSet& ruleSet() { ASSERT(m_ruleSet); return *m_ruleSet.get(); }
150 RuleSet& ensureRuleSet(const MediaQueryEvaluator&, AddRuleFlags);
154 StyleSheetContents(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext&);
155 StyleSheetContents(const StyleSheetContents&);
157 void clearCharsetRule();
159 StyleRuleImport* m_ownerRule;
161 String m_originalURL;
163 String m_encodingFromCharsetRule;
164 Vector<RefPtr<StyleRuleImport> > m_importRules;
165 Vector<RefPtr<StyleRuleBase> > m_childRules;
166 typedef HashMap<AtomicString, AtomicString> PrefixNamespaceURIMap;
167 PrefixNamespaceURIMap m_namespaces;
169 bool m_hasSyntacticallyValidCSSHeader : 1;
170 bool m_didLoadErrorOccur : 1;
171 bool m_usesRemUnits : 1;
172 bool m_isMutable : 1;
173 bool m_isInMemoryCache : 1;
174 bool m_hasFontFaceRule : 1;
175 bool m_hasMediaQueries : 1;
177 CSSParserContext m_parserContext;
179 Vector<CSSStyleSheet*> m_clients;
180 OwnPtr<RuleSet> m_ruleSet;