Truly independent web browser
LibWeb: Restrict specified custom properties to unparsed values
Andreas Kling2888ff3f0d8f
diff --git a/Libraries/LibWeb/CSS/CSSImageValue.cpp b/Libraries/LibWeb/CSS/CSSImageValue.cppindex a11a1bd1b44..5abb2816e1d 100644--- a/Libraries/LibWeb/CSS/CSSImageValue.cpp+++ b/Libraries/LibWeb/CSS/CSSImageValue.cpp@@ -40,10 +40,6 @@ WebIDL::ExceptionOr<NonnullRefPtr<StyleValue const>> CSSImageValue::create_an_in// NB: https://drafts.css-houdini.org/css-typed-om-1/#cssstylevalue-match-a-grammar doesn't list CSSImageValue, but// we should match <image>.bool const matches_grammar = [&] {- if (property.is_custom_property()) {- // FIXME: If this is a registered custom property, check if that allows <image>.- return true;- }return property_accepts_type(property.id(), ValueType::Image);}();if (perform_type_check == PerformTypeCheck::Yes && !matches_grammar) {diff --git a/Libraries/LibWeb/CSS/CSSKeywordValue.cpp b/Libraries/LibWeb/CSS/CSSKeywordValue.cppindex 79c452bede1..cb6d3751377 100644--- a/Libraries/LibWeb/CSS/CSSKeywordValue.cpp+++ b/Libraries/LibWeb/CSS/CSSKeywordValue.cpp@@ -81,10 +81,6 @@ WebIDL::ExceptionOr<NonnullRefPtr<StyleValue const>> CSSKeywordValue::create_an_// specified in the grammar for width), they apply to this comparison as well.if (auto keyword = keyword_from_string(m_value); keyword.has_value() && is_css_wide_keyword(*keyword))return true;- if (property.is_custom_property()) {- // FIXME: If this is a registered custom property, check if that allows the keyword.- return true;- }// NB: The keywords listed for a property are not all valid on their own, like `hanging` for text-indent,// and a property may accept identifiers that are not listed, like a <custom-ident> or the axis keywords// of scroll-snap-type. So we check if the property accepts the value, as an identifier, when parsed.diff --git a/Libraries/LibWeb/CSS/CSSMathValue.cpp b/Libraries/LibWeb/CSS/CSSMathValue.cppindex 74313104c59..3fbfcc5e76b 100644--- a/Libraries/LibWeb/CSS/CSSMathValue.cpp+++ b/Libraries/LibWeb/CSS/CSSMathValue.cpp@@ -31,7 +31,6 @@ WebIDL::ExceptionOr<NonnullRefPtr<StyleValue const>> CSSMathValue::create_an_int//// Return the value.- // FIXME: Check types allowed by registered custom properties.auto context = CalculationContext::for_property(property);auto matches = [&] {if (type().matches_angle(context.percentages_resolve_as))diff --git a/Libraries/LibWeb/CSS/CSSUnitValue.cpp b/Libraries/LibWeb/CSS/CSSUnitValue.cppindex b770f89cb23..3f270c63b17 100644--- a/Libraries/LibWeb/CSS/CSSUnitValue.cpp+++ b/Libraries/LibWeb/CSS/CSSUnitValue.cpp@@ -19,7 +19,6 @@#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>#include <LibWeb/CSS/StyleValues/ResolutionStyleValue.h>#include <LibWeb/CSS/StyleValues/TimeStyleValue.h>-#include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>#include <LibWeb/CSS/Units.h>#include <LibWeb/WebIDL/ExceptionOr.h>@@ -332,13 +331,6 @@ WebIDL::ExceptionOr<NonnullRefPtr<StyleValue const>> CSSUnitValue::create_an_int//// Return the value.- // NB: We store all custom properties as UnresolvedStyleValue, so we always need to create one here.- if (perform_type_check == PerformTypeCheck::Yes && property.is_custom_property()) {- Utf16StringBuilder builder;- serialize_unit_value(builder, {}, {});- return UnresolvedStyleValue::create(builder.to_string(), Parser::SubstitutionFunctionsPresence {});- }-auto wrap_in_math_sum = [this, &property](auto&& value) -> NonnullRefPtr<StyleValue const> {auto context = CalculationContext::for_property(property);Vector<CalcNodeRef> children;@@ -381,14 +373,13 @@ WebIDL::ExceptionOr<NonnullRefPtr<StyleValue const>> CSSUnitValue::create_an_int.release_nonnull();}- // FIXME: Check types allowed by registered custom properties.auto style_value = value->visit([&](Number const& number) -> RefPtr<StyleValue const> {// NB: Our parser represents an <opacity-value> as an opacity value that holds the number.if (property_accepts_type(property.id(), ValueType::OpacityValue))return OpacityValueStyleValue::create(NumberStyleValue::create(number.value()));- // NB: Number before Integer, because a custom property accepts either and we want to avoid rounding in that case.+ // NB: Prefer <number> to <integer> when both are accepted.if (property_accepts_type(property.id(), ValueType::Number)) {if (property_accepts_number(property.id(), number.value()))return NumberStyleValue::create(number.value());diff --git a/Libraries/LibWeb/CSS/StylePropertyMap.cpp b/Libraries/LibWeb/CSS/StylePropertyMap.cppindex c03ecddebf9..a3a4729894d 100644--- a/Libraries/LibWeb/CSS/StylePropertyMap.cpp+++ b/Libraries/LibWeb/CSS/StylePropertyMap.cpp@@ -68,8 +68,13 @@ static WebIDL::ExceptionOr<NonnullRefPtr<StyleValue const>> create_an_internal_r{// To create an internal representation, given a string property and a string or CSSStyleValue value:auto internal_representation = TRY(value.visit(- [&property](GC::Ref<CSSStyleValue> const& css_style_value) {- return css_style_value->create_an_internal_representation(property, CSSStyleValue::PerformTypeCheck::Yes);+ [&property](GC::Ref<CSSStyleValue> const& css_style_value) -> WebIDL::ExceptionOr<NonnullRefPtr<StyleValue const>> {+ auto internal_representation = TRY(css_style_value->create_an_internal_representation(property, CSSStyleValue::PerformTypeCheck::Yes));+ // INTEROP: Custom properties, including registered ones, only accept CSSUnparsedValue objects and+ // CSS-wide keywords.+ if (property.is_custom_property() && !is<CSSUnparsedValue>(*css_style_value) && !internal_representation->is_css_wide_keyword())+ return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Custom properties only accept unparsed values and CSS-wide keywords"_utf16 };+ return internal_representation;},[&](Utf16String const& css_text) -> WebIDL::ExceptionOr<NonnullRefPtr<StyleValue const>> {// If value is a USVString,diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/typedom.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/typedom.txtindex c1bb6a32925..76371f57141 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/typedom.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/typedom.txt@@ -2,8 +2,8 @@ Harness status: OKFound 325 tests-292 Pass-33 Fail+320 Pass+5 FailPass Computed * is reified as CSSUnparsedValuePass Computed <angle> is reified as CSSUnitValuePass Computed <color> is reified as CSSStyleValue@@ -166,36 +166,36 @@ Pass CSSUnparsedValue is accepted via set() for syntax <length>+ [attributeStylePass CSSUnparsedValue is accepted via set() for syntax <length>+ [styleMap]Pass CSSUnparsedValue is accepted via set() for syntax <length># [attributeStyleMap]Pass CSSUnparsedValue is accepted via set() for syntax <length># [styleMap]-Fail CSSKeywordValue rejected by set() for syntax * [attributeStyleMap]-Fail CSSKeywordValue rejected by set() for syntax * [styleMap]-Fail CSSKeywordValue rejected by set() for syntax foo [attributeStyleMap]-Fail CSSKeywordValue rejected by set() for syntax foo [styleMap]-Fail CSSUnitValue rejected by set() for syntax <angle> [attributeStyleMap]-Fail CSSUnitValue rejected by set() for syntax <angle> [styleMap]-Fail CSSKeywordValue rejected by set() for syntax <custom-ident> [attributeStyleMap]-Fail CSSKeywordValue rejected by set() for syntax <custom-ident> [styleMap]-Fail CSSImageValue rejected by set() for syntax <image> [attributeStyleMap]-Fail CSSImageValue rejected by set() for syntax <image> [styleMap]-Fail CSSUnitValue rejected by set() for syntax <integer> [attributeStyleMap]-Fail CSSUnitValue rejected by set() for syntax <integer> [styleMap]-Fail CSSUnitValue rejected by set() for syntax <length-percentage> [attributeStyleMap]-Fail CSSUnitValue rejected by set() for syntax <length-percentage> [styleMap]-Fail CSSUnitValue rejected by set() for syntax <length> [attributeStyleMap]-Fail CSSUnitValue rejected by set() for syntax <length> [styleMap]-Fail CSSUnitValue rejected by set() for syntax <number> [attributeStyleMap]-Fail CSSUnitValue rejected by set() for syntax <number> [styleMap]-Fail CSSUnitValue rejected by set() for syntax <percentage> [attributeStyleMap]-Fail CSSUnitValue rejected by set() for syntax <percentage> [styleMap]-Fail CSSUnitValue rejected by set() for syntax <resolution> [attributeStyleMap]-Fail CSSUnitValue rejected by set() for syntax <resolution> [styleMap]-Fail CSSUnitValue rejected by set() for syntax <time> [attributeStyleMap]-Fail CSSUnitValue rejected by set() for syntax <time> [styleMap]+Pass CSSKeywordValue rejected by set() for syntax * [attributeStyleMap]+Pass CSSKeywordValue rejected by set() for syntax * [styleMap]+Pass CSSKeywordValue rejected by set() for syntax foo [attributeStyleMap]+Pass CSSKeywordValue rejected by set() for syntax foo [styleMap]+Pass CSSUnitValue rejected by set() for syntax <angle> [attributeStyleMap]+Pass CSSUnitValue rejected by set() for syntax <angle> [styleMap]+Pass CSSKeywordValue rejected by set() for syntax <custom-ident> [attributeStyleMap]+Pass CSSKeywordValue rejected by set() for syntax <custom-ident> [styleMap]+Pass CSSImageValue rejected by set() for syntax <image> [attributeStyleMap]+Pass CSSImageValue rejected by set() for syntax <image> [styleMap]+Pass CSSUnitValue rejected by set() for syntax <integer> [attributeStyleMap]+Pass CSSUnitValue rejected by set() for syntax <integer> [styleMap]+Pass CSSUnitValue rejected by set() for syntax <length-percentage> [attributeStyleMap]+Pass CSSUnitValue rejected by set() for syntax <length-percentage> [styleMap]+Pass CSSUnitValue rejected by set() for syntax <length> [attributeStyleMap]+Pass CSSUnitValue rejected by set() for syntax <length> [styleMap]+Pass CSSUnitValue rejected by set() for syntax <number> [attributeStyleMap]+Pass CSSUnitValue rejected by set() for syntax <number> [styleMap]+Pass CSSUnitValue rejected by set() for syntax <percentage> [attributeStyleMap]+Pass CSSUnitValue rejected by set() for syntax <percentage> [styleMap]+Pass CSSUnitValue rejected by set() for syntax <resolution> [attributeStyleMap]+Pass CSSUnitValue rejected by set() for syntax <resolution> [styleMap]+Pass CSSUnitValue rejected by set() for syntax <time> [attributeStyleMap]+Pass CSSUnitValue rejected by set() for syntax <time> [styleMap]Pass CSSTransformValue rejected by set() for syntax <transform-list> [attributeStyleMap]Pass CSSTransformValue rejected by set() for syntax <transform-list> [styleMap]-Fail CSSUnitValue rejected by set() for syntax <length>+ [attributeStyleMap]-Fail CSSUnitValue rejected by set() for syntax <length>+ [styleMap]-Fail CSSUnitValue rejected by set() for syntax <length># [attributeStyleMap]-Fail CSSUnitValue rejected by set() for syntax <length># [styleMap]+Pass CSSUnitValue rejected by set() for syntax <length>+ [attributeStyleMap]+Pass CSSUnitValue rejected by set() for syntax <length>+ [styleMap]+Pass CSSUnitValue rejected by set() for syntax <length># [attributeStyleMap]+Pass CSSUnitValue rejected by set() for syntax <length># [styleMap]Pass Appending a string to * is not allowed [attributeStyleMap]Pass Appending a string to * is not allowed [styleMap]Pass Appending a string to foo+ is not allowed [attributeStyleMap]diff --git a/Tests/LibWeb/Text/input/css/typed-om-registered-small-number.html b/Tests/LibWeb/Text/input/css/typed-om-registered-small-number.htmlindex e8b34fffed2..ebe752a48c8 100644--- a/Tests/LibWeb/Text/input/css/typed-om-registered-small-number.html+++ b/Tests/LibWeb/Text/input/css/typed-om-registered-small-number.html@@ -7,8 +7,8 @@CSS.registerProperty({ name: "--rounded-number", syntax: "<number>", inherits: false, initialValue: "1" });const target = document.getElementById("target");- target.attributeStyleMap.set("--tiny-number", CSS.number(0.0000004));- target.attributeStyleMap.set("--rounded-number", CSS.number(1 / 3));+ target.attributeStyleMap.set("--tiny-number", CSS.number(0.0000004).toString());+ target.attributeStyleMap.set("--rounded-number", CSS.number(1 / 3).toString());println(`tiny: ${target.style.getPropertyValue("--tiny-number")}`);println(`rounded: ${target.style.getPropertyValue("--rounded-number")}`);