Skip to content

Truly independent web browser

LibWeb: Accept Typed OM numbers and percentages for <opacity-value>

Andreas Kling1d0ea098f752
diff --git a/Libraries/LibWeb/CSS/CSSMathValue.cpp b/Libraries/LibWeb/CSS/CSSMathValue.cpp
index efcc53e2820..51f3d5207ec 100644
--- a/Libraries/LibWeb/CSS/CSSMathValue.cpp
+++ b/Libraries/LibWeb/CSS/CSSMathValue.cpp
@@ -7,6 +7,7 @@
#include "CSSMathValue.h"
#include <LibWeb/CSS/PropertyNameAndID.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
+#include <LibWeb/CSS/StyleValues/OpacityValueStyleValue.h>
namespace Web::CSS {
@@ -42,9 +43,9 @@ WebIDL::ExceptionOr<NonnullRefPtr<StyleValue const>> CSSMathValue::create_an_int
if (type().matches_length(context.percentages_resolve_as))
return property_accepts_type(property.id(), ValueType::Length);
if (type().matches_number(context.percentages_resolve_as))
- return property_accepts_type(property.id(), ValueType::Number);
+ return property_accepts_type(property.id(), ValueType::Number) || property_accepts_type(property.id(), ValueType::OpacityValue);
if (type().matches_percentage())
- return property_accepts_type(property.id(), ValueType::Percentage);
+ return property_accepts_type(property.id(), ValueType::Percentage) || property_accepts_type(property.id(), ValueType::OpacityValue);
if (type().matches_resolution(context.percentages_resolve_as))
return property_accepts_type(property.id(), ValueType::Resolution);
if (type().matches_time(context.percentages_resolve_as))
@@ -55,7 +56,13 @@ WebIDL::ExceptionOr<NonnullRefPtr<StyleValue const>> CSSMathValue::create_an_int
if (perform_type_check == PerformTypeCheck::Yes && !matches)
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Property does not accept values of this type."_utf16 };
- return CalculatedStyleValue::create(TRY(create_calculation_node(context)), type(), move(context));
+ auto calculated_value = CalculatedStyleValue::create(TRY(create_calculation_node(context)), type(), move(context));
+
+ // NB: Our parser represents an <opacity-value> as an opacity value that holds the calculation.
+ if (property_accepts_type(property.id(), ValueType::OpacityValue) && (type().matches_number({}) || type().matches_percentage()))
+ return OpacityValueStyleValue::create(move(calculated_value));
+
+ return calculated_value;
}
}
diff --git a/Libraries/LibWeb/CSS/CSSUnitValue.cpp b/Libraries/LibWeb/CSS/CSSUnitValue.cpp
index fb89dd621b4..aeff7eec692 100644
--- a/Libraries/LibWeb/CSS/CSSUnitValue.cpp
+++ b/Libraries/LibWeb/CSS/CSSUnitValue.cpp
@@ -15,6 +15,7 @@
#include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
+#include <LibWeb/CSS/StyleValues/OpacityValueStyleValue.h>
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
#include <LibWeb/CSS/StyleValues/ResolutionStyleValue.h>
#include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
@@ -383,6 +384,10 @@ WebIDL::ExceptionOr<NonnullRefPtr<StyleValue const>> CSSUnitValue::create_an_int
// 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.
if (property_accepts_type(property.id(), ValueType::Number)) {
if (property_accepts_number(property.id(), number.value()))
@@ -400,6 +405,10 @@ WebIDL::ExceptionOr<NonnullRefPtr<StyleValue const>> CSSUnitValue::create_an_int
return {};
},
[&](Percentage const& percentage) -> RefPtr<StyleValue const> {
+ // NB: Our parser represents an <opacity-value> as an opacity value that holds the percentage.
+ if (property_accepts_type(property.id(), ValueType::OpacityValue))
+ return OpacityValueStyleValue::create(PercentageStyleValue::create(percentage));
+
if (property_accepts_type(property.id(), ValueType::Percentage)) {
if (property_accepts_percentage(property.id(), percentage))
return PercentageStyleValue::create(percentage);
diff --git a/Libraries/LibWeb/CSS/StyleValues/OpacityValueStyleValue.h b/Libraries/LibWeb/CSS/StyleValues/OpacityValueStyleValue.h
index d20d239d242..9c43096f2bd 100644
--- a/Libraries/LibWeb/CSS/StyleValues/OpacityValueStyleValue.h
+++ b/Libraries/LibWeb/CSS/StyleValues/OpacityValueStyleValue.h
@@ -13,6 +13,10 @@ namespace Web::CSS {
class OpacityValueStyleValue final : public StyleValueWithDefaultOperators<OpacityValueStyleValue> {
public:
+ static ValueComparingNonnullRefPtr<OpacityValueStyleValue const> create(ValueComparingNonnullRefPtr<StyleValue const> value)
+ {
+ return adopt_ref(*new (nothrow) OpacityValueStyleValue(StyleValueFFI::rust_style_value_create_opacity_value(StyleValueFFI::rust_style_value_retain(value->rust_style_value_data()))));
+ }
virtual ~OpacityValueStyleValue() override = default;
double resolved() const { return value()->as_number().number(); }
diff --git a/Libraries/LibWeb/Rust/src/css/style_value.rs b/Libraries/LibWeb/Rust/src/css/style_value.rs
index 9d2786c2610..15bd085a5d5 100644
--- a/Libraries/LibWeb/Rust/src/css/style_value.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_value.rs
@@ -3102,6 +3102,14 @@ pub unsafe extern "C" fn rust_style_value_create_ratio(
}))
}
+/// Takes ownership of one strong reference to the number, percentage or calculated value.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn rust_style_value_create_opacity_value(value: *const StyleValueData) -> *const StyleValueData {
+ Arc::into_raw(Arc::new(StyleValueData::OpacityValue {
+ value: unsafe { RetainedStyleValueData::from_retained_pointer(value) },
+ }))
+}
+
/// Takes ownership of one strong reference to the length-percentage value.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust_style_value_create_text_indent(
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/fill-opacity.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/fill-opacity.txt
index 29537310464..1642b9c57aa 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/fill-opacity.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/fill-opacity.txt
@@ -2,21 +2,21 @@ Harness status: OK
Found 31 tests
-23 Pass
-8 Fail
+30 Pass
+1 Fail
Pass Can set 'fill-opacity' to CSS-wide keywords: initial
Pass Can set 'fill-opacity' to CSS-wide keywords: inherit
Pass Can set 'fill-opacity' to CSS-wide keywords: unset
Pass Can set 'fill-opacity' to CSS-wide keywords: revert
Pass Can set 'fill-opacity' to var() references: var(--A)
-Fail Can set 'fill-opacity' to a number: 0
-Fail Can set 'fill-opacity' to a number: -3.14
-Fail Can set 'fill-opacity' to a number: 3.14
+Pass Can set 'fill-opacity' to a number: 0
+Pass Can set 'fill-opacity' to a number: -3.14
+Pass Can set 'fill-opacity' to a number: 3.14
Fail Can set 'fill-opacity' to a number: calc(2 + 3)
-Fail Can set 'fill-opacity' to a percent: 0%
-Fail Can set 'fill-opacity' to a percent: -3.14%
-Fail Can set 'fill-opacity' to a percent: 3.14%
-Fail Can set 'fill-opacity' to a percent: calc(0% + 0%)
+Pass Can set 'fill-opacity' to a percent: 0%
+Pass Can set 'fill-opacity' to a percent: -3.14%
+Pass Can set 'fill-opacity' to a percent: 3.14%
+Pass Can set 'fill-opacity' to a percent: calc(0% + 0%)
Pass Setting 'fill-opacity' to a length: 0px throws TypeError
Pass Setting 'fill-opacity' to a length: -3.14em throws TypeError
Pass Setting 'fill-opacity' to a length: 3.14cm throws TypeError
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/flood-opacity.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/flood-opacity.txt
index ecdffe9542b..0fd23dc6a0d 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/flood-opacity.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/flood-opacity.txt
@@ -2,21 +2,21 @@ Harness status: OK
Found 31 tests
-23 Pass
-8 Fail
+30 Pass
+1 Fail
Pass Can set 'flood-opacity' to CSS-wide keywords: initial
Pass Can set 'flood-opacity' to CSS-wide keywords: inherit
Pass Can set 'flood-opacity' to CSS-wide keywords: unset
Pass Can set 'flood-opacity' to CSS-wide keywords: revert
Pass Can set 'flood-opacity' to var() references: var(--A)
-Fail Can set 'flood-opacity' to a number: 0
-Fail Can set 'flood-opacity' to a number: -3.14
-Fail Can set 'flood-opacity' to a number: 3.14
+Pass Can set 'flood-opacity' to a number: 0
+Pass Can set 'flood-opacity' to a number: -3.14
+Pass Can set 'flood-opacity' to a number: 3.14
Fail Can set 'flood-opacity' to a number: calc(2 + 3)
-Fail Can set 'flood-opacity' to a percent: 0%
-Fail Can set 'flood-opacity' to a percent: -3.14%
-Fail Can set 'flood-opacity' to a percent: 3.14%
-Fail Can set 'flood-opacity' to a percent: calc(0% + 0%)
+Pass Can set 'flood-opacity' to a percent: 0%
+Pass Can set 'flood-opacity' to a percent: -3.14%
+Pass Can set 'flood-opacity' to a percent: 3.14%
+Pass Can set 'flood-opacity' to a percent: calc(0% + 0%)
Pass Setting 'flood-opacity' to a length: 0px throws TypeError
Pass Setting 'flood-opacity' to a length: -3.14em throws TypeError
Pass Setting 'flood-opacity' to a length: 3.14cm throws TypeError
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/opacity.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/opacity.txt
index d37d67331f9..6daeff3d778 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/opacity.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/opacity.txt
@@ -2,21 +2,21 @@ Harness status: OK
Found 31 tests
-23 Pass
-8 Fail
+30 Pass
+1 Fail
Pass Can set 'opacity' to CSS-wide keywords: initial
Pass Can set 'opacity' to CSS-wide keywords: inherit
Pass Can set 'opacity' to CSS-wide keywords: unset
Pass Can set 'opacity' to CSS-wide keywords: revert
Pass Can set 'opacity' to var() references: var(--A)
-Fail Can set 'opacity' to a number: 0
-Fail Can set 'opacity' to a number: -3.14
-Fail Can set 'opacity' to a number: 3.14
+Pass Can set 'opacity' to a number: 0
+Pass Can set 'opacity' to a number: -3.14
+Pass Can set 'opacity' to a number: 3.14
Fail Can set 'opacity' to a number: calc(2 + 3)
-Fail Can set 'opacity' to a percent: 0%
-Fail Can set 'opacity' to a percent: -3.14%
-Fail Can set 'opacity' to a percent: 3.14%
-Fail Can set 'opacity' to a percent: calc(0% + 0%)
+Pass Can set 'opacity' to a percent: 0%
+Pass Can set 'opacity' to a percent: -3.14%
+Pass Can set 'opacity' to a percent: 3.14%
+Pass Can set 'opacity' to a percent: calc(0% + 0%)
Pass Setting 'opacity' to a length: 0px throws TypeError
Pass Setting 'opacity' to a length: -3.14em throws TypeError
Pass Setting 'opacity' to a length: 3.14cm throws TypeError
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/shape-image-threshold.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/shape-image-threshold.txt
index 74414614e9d..8f520e74e4e 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/shape-image-threshold.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/shape-image-threshold.txt
@@ -2,25 +2,25 @@ Harness status: OK
Found 31 tests
-27 Pass
-4 Fail
+26 Pass
+5 Fail
Pass Can set 'shape-image-threshold' to CSS-wide keywords: initial
Pass Can set 'shape-image-threshold' to CSS-wide keywords: inherit
Pass Can set 'shape-image-threshold' to CSS-wide keywords: unset
Pass Can set 'shape-image-threshold' to CSS-wide keywords: revert
Pass Can set 'shape-image-threshold' to var() references: var(--A)
-Fail Can set 'shape-image-threshold' to a number: 0
-Fail Can set 'shape-image-threshold' to a number: -3.14
-Fail Can set 'shape-image-threshold' to a number: 3.14
+Pass Can set 'shape-image-threshold' to a number: 0
+Pass Can set 'shape-image-threshold' to a number: -3.14
+Pass Can set 'shape-image-threshold' to a number: 3.14
Fail Can set 'shape-image-threshold' to a number: calc(2 + 3)
Pass Setting 'shape-image-threshold' to a length: 0px throws TypeError
Pass Setting 'shape-image-threshold' to a length: -3.14em throws TypeError
Pass Setting 'shape-image-threshold' to a length: 3.14cm throws TypeError
Pass Setting 'shape-image-threshold' to a length: calc(0px + 0em) throws TypeError
-Pass Setting 'shape-image-threshold' to a percent: 0% throws TypeError
-Pass Setting 'shape-image-threshold' to a percent: -3.14% throws TypeError
-Pass Setting 'shape-image-threshold' to a percent: 3.14% throws TypeError
-Pass Setting 'shape-image-threshold' to a percent: calc(0% + 0%) throws TypeError
+Fail Setting 'shape-image-threshold' to a percent: 0% throws TypeError
+Fail Setting 'shape-image-threshold' to a percent: -3.14% throws TypeError
+Fail Setting 'shape-image-threshold' to a percent: 3.14% throws TypeError
+Fail Setting 'shape-image-threshold' to a percent: calc(0% + 0%) throws TypeError
Pass Setting 'shape-image-threshold' to a time: 0s throws TypeError
Pass Setting 'shape-image-threshold' to a time: -3.14ms throws TypeError
Pass Setting 'shape-image-threshold' to a time: 3.14s throws TypeError
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/stop-opacity.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/stop-opacity.txt
index fdba7489f84..658a05c13ab 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/stop-opacity.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/stop-opacity.txt
@@ -2,21 +2,21 @@ Harness status: OK
Found 31 tests
-23 Pass
-8 Fail
+30 Pass
+1 Fail
Pass Can set 'stop-opacity' to CSS-wide keywords: initial
Pass Can set 'stop-opacity' to CSS-wide keywords: inherit
Pass Can set 'stop-opacity' to CSS-wide keywords: unset
Pass Can set 'stop-opacity' to CSS-wide keywords: revert
Pass Can set 'stop-opacity' to var() references: var(--A)
-Fail Can set 'stop-opacity' to a number: 0
-Fail Can set 'stop-opacity' to a number: -3.14
-Fail Can set 'stop-opacity' to a number: 3.14
+Pass Can set 'stop-opacity' to a number: 0
+Pass Can set 'stop-opacity' to a number: -3.14
+Pass Can set 'stop-opacity' to a number: 3.14
Fail Can set 'stop-opacity' to a number: calc(2 + 3)
-Fail Can set 'stop-opacity' to a percent: 0%
-Fail Can set 'stop-opacity' to a percent: -3.14%
-Fail Can set 'stop-opacity' to a percent: 3.14%
-Fail Can set 'stop-opacity' to a percent: calc(0% + 0%)
+Pass Can set 'stop-opacity' to a percent: 0%
+Pass Can set 'stop-opacity' to a percent: -3.14%
+Pass Can set 'stop-opacity' to a percent: 3.14%
+Pass Can set 'stop-opacity' to a percent: calc(0% + 0%)
Pass Setting 'stop-opacity' to a length: 0px throws TypeError
Pass Setting 'stop-opacity' to a length: -3.14em throws TypeError
Pass Setting 'stop-opacity' to a length: 3.14cm throws TypeError
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/stroke-opacity.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/stroke-opacity.txt
index 82153dd57fc..d9218da598a 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/stroke-opacity.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/properties/stroke-opacity.txt
@@ -2,21 +2,21 @@ Harness status: OK
Found 31 tests
-23 Pass
-8 Fail
+30 Pass
+1 Fail
Pass Can set 'stroke-opacity' to CSS-wide keywords: initial
Pass Can set 'stroke-opacity' to CSS-wide keywords: inherit
Pass Can set 'stroke-opacity' to CSS-wide keywords: unset
Pass Can set 'stroke-opacity' to CSS-wide keywords: revert
Pass Can set 'stroke-opacity' to var() references: var(--A)
-Fail Can set 'stroke-opacity' to a number: 0
-Fail Can set 'stroke-opacity' to a number: -3.14
-Fail Can set 'stroke-opacity' to a number: 3.14
+Pass Can set 'stroke-opacity' to a number: 0
+Pass Can set 'stroke-opacity' to a number: -3.14
+Pass Can set 'stroke-opacity' to a number: 3.14
Fail Can set 'stroke-opacity' to a number: calc(2 + 3)
-Fail Can set 'stroke-opacity' to a percent: 0%
-Fail Can set 'stroke-opacity' to a percent: -3.14%
-Fail Can set 'stroke-opacity' to a percent: 3.14%
-Fail Can set 'stroke-opacity' to a percent: calc(0% + 0%)
+Pass Can set 'stroke-opacity' to a percent: 0%
+Pass Can set 'stroke-opacity' to a percent: -3.14%
+Pass Can set 'stroke-opacity' to a percent: 3.14%
+Pass Can set 'stroke-opacity' to a percent: calc(0% + 0%)
Pass Setting 'stroke-opacity' to a length: 0px throws TypeError
Pass Setting 'stroke-opacity' to a length: -3.14em throws TypeError
Pass Setting 'stroke-opacity' to a length: 3.14cm throws TypeError