Home | History | Annotate | Download | only in css
      1 /*
      2  * (C) 1999-2003 Lars Knoll (knoll (at) 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 StyleSheetContents_h
     22 #define StyleSheetContents_h
     23 
     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"
     34 
     35 
     36 namespace WebCore {
     37 
     38 class CSSStyleSheet;
     39 class CSSStyleSheetResource;
     40 class Document;
     41 class Node;
     42 class SecurityOrigin;
     43 class StyleRuleBase;
     44 class StyleRuleImport;
     45 
     46 class StyleSheetContents : public RefCounted<StyleSheetContents> {
     47 public:
     48     static PassRefPtr<StyleSheetContents> create(const CSSParserContext& context = CSSParserContext(HTMLStandardMode))
     49     {
     50         return adoptRef(new StyleSheetContents(0, String(), context));
     51     }
     52     static PassRefPtr<StyleSheetContents> create(const String& originalURL, const CSSParserContext& context)
     53     {
     54         return adoptRef(new StyleSheetContents(0, originalURL, context));
     55     }
     56     static PassRefPtr<StyleSheetContents> create(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext& context)
     57     {
     58         return adoptRef(new StyleSheetContents(ownerRule, originalURL, context));
     59     }
     60 
     61     ~StyleSheetContents();
     62 
     63     const CSSParserContext& parserContext() const { return m_parserContext; }
     64 
     65     const AtomicString& determineNamespace(const AtomicString& prefix);
     66 
     67     void parseAuthorStyleSheet(const CSSStyleSheetResource*, const SecurityOrigin*);
     68     bool parseString(const String&);
     69     bool parseStringAtPosition(const String&, const TextPosition&, bool);
     70 
     71     bool isCacheable() const;
     72 
     73     bool isLoading() const;
     74 
     75     void checkLoaded();
     76     void startLoadingDynamicSheet();
     77 
     78     StyleSheetContents* rootStyleSheet() const;
     79     bool hasSingleOwnerNode() const;
     80     Node* singleOwnerNode() const;
     81     Document* singleOwnerDocument() const;
     82 
     83     const String& charset() const { return m_parserContext.charset(); }
     84 
     85     bool loadCompleted() const { return m_loadCompleted; }
     86     bool hasFailedOrCanceledSubresources() const;
     87 
     88     KURL completeURL(const String& url) const;
     89     void addSubresourceStyleURLs(ListHashSet<KURL>&);
     90 
     91     void setHasSyntacticallyValidCSSHeader(bool b) { m_hasSyntacticallyValidCSSHeader = b; }
     92     bool hasSyntacticallyValidCSSHeader() const { return m_hasSyntacticallyValidCSSHeader; }
     93 
     94     void setHasFontFaceRule(bool b) { m_hasFontFaceRule = b; }
     95     bool hasFontFaceRule() const { return m_hasFontFaceRule; }
     96 
     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; }
    101 
    102     void clearRules();
    103 
    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; }
    109 
    110     void notifyLoadedSheet(const CSSStyleSheetResource*);
    111 
    112     StyleSheetContents* parentStyleSheet() const;
    113     StyleRuleImport* ownerRule() const { return m_ownerRule; }
    114     void clearOwnerRule() { m_ownerRule = 0; }
    115 
    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(); }
    121 
    122     unsigned ruleCount() const;
    123     StyleRuleBase* ruleAt(unsigned index) const;
    124 
    125     bool usesRemUnits() const { return m_usesRemUnits; }
    126 
    127     unsigned estimatedSizeInBytes() const;
    128 
    129     bool wrapperInsertRule(PassRefPtr<StyleRuleBase>, unsigned index);
    130     void wrapperDeleteRule(unsigned index);
    131 
    132     PassRefPtr<StyleSheetContents> copy() const { return adoptRef(new StyleSheetContents(*this)); }
    133 
    134     void registerClient(CSSStyleSheet*);
    135     void unregisterClient(CSSStyleSheet*);
    136     bool hasOneClient() { return m_clients.size() == 1; }
    137 
    138     bool isMutable() const { return m_isMutable; }
    139     void setMutable() { m_isMutable = true; }
    140 
    141     bool isInMemoryCache() const { return m_isInMemoryCache; }
    142     void addedToMemoryCache();
    143     void removedFromMemoryCache();
    144 
    145     void setHasMediaQueries();
    146     bool hasMediaQueries() { return m_hasMediaQueries; }
    147 
    148     void shrinkToFit();
    149     RuleSet& ruleSet() { ASSERT(m_ruleSet); return *m_ruleSet.get(); }
    150     RuleSet& ensureRuleSet(const MediaQueryEvaluator&, AddRuleFlags);
    151     void clearRuleSet();
    152 
    153 private:
    154     StyleSheetContents(StyleRuleImport* ownerRule, const String& originalURL, const CSSParserContext&);
    155     StyleSheetContents(const StyleSheetContents&);
    156 
    157     void clearCharsetRule();
    158 
    159     StyleRuleImport* m_ownerRule;
    160 
    161     String m_originalURL;
    162 
    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;
    168 
    169     bool m_loadCompleted : 1;
    170     bool m_hasSyntacticallyValidCSSHeader : 1;
    171     bool m_didLoadErrorOccur : 1;
    172     bool m_usesRemUnits : 1;
    173     bool m_isMutable : 1;
    174     bool m_isInMemoryCache : 1;
    175     bool m_hasFontFaceRule : 1;
    176     bool m_hasMediaQueries : 1;
    177 
    178     CSSParserContext m_parserContext;
    179 
    180     Vector<CSSStyleSheet*> m_clients;
    181     OwnPtr<RuleSet> m_ruleSet;
    182 };
    183 
    184 } // namespace
    185 
    186 #endif
    187