Home | History | Annotate | Download | only in css
      1 /*
      2  * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
      3  * Copyright (C) 2012 Apple Inc. All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *
      9  * 1. Redistributions of source code must retain the above
     10  *    copyright notice, this list of conditions and the following
     11  *    disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above
     13  *    copyright notice, this list of conditions and the following
     14  *    disclaimer in the documentation and/or other materials
     15  *    provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AS IS AND ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
     21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
     26  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
     27  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #ifndef CSSParserMode_h
     32 #define CSSParserMode_h
     33 
     34 #include "platform/weborigin/KURL.h"
     35 #include "platform/weborigin/Referrer.h"
     36 
     37 namespace WebCore {
     38 
     39 class Document;
     40 
     41 // Must not grow beyond 3 bits, due to packing in StylePropertySet.
     42 enum CSSParserMode {
     43     HTMLStandardMode,
     44     HTMLQuirksMode,
     45     // HTML attributes are parsed in quirks mode but also allows internal properties and values.
     46     HTMLAttributeMode,
     47     // SVG attributes are parsed in quirks mode but rules differ slightly.
     48     SVGAttributeMode,
     49     // @viewport rules are parsed in standards mode but CSSOM modifications (via StylePropertySet)
     50     // must call parseViewportProperties so needs a special mode.
     51     CSSViewportRuleMode,
     52     // User agent stylesheets are parsed in standards mode but also allows internal properties and values.
     53     UASheetMode
     54 };
     55 
     56 inline bool isQuirksModeBehavior(CSSParserMode mode)
     57 {
     58     return mode == HTMLQuirksMode; // || mode == HTMLAttributeMode;
     59 }
     60 
     61 inline bool isUASheetBehavior(CSSParserMode mode)
     62 {
     63     return mode == UASheetMode;
     64 }
     65 
     66 inline bool isInternalPropertyAndValueParsingEnabledForMode(CSSParserMode mode)
     67 {
     68     return mode == HTMLAttributeMode || mode == UASheetMode;
     69 }
     70 
     71 inline bool isUnitLessLengthParsingEnabledForMode(CSSParserMode mode)
     72 {
     73     return mode == HTMLQuirksMode || mode == HTMLAttributeMode || mode == SVGAttributeMode;
     74 }
     75 
     76 inline bool isCSSViewportParsingEnabledForMode(CSSParserMode mode)
     77 {
     78     return mode == CSSViewportRuleMode;
     79 }
     80 
     81 inline bool isSVGNumberParsingEnabledForMode(CSSParserMode mode)
     82 {
     83     return mode == SVGAttributeMode;
     84 }
     85 
     86 inline bool isUseCounterEnabledForMode(CSSParserMode mode)
     87 {
     88     // We don't count the UA style sheet in our statistics.
     89     return mode != UASheetMode;
     90 }
     91 
     92 class UseCounter;
     93 
     94 class CSSParserContext {
     95     WTF_MAKE_FAST_ALLOCATED;
     96 public:
     97     CSSParserContext(CSSParserMode, UseCounter*);
     98     // FIXME: We shouldn't need the UseCounter argument as we could infer it from the Document
     99     // but some callers want to disable use counting (e.g. the WebInspector).
    100     CSSParserContext(const Document&, UseCounter*, const KURL& baseURL = KURL(), const String& charset = emptyString());
    101     // FIXME: This constructor shouldn't exist if we properly piped the UseCounter through the CSS
    102     // subsystem. Currently the UseCounter life time is too crazy and we need a way to override it.
    103     CSSParserContext(const CSSParserContext&, UseCounter*);
    104 
    105     bool operator==(const CSSParserContext&) const;
    106     bool operator!=(const CSSParserContext& other) const { return !(*this == other); }
    107 
    108     CSSParserMode mode() const { return m_mode; }
    109     const KURL& baseURL() const { return m_baseURL; }
    110     const String& charset() const { return m_charset; }
    111     const Referrer& referrer() const { return m_referrer; }
    112     bool isHTMLDocument() const { return m_isHTMLDocument; }
    113 
    114     // This quirk is to maintain compatibility with Android apps built on
    115     // the Android SDK prior to and including version 18. Presumably, this
    116     // can be removed any time after 2015. See http://crbug.com/277157.
    117     bool useLegacyBackgroundSizeShorthandBehavior() const { return m_useLegacyBackgroundSizeShorthandBehavior; }
    118 
    119     // FIXME: These setters shouldn't exist, however the current lifetime of CSSParserContext
    120     // is not well understood and thus we sometimes need to override these fields.
    121     void setMode(CSSParserMode mode) { m_mode = mode; }
    122     void setBaseURL(const KURL& baseURL) { m_baseURL = baseURL; }
    123     void setCharset(const String& charset) { m_charset = charset; }
    124     void setReferrer(const Referrer& referrer) { m_referrer = referrer; }
    125 
    126     KURL completeURL(const String& url) const;
    127 
    128     UseCounter* useCounter() const { return m_useCounter; }
    129 
    130 private:
    131     KURL m_baseURL;
    132     String m_charset;
    133     CSSParserMode m_mode;
    134     Referrer m_referrer;
    135     bool m_isHTMLDocument;
    136     bool m_useLegacyBackgroundSizeShorthandBehavior;
    137 
    138     UseCounter* m_useCounter;
    139 };
    140 
    141 const CSSParserContext& strictCSSParserContext();
    142 
    143 };
    144 
    145 #endif // CSSParserMode_h
    146