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 
     36 namespace WebCore {
     37 
     38 class Document;
     39 
     40 // Must not grow beyond 3 bytes, due to packing in StylePropertySet.
     41 enum CSSParserMode {
     42     HTMLStandardMode,
     43     HTMLQuirksMode,
     44     // HTML attributes are parsed in quirks mode but also allows internal properties and values.
     45     HTMLAttributeMode,
     46     // SVG attributes are parsed in quirks mode but rules differ slightly.
     47     SVGAttributeMode,
     48     // @viewport rules are parsed in standards mode but CSSOM modifications (via StylePropertySet)
     49     // must call parseViewportProperties so needs a special mode.
     50     CSSViewportRuleMode,
     51     // User agent stylesheets are parsed in standards mode but also allows internal properties and values.
     52     UASheetMode
     53 };
     54 
     55 inline bool isQuirksModeBehavior(CSSParserMode mode)
     56 {
     57     return mode == HTMLQuirksMode; // || mode == HTMLAttributeMode;
     58 }
     59 
     60 inline bool isUASheetBehavior(CSSParserMode mode)
     61 {
     62     return mode == UASheetMode;
     63 }
     64 
     65 inline bool isInternalPropertyAndValueParsingEnabledForMode(CSSParserMode mode)
     66 {
     67     return mode == HTMLAttributeMode || mode == UASheetMode;
     68 }
     69 
     70 inline bool isUnitLessLengthParsingEnabledForMode(CSSParserMode mode)
     71 {
     72     return mode == HTMLQuirksMode || mode == HTMLAttributeMode || mode == SVGAttributeMode;
     73 }
     74 
     75 inline bool isCSSViewportParsingEnabledForMode(CSSParserMode mode)
     76 {
     77     return mode == CSSViewportRuleMode;
     78 }
     79 
     80 inline bool isSVGNumberParsingEnabledForMode(CSSParserMode mode)
     81 {
     82     return mode == SVGAttributeMode;
     83 }
     84 
     85 inline bool isUseCounterEnabledForMode(CSSParserMode mode)
     86 {
     87     // We don't count the UA style sheet in our statistics.
     88     return mode != UASheetMode;
     89 }
     90 
     91 class CSSParserContext {
     92     WTF_MAKE_FAST_ALLOCATED;
     93 public:
     94     CSSParserContext(CSSParserMode);
     95     CSSParserContext(const Document&, const KURL& baseURL = KURL(), const String& charset = emptyString());
     96 
     97     bool operator==(const CSSParserContext&) const;
     98     bool operator!=(const CSSParserContext& other) const { return !(*this == other); }
     99 
    100     CSSParserMode mode() const { return m_mode; }
    101     const KURL& baseURL() const { return m_baseURL; }
    102     const String& charset() const { return m_charset; }
    103     bool isHTMLDocument() const { return m_isHTMLDocument; }
    104 
    105     // This quirk is to maintain compatibility with Android apps built on
    106     // the Android SDK prior to and including version 18. Presumably, this
    107     // can be removed any time after 2015. See http://crbug.com/277157.
    108     bool useLegacyBackgroundSizeShorthandBehavior() const { return m_useLegacyBackgroundSizeShorthandBehavior; }
    109 
    110     // FIXME: These setters shouldn't exist, however the current lifetime of CSSParserContext
    111     // is not well understood and thus we sometimes need to override these fields.
    112     void setMode(CSSParserMode mode) { m_mode = mode; }
    113     void setBaseURL(const KURL& baseURL) { m_baseURL = baseURL; }
    114     void setCharset(const String& charset) { m_charset = charset; }
    115 
    116 private:
    117     KURL m_baseURL;
    118     String m_charset;
    119     CSSParserMode m_mode;
    120     bool m_isHTMLDocument;
    121     bool m_useLegacyBackgroundSizeShorthandBehavior;
    122 };
    123 
    124 const CSSParserContext& strictCSSParserContext();
    125 
    126 };
    127 
    128 #endif // CSSParserMode_h
    129