1 | /*
|
---|
2 | * Copyright (C) 2003 Lars Knoll ([email protected])
|
---|
3 | * Copyright (C) 2005 Allan Sandfeld Jensen ([email protected])
|
---|
4 | * Copyright (C) 2004-2022 Apple Inc. All rights reserved.
|
---|
5 | * Copyright (C) 2007 Nicholas Shanks <[email protected]>
|
---|
6 | * Copyright (C) 2008 Eric Seidel <[email protected]>
|
---|
7 | * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
|
---|
8 | * Copyright (C) 2012, 2013 Adobe Systems Incorporated. All rights reserved.
|
---|
9 | * Copyright (C) 2012 Intel Corporation. All rights reserved.
|
---|
10 | * Copyright (C) 2014 Google Inc. All rights reserved.
|
---|
11 | *
|
---|
12 | * This library is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU Library General Public
|
---|
14 | * License as published by the Free Software Foundation; either
|
---|
15 | * version 2 of the License, or (at your option) any later version.
|
---|
16 | *
|
---|
17 | * This library is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * Library General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU Library General Public License
|
---|
23 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
24 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
25 | * Boston, MA 02110-1301, USA.
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include "config.h"
|
---|
29 | #include "CSSParser.h"
|
---|
30 |
|
---|
31 | #include "CSSCustomPropertyValue.h"
|
---|
32 | #include "CSSKeyframeRule.h"
|
---|
33 | #include "CSSParserFastPaths.h"
|
---|
34 | #include "CSSParserImpl.h"
|
---|
35 | #include "CSSParserTokenRange.h"
|
---|
36 | #include "CSSPendingSubstitutionValue.h"
|
---|
37 | #include "CSSPropertyParser.h"
|
---|
38 | #include "CSSPropertyParserHelpers.h"
|
---|
39 | #include "CSSSelectorParser.h"
|
---|
40 | #include "CSSSupportsParser.h"
|
---|
41 | #include "CSSTokenizer.h"
|
---|
42 | #include "CSSValuePool.h"
|
---|
43 | #include "CSSVariableData.h"
|
---|
44 | #include "CSSVariableReferenceValue.h"
|
---|
45 | #include "Document.h"
|
---|
46 | #include "Element.h"
|
---|
47 | #include "Page.h"
|
---|
48 | #include "RenderStyle.h"
|
---|
49 | #include "RenderTheme.h"
|
---|
50 | #include "Settings.h"
|
---|
51 | #include "StyleBuilder.h"
|
---|
52 | #include "StyleColor.h"
|
---|
53 | #include "StyleResolver.h"
|
---|
54 | #include "StyleRule.h"
|
---|
55 | #include "StyleSheetContents.h"
|
---|
56 | #include <wtf/NeverDestroyed.h>
|
---|
57 | #include <wtf/text/StringBuilder.h>
|
---|
58 |
|
---|
59 | namespace WebCore {
|
---|
60 |
|
---|
61 | CSSParser::CSSParser(const CSSParserContext& context)
|
---|
62 | : m_context(context)
|
---|
63 | {
|
---|
64 | }
|
---|
65 |
|
---|
66 | CSSParser::~CSSParser() = default;
|
---|
67 |
|
---|
68 | void CSSParser::parseSheet(StyleSheetContents& sheet, const String& string)
|
---|
69 | {
|
---|
70 | return CSSParserImpl::parseStyleSheet(string, m_context, sheet);
|
---|
71 | }
|
---|
72 |
|
---|
73 | void CSSParser::parseSheetForInspector(const CSSParserContext& context, StyleSheetContents* sheet, const String& string, CSSParserObserver& observer)
|
---|
74 | {
|
---|
75 | return CSSParserImpl::parseStyleSheetForInspector(string, context, sheet, observer);
|
---|
76 | }
|
---|
77 |
|
---|
78 | RefPtr<StyleRuleBase> CSSParser::parseRule(const CSSParserContext& context, StyleSheetContents* sheet, const String& string)
|
---|
79 | {
|
---|
80 | return CSSParserImpl::parseRule(string, context, sheet, CSSParserImpl::AllowImportRules);
|
---|
81 | }
|
---|
82 |
|
---|
83 | RefPtr<StyleRuleKeyframe> CSSParser::parseKeyframeRule(const String& string)
|
---|
84 | {
|
---|
85 | RefPtr<StyleRuleBase> keyframe = CSSParserImpl::parseRule(string, m_context, nullptr, CSSParserImpl::KeyframeRules);
|
---|
86 | return downcast<StyleRuleKeyframe>(keyframe.get());
|
---|
87 | }
|
---|
88 |
|
---|
89 | bool CSSParser::parseSupportsCondition(const String& condition)
|
---|
90 | {
|
---|
91 | CSSParserImpl parser(m_context, condition);
|
---|
92 | if (!parser.tokenizer())
|
---|
93 | return false;
|
---|
94 | return CSSSupportsParser::supportsCondition(parser.tokenizer()->tokenRange(), parser, CSSSupportsParser::ForWindowCSS) == CSSSupportsParser::Supported;
|
---|
95 | }
|
---|
96 |
|
---|
97 | Color CSSParser::parseColor(const String& string, const CSSParserContext& context)
|
---|
98 | {
|
---|
99 | bool strict = !isQuirksModeBehavior(context.mode);
|
---|
100 | if (auto color = CSSParserFastPaths::parseSimpleColor(string, strict))
|
---|
101 | return *color;
|
---|
102 | auto value = parseSingleValue(CSSPropertyColor, string, context);
|
---|
103 | if (!is<CSSPrimitiveValue>(value))
|
---|
104 | return { };
|
---|
105 | auto& primitiveValue = downcast<CSSPrimitiveValue>(*value);
|
---|
106 | if (!primitiveValue.isRGBColor())
|
---|
107 | return { };
|
---|
108 | return primitiveValue.color();
|
---|
109 | }
|
---|
110 |
|
---|
111 | Color CSSParser::parseColorWithoutContext(const String& string, bool strict)
|
---|
112 | {
|
---|
113 | if (auto color = CSSParserFastPaths::parseSimpleColor(string, strict))
|
---|
114 | return *color;
|
---|
115 | // FIXME: Unclear why we want to ignore the boolean argument "strict" and always pass strictCSSParserContext here.
|
---|
116 | auto value = parseSingleValue(CSSPropertyColor, string, strictCSSParserContext());
|
---|
117 | if (!is<CSSPrimitiveValue>(value))
|
---|
118 | return { };
|
---|
119 | auto& primitiveValue = downcast<CSSPrimitiveValue>(*value);
|
---|
120 | if (!primitiveValue.isRGBColor())
|
---|
121 | return { };
|
---|
122 | return primitiveValue.color();
|
---|
123 | }
|
---|
124 |
|
---|
125 | Color CSSParser::parseSystemColor(StringView string)
|
---|
126 | {
|
---|
127 | auto keyword = cssValueKeywordID(string);
|
---|
128 | if (!StyleColor::isSystemColorKeyword(keyword))
|
---|
129 | return { };
|
---|
130 | return RenderTheme::singleton().systemColor(keyword, { });
|
---|
131 | }
|
---|
132 |
|
---|
133 | std::optional<SRGBA<uint8_t>> CSSParser::parseNamedColor(StringView string)
|
---|
134 | {
|
---|
135 | return CSSParserFastPaths::parseNamedColor(string);
|
---|
136 | }
|
---|
137 |
|
---|
138 | std::optional<SRGBA<uint8_t>> CSSParser::parseHexColor(StringView string)
|
---|
139 | {
|
---|
140 | return CSSParserFastPaths::parseHexColor(string);
|
---|
141 | }
|
---|
142 |
|
---|
143 | RefPtr<CSSValue> CSSParser::parseSingleValue(CSSPropertyID propertyID, const String& string, const CSSParserContext& context)
|
---|
144 | {
|
---|
145 | if (string.isEmpty())
|
---|
146 | return nullptr;
|
---|
147 | if (auto value = CSSParserFastPaths::maybeParseValue(propertyID, string, context))
|
---|
148 | return value;
|
---|
149 | CSSTokenizer tokenizer(string);
|
---|
150 | return CSSPropertyParser::parseSingleValue(propertyID, tokenizer.tokenRange(), context);
|
---|
151 | }
|
---|
152 |
|
---|
153 | CSSParser::ParseResult CSSParser::parseValue(MutableStyleProperties& declaration, CSSPropertyID propertyID, const String& string, bool important, const CSSParserContext& context)
|
---|
154 | {
|
---|
155 | ASSERT(!string.isEmpty());
|
---|
156 | if (auto value = CSSParserFastPaths::maybeParseValue(propertyID, string, context))
|
---|
157 | return declaration.addParsedProperty(CSSProperty(propertyID, WTFMove(value), important)) ? CSSParser::ParseResult::Changed : CSSParser::ParseResult::Unchanged;
|
---|
158 | CSSParser parser(context);
|
---|
159 | return parser.parseValue(declaration, propertyID, string, important);
|
---|
160 | }
|
---|
161 |
|
---|
162 | CSSParser::ParseResult CSSParser::parseCustomPropertyValue(MutableStyleProperties& declaration, const AtomString& propertyName, const String& string, bool important, const CSSParserContext& context)
|
---|
163 | {
|
---|
164 | return CSSParserImpl::parseCustomPropertyValue(&declaration, propertyName, string, important, context);
|
---|
165 | }
|
---|
166 |
|
---|
167 | CSSParser::ParseResult CSSParser::parseValue(MutableStyleProperties& declaration, CSSPropertyID propertyID, const String& string, bool important)
|
---|
168 | {
|
---|
169 | return CSSParserImpl::parseValue(&declaration, propertyID, string, important, m_context);
|
---|
170 | }
|
---|
171 |
|
---|
172 | std::optional<CSSSelectorList> CSSParser::parseSelector(const String& string)
|
---|
173 | {
|
---|
174 | return parseCSSSelector(CSSTokenizer(string).tokenRange(), m_context, nullptr);
|
---|
175 | }
|
---|
176 |
|
---|
177 | Ref<ImmutableStyleProperties> CSSParser::parseInlineStyleDeclaration(const String& string, const Element* element)
|
---|
178 | {
|
---|
179 | return CSSParserImpl::parseInlineStyleDeclaration(string, element);
|
---|
180 | }
|
---|
181 |
|
---|
182 | bool CSSParser::parseDeclaration(MutableStyleProperties& declaration, const String& string)
|
---|
183 | {
|
---|
184 | return CSSParserImpl::parseDeclarationList(&declaration, string, m_context);
|
---|
185 | }
|
---|
186 |
|
---|
187 | void CSSParser::parseDeclarationForInspector(const CSSParserContext& context, const String& string, CSSParserObserver& observer)
|
---|
188 | {
|
---|
189 | CSSParserImpl::parseDeclarationListForInspector(string, context, observer);
|
---|
190 | }
|
---|
191 |
|
---|
192 | RefPtr<CSSValue> CSSParser::parseValueWithVariableReferences(CSSPropertyID propID, const CSSValue& value, Style::BuilderState& builderState)
|
---|
193 | {
|
---|
194 | ASSERT((propID == CSSPropertyCustom && value.isCustomPropertyValue()) || (propID != CSSPropertyCustom && !value.isCustomPropertyValue()));
|
---|
195 |
|
---|
196 | if (is<CSSPendingSubstitutionValue>(value)) {
|
---|
197 | // FIXME: Should have a resolvedShorthands cache to stop this from being done over and over for each longhand value.
|
---|
198 | auto& substitution = downcast<CSSPendingSubstitutionValue>(value);
|
---|
199 |
|
---|
200 | auto shorthandID = substitution.shorthandPropertyId();
|
---|
201 |
|
---|
202 | auto resolvedData = substitution.shorthandValue().resolveVariableReferences(builderState);
|
---|
203 | if (!resolvedData)
|
---|
204 | return nullptr;
|
---|
205 |
|
---|
206 | ParsedPropertyVector parsedProperties;
|
---|
207 | if (!CSSPropertyParser::parseValue(shorthandID, false, resolvedData->tokens(), substitution.shorthandValue().context(), parsedProperties, StyleRuleType::Style))
|
---|
208 | return nullptr;
|
---|
209 |
|
---|
210 | for (auto& property : parsedProperties) {
|
---|
211 | if (property.id() == propID)
|
---|
212 | return property.value();
|
---|
213 | }
|
---|
214 |
|
---|
215 | return nullptr;
|
---|
216 | }
|
---|
217 |
|
---|
218 | if (value.isVariableReferenceValue()) {
|
---|
219 | const CSSVariableReferenceValue& valueWithReferences = downcast<CSSVariableReferenceValue>(value);
|
---|
220 | auto resolvedData = valueWithReferences.resolveVariableReferences(builderState);
|
---|
221 | if (!resolvedData)
|
---|
222 | return nullptr;
|
---|
223 | return CSSPropertyParser::parseSingleValue(propID, resolvedData->tokens(), valueWithReferences.context());
|
---|
224 | }
|
---|
225 |
|
---|
226 | const auto& customPropValue = downcast<CSSCustomPropertyValue>(value);
|
---|
227 | const auto& valueWithReferences = std::get<Ref<CSSVariableReferenceValue>>(customPropValue.value()).get();
|
---|
228 |
|
---|
229 | auto& name = downcast<CSSCustomPropertyValue>(value).name();
|
---|
230 | auto* registered = builderState.document().getCSSRegisteredCustomPropertySet().get(name);
|
---|
231 | auto& syntax = registered ? registered->syntax : "*"_s;
|
---|
232 | auto resolvedData = valueWithReferences.resolveVariableReferences(builderState);
|
---|
233 | if (!resolvedData)
|
---|
234 | return nullptr;
|
---|
235 |
|
---|
236 | // FIXME handle REM cycles.
|
---|
237 | HashSet<CSSPropertyID> dependencies;
|
---|
238 | CSSPropertyParser::collectParsedCustomPropertyValueDependencies(syntax, false, dependencies, resolvedData->tokens(), valueWithReferences.context());
|
---|
239 |
|
---|
240 | for (auto id : dependencies)
|
---|
241 | builderState.builder().applyProperty(id);
|
---|
242 |
|
---|
243 | return CSSPropertyParser::parseTypedCustomPropertyValue(AtomString { name }, syntax, resolvedData->tokens(), builderState, valueWithReferences.context());
|
---|
244 | }
|
---|
245 |
|
---|
246 | Vector<double> CSSParser::parseKeyframeKeyList(const String& selector)
|
---|
247 | {
|
---|
248 | return CSSParserImpl::parseKeyframeKeyList(selector);
|
---|
249 | }
|
---|
250 |
|
---|
251 | }
|
---|