2 * Copyright (C) 2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
4 * Copyright (C) 2004-2016 Apple Inc. All rights reserved.
5 * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com>
6 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
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.
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.
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.
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.
29 #include "CSSParser.h"
31 #include "CSSKeyframeRule.h"
32 #include "CSSParserFastPaths.h"
33 #include "CSSParserImpl.h"
34 #include "CSSPendingSubstitutionValue.h"
35 #include "CSSPropertyParser.h"
36 #include "CSSSelectorParser.h"
37 #include "CSSSupportsParser.h"
38 #include "CSSTokenizer.h"
39 #include "CSSVariableData.h"
40 #include "CSSVariableReferenceValue.h"
44 #include "RenderTheme.h"
45 #include "RuntimeEnabledFeatures.h"
47 #include "StyleColor.h"
48 #include "StyleRule.h"
49 #include "StyleSheetContents.h"
50 #include <wtf/NeverDestroyed.h>
51 #include <wtf/text/StringBuilder.h>
57 const CSSParserContext& strictCSSParserContext()
59 static NeverDestroyed<CSSParserContext> strictContext(HTMLStandardMode);
63 CSSParserContext::CSSParserContext(CSSParserMode mode, const URL& baseURL)
68 // FIXME: Force the site specific quirk below to work on iOS. Investigating other site specific quirks
69 // to see if we can enable the preference all together is to be handled by:
70 // <rdar://problem/8493309> Investigate Enabling Site Specific Quirks in MobileSafari and UIWebView
71 needsSiteSpecificQuirks = true;
75 CSSParserContext::CSSParserContext(Document& document, const URL& sheetBaseURL, const String& charset)
76 : baseURL(sheetBaseURL.isNull() ? document.baseURL() : sheetBaseURL)
78 , mode(document.inQuirksMode() ? HTMLQuirksMode : HTMLStandardMode)
79 , isHTMLDocument(document.isHTMLDocument())
80 , hasDocumentSecurityOrigin(sheetBaseURL.isNull() || document.securityOrigin().canRequest(baseURL))
83 needsSiteSpecificQuirks = document.settings().needsSiteSpecificQuirks();
84 enforcesCSSMIMETypeInNoQuirksMode = document.settings().enforceCSSMIMETypeInNoQuirksMode();
85 useLegacyBackgroundSizeShorthandBehavior = document.settings().useLegacyBackgroundSizeShorthandBehavior();
86 #if ENABLE(TEXT_AUTOSIZING)
87 textAutosizingEnabled = document.settings().textAutosizingEnabled();
89 springTimingFunctionEnabled = document.settings().springTimingFunctionEnabled();
90 constantPropertiesEnabled = document.settings().constantPropertiesEnabled();
91 conicGradientsEnabled = document.settings().conicGradientsEnabled();
92 colorFilterEnabled = document.settings().colorFilterEnabled();
93 deferredCSSParserEnabled = document.settings().deferredCSSParserEnabled();
94 allowNewLinesClamp = document.settings().appleMailLinesClampEnabled();
95 useSystemAppearance = document.page() ? document.page()->useSystemAppearance() : false;
98 // FIXME: Force the site specific quirk below to work on iOS. Investigating other site specific quirks
99 // to see if we can enable the preference all together is to be handled by:
100 // <rdar://problem/8493309> Investigate Enabling Site Specific Quirks in MobileSafari and UIWebView
101 needsSiteSpecificQuirks = true;
105 bool operator==(const CSSParserContext& a, const CSSParserContext& b)
107 return a.baseURL == b.baseURL
108 && a.charset == b.charset
110 && a.isHTMLDocument == b.isHTMLDocument
111 && a.needsSiteSpecificQuirks == b.needsSiteSpecificQuirks
112 && a.enforcesCSSMIMETypeInNoQuirksMode == b.enforcesCSSMIMETypeInNoQuirksMode
113 && a.useLegacyBackgroundSizeShorthandBehavior == b.useLegacyBackgroundSizeShorthandBehavior
114 && a.springTimingFunctionEnabled == b.springTimingFunctionEnabled
115 && a.constantPropertiesEnabled == b.constantPropertiesEnabled
116 && a.conicGradientsEnabled == b.conicGradientsEnabled
117 && a.colorFilterEnabled == b.colorFilterEnabled
118 && a.deferredCSSParserEnabled == b.deferredCSSParserEnabled
119 && a.hasDocumentSecurityOrigin == b.hasDocumentSecurityOrigin
120 && a.useSystemAppearance == b.useSystemAppearance;
123 CSSParser::CSSParser(const CSSParserContext& context)
128 CSSParser::~CSSParser() = default;
130 void CSSParser::parseSheet(StyleSheetContents* sheet, const String& string, RuleParsing ruleParsing)
132 return CSSParserImpl::parseStyleSheet(string, m_context, sheet, ruleParsing);
135 void CSSParser::parseSheetForInspector(const CSSParserContext& context, StyleSheetContents* sheet, const String& string, CSSParserObserver& observer)
137 return CSSParserImpl::parseStyleSheetForInspector(string, context, sheet, observer);
140 RefPtr<StyleRuleBase> CSSParser::parseRule(const CSSParserContext& context, StyleSheetContents* sheet, const String& string)
142 return CSSParserImpl::parseRule(string, context, sheet, CSSParserImpl::AllowImportRules);
145 RefPtr<StyleRuleKeyframe> CSSParser::parseKeyframeRule(const String& string)
147 RefPtr<StyleRuleBase> keyframe = CSSParserImpl::parseRule(string, m_context, nullptr, CSSParserImpl::KeyframeRules);
148 return downcast<StyleRuleKeyframe>(keyframe.get());
151 bool CSSParser::parseSupportsCondition(const String& condition)
153 CSSParserImpl parser(m_context, condition);
154 return CSSSupportsParser::supportsCondition(parser.tokenizer()->tokenRange(), parser, CSSSupportsParser::ForWindowCSS) == CSSSupportsParser::Supported;
157 Color CSSParser::parseColor(const String& string, bool strict)
159 if (string.isEmpty())
162 // Try named colors first.
163 Color namedColor { string };
164 if (namedColor.isValid())
167 // Try the fast path to parse hex and rgb.
168 RefPtr<CSSValue> value = CSSParserFastPaths::parseColor(string, strict ? HTMLStandardMode : HTMLQuirksMode);
170 // If that fails, try the full parser.
172 value = parseSingleValue(CSSPropertyColor, string, strictCSSParserContext());
173 if (!value || !value->isPrimitiveValue())
175 const auto& primitiveValue = downcast<CSSPrimitiveValue>(*value);
176 if (!primitiveValue.isRGBColor())
178 return primitiveValue.color();
181 Color CSSParser::parseSystemColor(const String& string, std::optional<std::reference_wrapper<const CSSParserContext>> context)
183 CSSValueID id = cssValueKeywordID(string);
184 if (!StyleColor::isSystemColor(id))
187 OptionSet<StyleColor::Options> options;
188 if (context && context->get().useSystemAppearance)
189 options |= StyleColor::Options::UseSystemAppearance;
190 return RenderTheme::singleton().systemColor(id, options);
193 RefPtr<CSSValue> CSSParser::parseSingleValue(CSSPropertyID propertyID, const String& string, const CSSParserContext& context)
195 if (string.isEmpty())
197 if (RefPtr<CSSValue> value = CSSParserFastPaths::maybeParseValue(propertyID, string, context.mode))
199 CSSTokenizer tokenizer(string);
200 return CSSPropertyParser::parseSingleValue(propertyID, tokenizer.tokenRange(), context);
203 CSSParser::ParseResult CSSParser::parseValue(MutableStyleProperties& declaration, CSSPropertyID propertyID, const String& string, bool important, const CSSParserContext& context)
205 ASSERT(!string.isEmpty());
206 RefPtr<CSSValue> value = CSSParserFastPaths::maybeParseValue(propertyID, string, context.mode);
208 return declaration.addParsedProperty(CSSProperty(propertyID, WTFMove(value), important)) ? CSSParser::ParseResult::Changed : CSSParser::ParseResult::Unchanged;
210 CSSParser parser(context);
211 return parser.parseValue(declaration, propertyID, string, important);
214 CSSParser::ParseResult CSSParser::parseCustomPropertyValue(MutableStyleProperties& declaration, const AtomicString& propertyName, const String& string, bool important, const CSSParserContext& context)
216 return CSSParserImpl::parseCustomPropertyValue(&declaration, propertyName, string, important, context);
219 CSSParser::ParseResult CSSParser::parseValue(MutableStyleProperties& declaration, CSSPropertyID propertyID, const String& string, bool important)
221 return CSSParserImpl::parseValue(&declaration, propertyID, string, important, m_context);
224 void CSSParser::parseSelector(const String& string, CSSSelectorList& selectorList)
226 CSSTokenizer tokenizer(string);
227 selectorList = CSSSelectorParser::parseSelector(tokenizer.tokenRange(), m_context, nullptr);
230 Ref<ImmutableStyleProperties> CSSParser::parseInlineStyleDeclaration(const String& string, const Element* element)
232 return CSSParserImpl::parseInlineStyleDeclaration(string, element);
235 bool CSSParser::parseDeclaration(MutableStyleProperties& declaration, const String& string)
237 return CSSParserImpl::parseDeclarationList(&declaration, string, m_context);
240 void CSSParser::parseDeclarationForInspector(const CSSParserContext& context, const String& string, CSSParserObserver& observer)
242 CSSParserImpl::parseDeclarationListForInspector(string, context, observer);
245 RefPtr<CSSValue> CSSParser::parseValueWithVariableReferences(CSSPropertyID propID, const CSSValue& value, const CustomPropertyValueMap& customProperties, TextDirection direction, WritingMode writingMode)
247 if (value.isPendingSubstitutionValue()) {
248 // FIXME: Should have a resolvedShorthands cache to stop this from being done
249 // over and over for each longhand value.
250 const CSSPendingSubstitutionValue& pendingSubstitution = downcast<CSSPendingSubstitutionValue>(value);
251 CSSPropertyID shorthandID = pendingSubstitution.shorthandPropertyId();
252 if (CSSProperty::isDirectionAwareProperty(shorthandID))
253 shorthandID = CSSProperty::resolveDirectionAwareProperty(shorthandID, direction, writingMode);
254 CSSVariableReferenceValue* shorthandValue = pendingSubstitution.shorthandValue();
255 const CSSVariableData* variableData = shorthandValue->variableDataValue();
256 ASSERT(variableData);
258 Vector<CSSParserToken> resolvedTokens;
259 if (!variableData->resolveTokenRange(customProperties, variableData->tokens(), resolvedTokens))
262 ParsedPropertyVector parsedProperties;
263 if (!CSSPropertyParser::parseValue(shorthandID, false, resolvedTokens, m_context, parsedProperties, StyleRule::Style))
266 for (auto& property : parsedProperties) {
267 if (property.id() == propID)
268 return property.value();
274 if (value.isVariableReferenceValue()) {
275 const CSSVariableReferenceValue& valueWithReferences = downcast<CSSVariableReferenceValue>(value);
276 const CSSVariableData* variableData = valueWithReferences.variableDataValue();
277 ASSERT(variableData);
279 Vector<CSSParserToken> resolvedTokens;
280 if (!variableData->resolveTokenRange(customProperties, variableData->tokens(), resolvedTokens))
283 return CSSPropertyParser::parseSingleValue(propID, resolvedTokens, m_context);
289 std::unique_ptr<Vector<double>> CSSParser::parseKeyframeKeyList(const String& selector)
291 return CSSParserImpl::parseKeyframeKeyList(selector);
294 RefPtr<CSSValue> CSSParser::parseFontFaceDescriptor(CSSPropertyID propertyID, const String& propertyValue, const CSSParserContext& context)
296 StringBuilder builder;
297 builder.appendLiteral("@font-face { ");
298 builder.append(getPropertyNameString(propertyID));
299 builder.appendLiteral(" : ");
300 builder.append(propertyValue);
301 builder.appendLiteral("; }");
302 RefPtr<StyleRuleBase> rule = parseRule(context, nullptr, builder.toString());
303 if (!rule || !rule->isFontFaceRule())
305 return downcast<StyleRuleFontFace>(*rule.get()).properties().getPropertyCSSValue(propertyID);