1/*
2 * Copyright (C) 2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "CSSParserMode.h"
29#include "TextEncoding.h"
30#include <wtf/HashFunctions.h>
31#include <wtf/URL.h>
32#include <wtf/URLHash.h>
33#include <wtf/text/StringHash.h>
34
35namespace WebCore {
36
37class Document;
38
39struct CSSParserContext {
40 WTF_MAKE_FAST_ALLOCATED;
41public:
42 CSSParserContext(CSSParserMode, const URL& baseURL = URL());
43 WEBCORE_EXPORT CSSParserContext(const Document&, const URL& baseURL = URL(), const String& charset = emptyString());
44
45 URL baseURL;
46 String charset;
47 CSSParserMode mode { HTMLStandardMode };
48 bool isHTMLDocument { false };
49#if ENABLE(TEXT_AUTOSIZING)
50 bool textAutosizingEnabled { false };
51#endif
52#if ENABLE(OVERFLOW_SCROLLING_TOUCH)
53 bool legacyOverflowScrollingTouchEnabled { false };
54#endif
55 bool enforcesCSSMIMETypeInNoQuirksMode { true };
56 bool useLegacyBackgroundSizeShorthandBehavior { false };
57 bool springTimingFunctionEnabled { false };
58 bool constantPropertiesEnabled { false };
59 bool colorFilterEnabled { false };
60#if ENABLE(ATTACHMENT_ELEMENT)
61 bool attachmentEnabled { false };
62#endif
63 bool deferredCSSParserEnabled { false };
64
65 // This is only needed to support getMatchedCSSRules.
66 bool hasDocumentSecurityOrigin { false };
67
68 bool useSystemAppearance { false };
69
70 URL completeURL(const String& url) const
71 {
72 if (url.isNull())
73 return URL();
74 if (charset.isEmpty())
75 return URL(baseURL, url);
76 TextEncoding encoding(charset);
77 auto& encodingForURLParsing = encoding.encodingForFormSubmissionOrURLParsing();
78 return URL(baseURL, url, encodingForURLParsing == UTF8Encoding() ? nullptr : &encodingForURLParsing);
79 }
80
81 bool isContentOpaque { false };
82};
83
84bool operator==(const CSSParserContext&, const CSSParserContext&);
85inline bool operator!=(const CSSParserContext& a, const CSSParserContext& b) { return !(a == b); }
86
87WEBCORE_EXPORT const CSSParserContext& strictCSSParserContext();
88
89struct CSSParserContextHash {
90 static unsigned hash(const CSSParserContext& key)
91 {
92 auto hash = WTF::URLHash::hash(key.baseURL);
93 if (!key.charset.isEmpty())
94 hash ^= StringHash::hash(key.charset);
95 unsigned bits = key.isHTMLDocument << 0
96#if ENABLE(TEXT_AUTOSIZING)
97 & key.textAutosizingEnabled << 1
98#endif
99#if ENABLE(OVERFLOW_SCROLLING_TOUCH)
100 & key.legacyOverflowScrollingTouchEnabled << 2
101#endif
102 & key.enforcesCSSMIMETypeInNoQuirksMode << 3
103 & key.useLegacyBackgroundSizeShorthandBehavior << 4
104 & key.springTimingFunctionEnabled << 5
105 & key.constantPropertiesEnabled << 6
106 & key.colorFilterEnabled << 7
107 & key.deferredCSSParserEnabled << 8
108 & key.hasDocumentSecurityOrigin << 9
109 & key.useSystemAppearance << 10
110#if ENABLE(ATTACHMENT_ELEMENT)
111 & key.attachmentEnabled << 11
112#endif
113 & key.mode << 12; // Keep this last.
114 hash ^= WTF::intHash(bits);
115 return hash;
116 }
117 static bool equal(const CSSParserContext& a, const CSSParserContext& b)
118 {
119 return a == b;
120 }
121 static const bool safeToCompareToEmptyOrDeleted = false;
122};
123
124} // namespace WebCore
125
126namespace WTF {
127template<> struct HashTraits<WebCore::CSSParserContext> : GenericHashTraits<WebCore::CSSParserContext> {
128 static void constructDeletedValue(WebCore::CSSParserContext& slot) { new (NotNull, &slot.baseURL) URL(WTF::HashTableDeletedValue); }
129 static bool isDeletedValue(const WebCore::CSSParserContext& value) { return value.baseURL.isHashTableDeletedValue(); }
130 static WebCore::CSSParserContext emptyValue() { return WebCore::CSSParserContext(WebCore::HTMLStandardMode); }
131};
132
133template<> struct DefaultHash<WebCore::CSSParserContext> {
134 typedef WebCore::CSSParserContextHash Hash;
135};
136} // namespace WTF
137