1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/*
* Copyright (C) 2003 Lars Knoll (knoll@kde.org)
* Copyright (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
* Copyright (C) 2004-2016 Apple Inc. All rights reserved.
* Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com>
* Copyright (C) 2008 Eric Seidel <eric@webkit.org>
* Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
* Copyright (C) 2012, 2013 Adobe Systems Incorporated. All rights reserved.
* Copyright (C) 2012 Intel Corporation. All rights reserved.
* Copyright (C) 2014 Google Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "CSSParser.h"
#include "CSSKeyframeRule.h"
#include "CSSParserFastPaths.h"
#include "CSSParserImpl.h"
#include "CSSPendingSubstitutionValue.h"
#include "CSSPropertyParser.h"
#include "CSSSelectorParser.h"
#include "CSSSupportsParser.h"
#include "CSSTokenizer.h"
#include "CSSVariableData.h"
#include "CSSVariableReferenceValue.h"
#include "Document.h"
#include "Element.h"
#include "Page.h"
#include "RenderTheme.h"
#include "RuntimeEnabledFeatures.h"
#include "Settings.h"
#include "StyleColor.h"
#include "StyleRule.h"
#include "StyleSheetContents.h"
#include <wtf/NeverDestroyed.h>
#include <wtf/text/StringBuilder.h>
using namespace WTF;
namespace WebCore {
const CSSParserContext& strictCSSParserContext()
{
static NeverDestroyed<CSSParserContext> strictContext(HTMLStandardMode);
return strictContext;
}
CSSParserContext::CSSParserContext(CSSParserMode mode, const URL& baseURL)
: baseURL(baseURL)
, mode(mode)
, cssGridLayoutEnabled(RuntimeEnabledFeatures::sharedFeatures().isCSSGridLayoutEnabled())
{
#if PLATFORM(IOS)
// FIXME: Force the site specific quirk below to work on iOS. Investigating other site specific quirks
// to see if we can enable the preference all together is to be handled by:
// <rdar://problem/8493309> Investigate Enabling Site Specific Quirks in MobileSafari and UIWebView
needsSiteSpecificQuirks = true;
#endif
}
CSSParserContext::CSSParserContext(Document& document, const URL& baseURL, const String& charset)
: baseURL(baseURL.isNull() ? document.baseURL() : baseURL)
, charset(charset)
, mode(document.inQuirksMode() ? HTMLQuirksMode : HTMLStandardMode)
, isHTMLDocument(document.isHTMLDocument())
, cssGridLayoutEnabled(document.isCSSGridLayoutEnabled())
{
needsSiteSpecificQuirks = document.settings().needsSiteSpecificQuirks();
enforcesCSSMIMETypeInNoQuirksMode = document.settings().enforceCSSMIMETypeInNoQuirksMode();
useLegacyBackgroundSizeShorthandBehavior = document.settings().useLegacyBackgroundSizeShorthandBehavior();
#if ENABLE(TEXT_AUTOSIZING)
textAutosizingEnabled = document.settings().textAutosizingEnabled();
#endif
springTimingFunctionEnabled = document.settings().springTimingFunctionEnabled();
deferredCSSParserEnabled = document.settings().deferredCSSParserEnabled();
#if PLATFORM(IOS)
// FIXME: Force the site specific quirk below to work on iOS. Investigating other site specific quirks
// to see if we can enable the preference all together is to be handled by:
// <rdar://problem/8493309> Investigate Enabling Site Specific Quirks in MobileSafari and UIWebView
needsSiteSpecificQuirks = true;
#endif
}
bool operator==(const CSSParserContext& a, const CSSParserContext& b)
{
return a.baseURL == b.baseURL
&& a.charset == b.charset
&& a.mode == b.mode
&& a.isHTMLDocument == b.isHTMLDocument
&& a.cssGridLayoutEnabled == b.cssGridLayoutEnabled
&& a.needsSiteSpecificQuirks == b.needsSiteSpecificQuirks
&& a.enforcesCSSMIMETypeInNoQuirksMode == b.enforcesCSSMIMETypeInNoQuirksMode
&& a.useLegacyBackgroundSizeShorthandBehavior == b.useLegacyBackgroundSizeShorthandBehavior
&& a.springTimingFunctionEnabled == b.springTimingFunctionEnabled
&& a.deferredCSSParserEnabled == b.deferredCSSParserEnabled;
}
CSSParser::CSSParser(const CSSParserContext& context)
: m_context(context)
{
}
CSSParser::~CSSParser()
{
}
void CSSParser::parseSheet(StyleSheetContents* sheet, const String& string, RuleParsing ruleParsing)
{
return CSSParserImpl::parseStyleSheet(string, m_context, sheet, ruleParsing);
}
void CSSParser::parseSheetForInspector(const CSSParserContext& context, StyleSheetContents* sheet, const String& string, CSSParserObserver& observer)
{
return CSSParserImpl::parseStyleSheetForInspector(string, context, sheet, observer);
}
RefPtr<StyleRuleBase> CSSParser::parseRule(const CSSParserContext& context, StyleSheetContents* sheet, const String& string)
{
return CSSParserImpl::parseRule(string, context, sheet, CSSParserImpl::AllowImportRules);
}
RefPtr<StyleRuleKeyframe> CSSParser::parseKeyframeRule(const String& string)
{
RefPtr<StyleRuleBase> keyframe = CSSParserImpl::parseRule(string, m_context, nullptr, CSSParserImpl::KeyframeRules);
return downcast<StyleRuleKeyframe>(keyframe.get());
}
bool CSSParser::parseSupportsCondition(const String& condition)
{
CSSParserImpl parser(m_context, condition);
return CSSSupportsParser::supportsCondition(parser.tokenizer()->tokenRange(), parser) == CSSSupportsParser::Supported;
}
Color CSSParser::parseColor(const String& string, bool strict)
{
if (string.isEmpty())
return Color();
// Try named colors first.
Color namedColor { string };
if (namedColor.isValid())
return namedColor;
// Try the fast path to parse hex and rgb.
RefPtr<CSSValue> value = CSSParserFastPaths::parseColor(string, strict ? HTMLStandardMode : HTMLQuirksMode);
// If that fails, try the full parser.
if (!value)
value = parseSingleValue(CSSPropertyColor, string, strictCSSParserContext());
if (!value || !value->isPrimitiveValue())
return Color();
const auto& primitiveValue = downcast<CSSPrimitiveValue>(*value);
if (!primitiveValue.isRGBColor())
return Color();
return primitiveValue.color();
}
Color CSSParser::parseSystemColor(const String& string, Document* document)
{
if (!document || !document->page())
return Color();
CSSValueID id = cssValueKeywordID(string);
if (!StyleColor::isSystemColor(id))
return Color();
return document->page()->theme().systemColor(id);
}
RefPtr<CSSValue> CSSParser::parseSingleValue(CSSPropertyID propertyID, const String& string, const CSSParserContext& context)
{
if (string.isEmpty())
return nullptr;
if (RefPtr<CSSValue> value = CSSParserFastPaths::maybeParseValue(propertyID, string, context.mode))
return value;
CSSTokenizer tokenizer(string);
return CSSPropertyParser::parseSingleValue(propertyID, tokenizer.tokenRange(), context, nullptr);
}
CSSParser::ParseResult CSSParser::parseValue(MutableStyleProperties& declaration, CSSPropertyID propertyID, const String& string, bool important, const CSSParserContext& context)
{
ASSERT(!string.isEmpty());
RefPtr<CSSValue> value = CSSParserFastPaths::maybeParseValue(propertyID, string, context.mode);
if (value)
return declaration.addParsedProperty(CSSProperty(propertyID, WTFMove(value), important)) ? CSSParser::ParseResult::Changed : CSSParser::ParseResult::Unchanged;
CSSParser parser(context);
return parser.parseValue(declaration, propertyID, string, important);
}
CSSParser::ParseResult CSSParser::parseCustomPropertyValue(MutableStyleProperties& declaration, const AtomicString& propertyName, const String& string, bool important, const CSSParserContext& context)
{
return CSSParserImpl::parseCustomPropertyValue(&declaration, propertyName, string, important, context);
}
CSSParser::ParseResult CSSParser::parseValue(MutableStyleProperties& declaration, CSSPropertyID propertyID, const String& string, bool important)
{
return CSSParserImpl::parseValue(&declaration, propertyID, string, important, m_context);
}
void CSSParser::parseSelector(const String& string, CSSSelectorList& selectorList)
{
CSSTokenizer tokenizer(string);
selectorList = CSSSelectorParser::parseSelector(tokenizer.tokenRange(), m_context, nullptr);
}
Ref<ImmutableStyleProperties> CSSParser::parseInlineStyleDeclaration(const String& string, Element* element)
{
CSSParserContext context(element->document());
context.mode = strictToCSSParserMode(element->isHTMLElement() && !element->document().inQuirksMode());
return CSSParserImpl::parseInlineStyleDeclaration(string, element);
}
bool CSSParser::parseDeclaration(MutableStyleProperties& declaration, const String& string)
{
return CSSParserImpl::parseDeclarationList(&declaration, string, m_context);
}
void CSSParser::parseDeclarationForInspector(const CSSParserContext& context, const String& string, CSSParserObserver& observer)
{
CSSParserImpl::parseDeclarationListForInspector(string, context, observer);
}
RefPtr<CSSValue> CSSParser::parseValueWithVariableReferences(CSSPropertyID propID, const CSSValue& value, const CustomPropertyValueMap& customProperties, TextDirection direction, WritingMode writingMode)
{
if (value.isPendingSubstitutionValue()) {
// FIXME: Should have a resolvedShorthands cache to stop this from being done
// over and over for each longhand value.
const CSSPendingSubstitutionValue& pendingSubstitution = downcast<CSSPendingSubstitutionValue>(value);
CSSPropertyID shorthandID = pendingSubstitution.shorthandPropertyId();
if (CSSProperty::isDirectionAwareProperty(shorthandID))
shorthandID = CSSProperty::resolveDirectionAwareProperty(shorthandID, direction, writingMode);
CSSVariableReferenceValue* shorthandValue = pendingSubstitution.shorthandValue();
const CSSVariableData* variableData = shorthandValue->variableDataValue();
ASSERT(variableData);
Vector<CSSParserToken> resolvedTokens;
if (!variableData->resolveTokenRange(customProperties, variableData->tokens(), resolvedTokens))
return nullptr;
ParsedPropertyVector parsedProperties;
if (!CSSPropertyParser::parseValue(shorthandID, false, resolvedTokens, m_context, nullptr, parsedProperties, StyleRule::Style))
return nullptr;
for (auto& property : parsedProperties) {
if (property.id() == propID)
return property.value();
}
return nullptr;
}
if (value.isVariableReferenceValue()) {
const CSSVariableReferenceValue& valueWithReferences = downcast<CSSVariableReferenceValue>(value);
const CSSVariableData* variableData = valueWithReferences.variableDataValue();
ASSERT(variableData);
Vector<CSSParserToken> resolvedTokens;
if (!variableData->resolveTokenRange(customProperties, variableData->tokens(), resolvedTokens))
return nullptr;
return CSSPropertyParser::parseSingleValue(propID, resolvedTokens, m_context, nullptr);
}
return nullptr;
}
std::unique_ptr<Vector<double>> CSSParser::parseKeyframeKeyList(const String& selector)
{
return CSSParserImpl::parseKeyframeKeyList(selector);
}
RefPtr<CSSValue> CSSParser::parseFontFaceDescriptor(CSSPropertyID propertyID, const String& propertyValue, const CSSParserContext& context)
{
StringBuilder builder;
builder.append("@font-face { ");
builder.append(getPropertyNameString(propertyID));
builder.append(" : ");
builder.append(propertyValue);
builder.append("; }");
RefPtr<StyleRuleBase> rule = parseRule(context, nullptr, builder.toString());
if (!rule || !rule->isFontFaceRule())
return nullptr;
return downcast<StyleRuleFontFace>(*rule.get()).properties().getPropertyCSSValue(propertyID);
}
}
|