Truly independent web browser
LibWeb: Compare unresolved values by source text and component values
Callum Lawbf2a334d20be
diff --git a/Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/StyleValue.cppindex 763c04dc63e..280f9c6fc85 100644--- a/Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp+++ b/Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp@@ -332,10 +332,6 @@ bool StyleValue::equals(StyleValue const& other) const// The value of a pending-substitution value is unknown, so they never compare equal// (not even to themselves).return false;- case Type::Unresolved:- // Unresolved values compare by their normalized comparison text (and attr taint), not- // by the raw source text and cached parse state the data also carries.- return as_unresolved().equals(other);case Type::Calculated:// Calculations compare by tree structure alone; the data also carries parse-context// fields (resolved type, accepted ranges) that must not affect equality.diff --git a/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cppindex bc14bee60f4..453402d0113 100644--- a/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp+++ b/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp@@ -32,14 +32,6 @@ static StyleValueFFI::StyleValueData const* create_rust_style_value_from_source(parsed_value ? StyleValueFFI::rust_style_value_retain(parsed_value->rust_style_value_data()) : nullptr);}-Utf16String UnresolvedStyleValue::comparison_text() const-{- auto value_comparison_text = this->value_comparison_text();- if (!value_comparison_text.is_empty())- return value_comparison_text;- return source_text().trim_ascii_whitespace();-}-ValueComparingNonnullRefPtr<UnresolvedStyleValue const> UnresolvedStyleValue::create_internal(Utf16String token_source, Parser::SubstitutionFunctionsPresence substitution_presence, SourceTextMode source_text_mode, bool contains_attr_tainted_values, RefPtr<StyleValue const> parsed_value){auto* data = create_rust_style_value_from_source(token_source, substitution_presence, source_text_mode, contains_attr_tainted_values, parsed_value.ptr());@@ -57,16 +49,6 @@ ValueComparingNonnullRefPtr<UnresolvedStyleValue const> UnresolvedStyleValue::crreturn create_internal(move(token_source), substitution_presence, source_text_mode, true, move(parsed_value));}-bool UnresolvedStyleValue::equals(StyleValue const& other) const-{- if (type() != other.type())- return false;-- auto const& other_unresolved = other.as_unresolved();- return contains_attr_tainted_values() == other_unresolved.contains_attr_tainted_values()- && comparison_text() == other_unresolved.comparison_text();-}-// https://drafts.css-houdini.org/css-typed-om-1/#reify-a-list-of-component-valuesGC::Ref<CSSStyleValue> UnresolvedStyleValue::reify(Utf16FlyString const&) const{diff --git a/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.h b/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.hindex 96c1ac05e7b..02fd8b03ed8 100644--- a/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.h+++ b/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.h@@ -32,8 +32,6 @@ public:bool contains_attr_tainted_values() const { return m_value->unresolved.contains_attr_tainted_values; }RefPtr<StyleValue const> parsed_value() const { return wrap_rust_child_or_null(m_value->unresolved.parsed_value); }- bool equals(StyleValue const& other) const;-GC::Ref<CSSStyleValue> reify(Utf16FlyString const& associated_property) const;private:@@ -45,17 +43,6 @@ private:}static ValueComparingNonnullRefPtr<UnresolvedStyleValue const> create_internal(Utf16String token_source, Parser::SubstitutionFunctionsPresence, SourceTextMode, bool contains_attr_tainted_values, RefPtr<StyleValue const> parsed_value);-- Utf16String comparison_text() const;-- static Utf16String string_from_rust_data(StyleValueFFI::CssString const& string)- {- auto view = StyleValueFFI::rust_css_string_view(&string);- return Utf16String::from_utf16({ reinterpret_cast<char16_t const*>(view.data), view.length });- }-- Utf16String source_text() const { return string_from_rust_data(m_value->unresolved.source_text); }- Utf16String value_comparison_text() const { return string_from_rust_data(m_value->unresolved.value_comparison_text); }};}diff --git a/Libraries/LibWeb/Rust/src/css/style_value.rs b/Libraries/LibWeb/Rust/src/css/style_value.rsindex afc05e94872..fb6b9ba5b63 100644--- a/Libraries/LibWeb/Rust/src/css/style_value.rs+++ b/Libraries/LibWeb/Rust/src/css/style_value.rs@@ -329,10 +329,41 @@ impl Clone for RetainedComponentValueList {}impl PartialEq for RetainedComponentValueList {- fn eq(&self, _other: &Self) -> bool {- // The retained tree is a parsing/serialization cache. Unresolved value identity is- // defined by the source and comparison text fields, as it was before this cache existed.- true+ fn eq(&self, other: &Self) -> bool {+ fn components_equal(first: &[ComponentValue], second: &[ComponentValue]) -> bool {+ first.len() == second.len()+ && first+ .iter()+ .zip(second)+ .all(|(first, second)| match (&first.kind, &second.kind) {+ (ComponentKind::Token(first), ComponentKind::Token(second)) => first == second,+ (+ ComponentKind::Function {+ name: first_name,+ values: first,+ },+ ComponentKind::Function {+ name: second_name,+ values: second,+ },+ ) => first_name == second_name && components_equal(first, second),+ (+ ComponentKind::SimpleBlock {+ opening: first_opening,+ values: first,+ },+ ComponentKind::SimpleBlock {+ opening: second_opening,+ values: second,+ },+ ) => first_opening == second_opening && components_equal(first, second),+ _ => false,+ })+ }++ // Parsing retained components depends on their token kinds, even when their normalized+ // serialization is identical. Source text and positions do not affect this comparison.+ components_equal(self.as_slice(), other.as_slice())}}@@ -3694,6 +3725,24 @@ fn hash_string_units(hasher: &mut crate::css::style::fast_hash::FastHasher, unit#[cfg(test)]mod equality_tests {use super::*;+ use crate::css::css_tokenizer::tokenize_for_parser;+ use crate::css::parser::component_value::consume_a_list_of_component_values;+ use crate::css::parser::value_parser::unresolved_value;++ fn unresolved(source: &str) -> StyleValueData {+ let values = consume_a_list_of_component_values(tokenize_for_parser(source.as_bytes())).unwrap();+ let serialized = serialize_component_values_to_utf16(&values, ComponentSerializationMode::Normalized);+ unresolved_value(&serialized, &[], values, Default::default())+ }++ #[test]+ fn unresolved_equality_preserves_numeric_token_types() {+ for (first, second) in [("1", "1.0"), ("1", "1e0"), ("f(1)", "f(1.0)"), ("[1]", "[1.0]")] {+ let first = unresolved(first);+ let second = unresolved(second);+ assert!(!style_values_equal(&first, &second));+ }+ }#[test]fn text_hashes_alike_in_either_spelling() {diff --git a/Tests/LibWeb/Text/expected/css/custom-property-value-equality.txt b/Tests/LibWeb/Text/expected/css/custom-property-value-equality.txtnew file mode 100644index 00000000000..82f2d20c249--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/custom-property-value-equality.txt@@ -0,0 +1,6 @@+token kind: ["1.0","7"]+token kind: ["1","1"]+numeric spelling: ["1.0","1"]+numeric spelling: ["1e0","1"]+identifier spelling: ["a","a"]+identifier spelling: ["\\61","a"]diff --git a/Tests/LibWeb/Text/input/css/custom-property-value-equality.html b/Tests/LibWeb/Text/input/css/custom-property-value-equality.htmlnew file mode 100644index 00000000000..3212b189b94--- /dev/null+++ b/Tests/LibWeb/Text/input/css/custom-property-value-equality.html@@ -0,0 +1,28 @@+<!doctype html>+<div id="target"></div>+<script src="../include.js"></script>+<script>+ test(() => {+ for (const [name, syntax, initialValue] of [+ ["--integer", "<integer>", "7"],+ ["--number", "<number>", "9"],+ ["--identifier", "<custom-ident>", "fallback"],+ ]) {+ CSS.registerProperty({ name, syntax, inherits: false, initialValue });+ }++ for (const [label, name, sources] of [+ ["token kind", "--integer", ["1.0", "1"]],+ ["numeric spelling", "--number", ["1.0", "1e0"]],+ ["identifier spelling", "--identifier", ["a", "\\61"]],+ ]) {+ for (const source of sources) {+ target.style.setProperty(name, source);+ println(`${label}: ${JSON.stringify([+ target.style.getPropertyValue(name),+ getComputedStyle(target).getPropertyValue(name),+ ])}`);+ }+ }+ });+</script>