#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>
namespace WebCore {
using namespace WTF;
CSSParser::CSSParser(const CSSParserContext& context)
: m_context(context)
{
}
CSSParser::~CSSParser() = default;
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::ForWindowCSS) == CSSSupportsParser::Supported;
}
Color CSSParser::parseColor(const String& string, bool strict)
{
if (string.isEmpty())
return Color();
Color namedColor { string };
if (namedColor.isValid())
return namedColor;
RefPtr<CSSValue> value = CSSParserFastPaths::parseColor(string, strict ? HTMLStandardMode : HTMLQuirksMode);
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, const CSSParserContext* context)
{
CSSValueID id = cssValueKeywordID(string);
if (!StyleColor::isSystemColor(id))
return Color();
OptionSet<StyleColor::Options> options;
if (context && context->useSystemAppearance)
options |= StyleColor::Options::UseSystemAppearance;
return RenderTheme::singleton().systemColor(id, options);
}
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);
}
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, const Element* element)
{
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()) {
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, 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);
}
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.appendLiteral("@font-face { ");
builder.append(getPropertyNameString(propertyID));
builder.appendLiteral(" : ");
builder.append(propertyValue);
builder.appendLiteral("; }");
RefPtr<StyleRuleBase> rule = parseRule(context, nullptr, builder.toString());
if (!rule || !rule->isFontFaceRule())
return nullptr;
return downcast<StyleRuleFontFace>(*rule.get()).properties().getPropertyCSSValue(propertyID);
}
}