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 blink {
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
isQuirksModeBehavior(CSSParserMode mode)56 inline bool isQuirksModeBehavior(CSSParserMode mode)
57 {
58 return mode == HTMLQuirksMode; // || mode == HTMLAttributeMode;
59 }
60
isUASheetBehavior(CSSParserMode mode)61 inline bool isUASheetBehavior(CSSParserMode mode)
62 {
63 return mode == UASheetMode;
64 }
65
isInternalPropertyAndValueParsingEnabledForMode(CSSParserMode mode)66 inline bool isInternalPropertyAndValueParsingEnabledForMode(CSSParserMode mode)
67 {
68 return mode == HTMLAttributeMode || mode == UASheetMode;
69 }
70
isUnitLessLengthParsingEnabledForMode(CSSParserMode mode)71 inline bool isUnitLessLengthParsingEnabledForMode(CSSParserMode mode)
72 {
73 return mode == HTMLQuirksMode || mode == HTMLAttributeMode || mode == SVGAttributeMode;
74 }
75
isCSSViewportParsingEnabledForMode(CSSParserMode mode)76 inline bool isCSSViewportParsingEnabledForMode(CSSParserMode mode)
77 {
78 return mode == CSSViewportRuleMode;
79 }
80
isUseCounterEnabledForMode(CSSParserMode mode)81 inline bool isUseCounterEnabledForMode(CSSParserMode mode)
82 {
83 // We don't count the UA style sheet in our statistics.
84 return mode != UASheetMode;
85 }
86
87 class UseCounter;
88
89 class CSSParserContext {
90 WTF_MAKE_FAST_ALLOCATED;
91 public:
92 CSSParserContext(CSSParserMode, UseCounter*);
93 // FIXME: We shouldn't need the UseCounter argument as we could infer it from the Document
94 // but some callers want to disable use counting (e.g. the WebInspector).
95 CSSParserContext(const Document&, UseCounter*, const KURL& baseURL = KURL(), const String& charset = emptyString());
96 // FIXME: This constructor shouldn't exist if we properly piped the UseCounter through the CSS
97 // subsystem. Currently the UseCounter life time is too crazy and we need a way to override it.
98 CSSParserContext(const CSSParserContext&, UseCounter*);
99
100 bool operator==(const CSSParserContext&) const;
101 bool operator!=(const CSSParserContext& other) const { return !(*this == other); }
102
mode()103 CSSParserMode mode() const { return m_mode; }
baseURL()104 const KURL& baseURL() const { return m_baseURL; }
charset()105 const String& charset() const { return m_charset; }
referrer()106 const Referrer& referrer() const { return m_referrer; }
isHTMLDocument()107 bool isHTMLDocument() const { return m_isHTMLDocument; }
108
109 // This quirk is to maintain compatibility with Android apps built on
110 // the Android SDK prior to and including version 18. Presumably, this
111 // can be removed any time after 2015. See http://crbug.com/277157.
useLegacyBackgroundSizeShorthandBehavior()112 bool useLegacyBackgroundSizeShorthandBehavior() const { return m_useLegacyBackgroundSizeShorthandBehavior; }
113
114 // FIXME: These setters shouldn't exist, however the current lifetime of CSSParserContext
115 // is not well understood and thus we sometimes need to override these fields.
setMode(CSSParserMode mode)116 void setMode(CSSParserMode mode) { m_mode = mode; }
setBaseURL(const KURL & baseURL)117 void setBaseURL(const KURL& baseURL) { m_baseURL = baseURL; }
setCharset(const String & charset)118 void setCharset(const String& charset) { m_charset = charset; }
setReferrer(const Referrer & referrer)119 void setReferrer(const Referrer& referrer) { m_referrer = referrer; }
120
121 KURL completeURL(const String& url) const;
122
useCounter()123 UseCounter* useCounter() const { return m_useCounter; }
124
125 private:
126 KURL m_baseURL;
127 String m_charset;
128 CSSParserMode m_mode;
129 Referrer m_referrer;
130 bool m_isHTMLDocument;
131 bool m_useLegacyBackgroundSizeShorthandBehavior;
132
133 UseCounter* m_useCounter;
134 };
135
136 const CSSParserContext& strictCSSParserContext();
137
138 };
139
140 #endif // CSSParserMode_h
141