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 "weborigin/KURL.h"
26 #include "wtf/HashMap.h"
27 #include "wtf/ListHashSet.h"
28 #include "wtf/RefCounted.h"
29 #include "wtf/Vector.h"
30 #include "wtf/text/AtomicStringHash.h"
31 #include "wtf/text/TextPosition.h"
37 class CSSStyleSheetResource;
42 class StyleRuleImport;
44 class StyleSheetContents : public RefCounted<StyleSheetContents> {
46 static PassRefPtr<StyleSheetContents> create(const CSSParserContext& context = CSSParserContext(HTMLStandardMode))
48 return adoptRef(new StyleSheetContents(0, String(), context));
50 static PassRefPtr<StyleSheetContents> create(const String& originalURL, const CSSParserContext& context)
52 return adoptRef(new StyleSheetContents(0, originalURL, context));
54 static PassRefPtr<StyleSheetContents> create(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext& context)
56 return adoptRef(new StyleSheetContents(ownerRule, originalURL, context));
59 ~StyleSheetContents();
61 const CSSParserContext& parserContext() const { return m_parserContext; }
63 const AtomicString& determineNamespace(const AtomicString& prefix);
65 void parseAuthorStyleSheet(const CSSStyleSheetResource*, const SecurityOrigin*);
66 bool parseString(const String&);
67 bool parseStringAtPosition(const String&, const TextPosition&, bool);
69 bool isCacheable() const;
71 bool isLoading() const;
74 void startLoadingDynamicSheet();
76 StyleSheetContents* rootStyleSheet() const;
77 bool hasSingleOwnerNode() const;
78 Node* singleOwnerNode() const;
79 Document* singleOwnerDocument() const;
81 const String& charset() const { return m_parserContext.charset; }
83 bool loadCompleted() const { return m_loadCompleted; }
84 bool hasFailedOrCanceledSubresources() const;
86 KURL completeURL(const String& url) const;
87 void addSubresourceStyleURLs(ListHashSet<KURL>&);
89 void setIsUserStyleSheet(bool b) { m_isUserStyleSheet = b; }
90 bool isUserStyleSheet() const { return m_isUserStyleSheet; }
91 void setHasSyntacticallyValidCSSHeader(bool b) { m_hasSyntacticallyValidCSSHeader = b; }
92 bool hasSyntacticallyValidCSSHeader() const { return m_hasSyntacticallyValidCSSHeader; }
94 void parserAddNamespace(const AtomicString& prefix, const AtomicString& uri);
95 void parserAppendRule(PassRefPtr<StyleRuleBase>);
96 void parserSetEncodingFromCharsetRule(const String& encoding);
97 void parserSetUsesRemUnits(bool b) { m_usesRemUnits = b; }
101 bool hasCharsetRule() const { return !m_encodingFromCharsetRule.isNull(); }
102 String encodingFromCharsetRule() const { return m_encodingFromCharsetRule; }
103 // Rules other than @charset and @import.
104 const Vector<RefPtr<StyleRuleBase> >& childRules() const { return m_childRules; }
105 const Vector<RefPtr<StyleRuleImport> >& importRules() const { return m_importRules; }
107 void notifyLoadedSheet(const CSSStyleSheetResource*);
109 StyleSheetContents* parentStyleSheet() const;
110 StyleRuleImport* ownerRule() const { return m_ownerRule; }
111 void clearOwnerRule() { m_ownerRule = 0; }
113 // Note that href is the URL that started the redirect chain that led to
114 // this style sheet. This property probably isn't useful for much except
115 // the JavaScript binding (which needs to use this value for security).
116 String originalURL() const { return m_originalURL; }
117 const KURL& baseURL() const { return m_parserContext.baseURL; }
119 unsigned ruleCount() const;
120 StyleRuleBase* ruleAt(unsigned index) const;
122 bool usesRemUnits() const { return m_usesRemUnits; }
124 unsigned estimatedSizeInBytes() const;
126 bool wrapperInsertRule(PassRefPtr<StyleRuleBase>, unsigned index);
127 void wrapperDeleteRule(unsigned index);
129 PassRefPtr<StyleSheetContents> copy() const { return adoptRef(new StyleSheetContents(*this)); }
131 void registerClient(CSSStyleSheet*);
132 void unregisterClient(CSSStyleSheet*);
133 bool hasOneClient() { return m_clients.size() == 1; }
135 bool isMutable() const { return m_isMutable; }
136 void setMutable() { m_isMutable = true; }
138 bool isInMemoryCache() const { return m_isInMemoryCache; }
139 void addedToMemoryCache();
140 void removedFromMemoryCache();
145 StyleSheetContents(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext&);
146 StyleSheetContents(const StyleSheetContents&);
148 void clearCharsetRule();
150 StyleRuleImport* m_ownerRule;
152 String m_originalURL;
154 String m_encodingFromCharsetRule;
155 Vector<RefPtr<StyleRuleImport> > m_importRules;
156 Vector<RefPtr<StyleRuleBase> > m_childRules;
157 typedef HashMap<AtomicString, AtomicString> PrefixNamespaceURIMap;
158 PrefixNamespaceURIMap m_namespaces;
160 bool m_loadCompleted : 1;
161 bool m_isUserStyleSheet : 1;
162 bool m_hasSyntacticallyValidCSSHeader : 1;
163 bool m_didLoadErrorOccur : 1;
164 bool m_usesRemUnits : 1;
165 bool m_isMutable : 1;
166 bool m_isInMemoryCache : 1;
168 CSSParserContext m_parserContext;
170 Vector<CSSStyleSheet*> m_clients;