Truly independent web browser
LibWeb: Enumerate computed custom properties
Andreas Kling9f3e60d51221
diff --git a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp b/Libraries/LibWeb/CSS/CSSStyleProperties.cppindex 5f3f107213e..87ac468a850 100644--- a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp+++ b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp@@ -101,9 +101,12 @@ CSSStyleProperties::CSSStyleProperties(Computed computed, Readonly readonly, Russet_owner_node(move(owner_node));}+CSSStyleProperties::~CSSStyleProperties() = default;+size_t CSSStyleProperties::external_memory_size() const{- return JS::saturating_add_external_memory_size(Base::external_memory_size(), m_declarations.external_memory_size());+ auto size = JS::saturating_add_external_memory_size(Base::external_memory_size(), m_declarations.external_memory_size());+ return JS::saturating_add_external_memory_size(size, m_cached_computed_custom_property_names.capacity() * sizeof(Utf16FlyString));}// https://drafts.csswg.org/cssom/#dom-window-getcomputedstyle@@ -122,6 +125,40 @@ static bool element_exposes_computed_style(DOM::Element const& element)return true;}+Vector<Utf16FlyString> const& CSSStyleProperties::computed_custom_property_names() const+{+ auto const& element = *owner_node();+ auto& document = element.document();+ document.update_style_for_element(element);+ auto data = element.custom_property_data();+ auto registration_generation = document.custom_property_registration_generation();+ if (m_cached_computed_custom_property_document.ptr().ptr() == &document+ && m_cached_computed_custom_property_data == data+ && m_cached_custom_property_registration_generation == registration_generation)+ return m_cached_computed_custom_property_names;++ HashTable<Utf16FlyString> names;+ if (data) {+ data->for_each_property([&](Utf16FlyString const& name, StyleProperty const& property) {+ if (!property.value->is_guaranteed_invalid())+ names.set(name);+ });+ }+ document.for_each_registered_custom_property([&](CustomPropertyRegistration const& registration) {+ if (registration.initial_value && !registration.initial_value->is_guaranteed_invalid())+ names.set(registration.property_name);+ });+ m_cached_computed_custom_property_names.clear_with_capacity();+ m_cached_computed_custom_property_names.ensure_capacity(names.size());+ for (auto const& name : names)+ m_cached_computed_custom_property_names.unchecked_append(name);+ quick_sort(m_cached_computed_custom_property_names);+ m_cached_computed_custom_property_document = document;+ m_cached_computed_custom_property_data = move(data);+ m_cached_custom_property_registration_generation = registration_generation;+ return m_cached_computed_custom_property_names;+}+// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-lengthsize_t CSSStyleProperties::length() const{@@ -131,7 +168,7 @@ size_t CSSStyleProperties::length() constreturn 0;if (!element_exposes_computed_style(owner_node()->element()))return 0;- return number_of_longhand_properties;+ return number_of_longhand_properties + computed_custom_property_names().size();}return properties().size() + custom_properties().size();@@ -147,6 +184,8 @@ Utf16String CSSStyleProperties::item(size_t index) constreturn {};if (is_computed()) {+ if (index >= number_of_longhand_properties)+ return computed_custom_property_names()[index - number_of_longhand_properties].to_utf16_string();auto property_id = static_cast<PropertyID>(index + to_underlying(first_longhand_property_id));return string_from_property_id(property_id).to_utf16_string();}diff --git a/Libraries/LibWeb/CSS/CSSStyleProperties.h b/Libraries/LibWeb/CSS/CSSStyleProperties.hindex f8202f2a80f..dc86ea68f03 100644--- a/Libraries/LibWeb/CSS/CSSStyleProperties.h+++ b/Libraries/LibWeb/CSS/CSSStyleProperties.h@@ -10,6 +10,7 @@#include <AK/OwnPtr.h>#include <AK/Utf16String.h>#include <AK/Utf16View.h>+#include <LibGC/Weak.h>#include <LibWeb/CSS/CSSStyleDeclaration.h>#include <LibWeb/CSS/GeneratedCSSStyleProperties.h>#include <LibWeb/CSS/RustDeclarationBlock.h>@@ -31,7 +32,7 @@ public:[[nodiscard]] static GC::Ref<CSSStyleProperties> create_element_inline_style(DOM::AbstractElement);[[nodiscard]] static GC::Ref<CSSStyleProperties> create_element_inline_style(DOM::AbstractElement, RustDeclarationBlock);- virtual ~CSSStyleProperties() override = default;+ virtual ~CSSStyleProperties() override;virtual size_t length() const override;virtual Utf16String item(size_t index) const override;@@ -91,7 +92,13 @@ private:void invalidate_owners();+ Vector<Utf16FlyString> const& computed_custom_property_names() const;+RustDeclarationBlock m_declarations;+ mutable Vector<Utf16FlyString> m_cached_computed_custom_property_names;+ mutable GC::Weak<DOM::Document> m_cached_computed_custom_property_document;+ mutable RefPtr<CustomPropertyData const> m_cached_computed_custom_property_data;+ mutable u64 m_cached_custom_property_registration_generation { 0 };};#undef ENUMERATE_GENERATED_CSS_STYLE_PROPERTIESdiff --git a/Tests/LibWeb/Text/expected/css/computed-custom-property-enumeration-is-live.txt b/Tests/LibWeb/Text/expected/css/computed-custom-property-enumeration-is-live.txtindex 9214dc44ceb..269f94943f2 100644--- a/Tests/LibWeb/Text/expected/css/computed-custom-property-enumeration-is-live.txt+++ b/Tests/LibWeb/Text/expected/css/computed-custom-property-enumeration-is-live.txt@@ -1,5 +1,5 @@-initial: ""-specified: ""-registered: ""-stylesheet: ""-removed: ""+initial: "--sheet-initial"+specified: "--dynamic, --sheet-initial"+registered: "--api-initial, --dynamic, --sheet-initial"+stylesheet: "--api-initial, --dynamic, --replacement-initial"+removed: "--api-initial, --replacement-initial"diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/get-computed-style-enumeration.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/get-computed-style-enumeration.txtindex db36b59b8ca..540a5b358f7 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/get-computed-style-enumeration.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/get-computed-style-enumeration.txt@@ -2,10 +2,9 @@ Harness status: OKFound 5 tests-2 Pass-3 Fail-Fail Custom properties specified on the node exposed when enumerating computed style.-Fail Inherited custom properties specified on the parent exposed when enumerating computed style.-Fail Unspecified properties with initial values exposed when enumerating computed style.+5 Pass+Pass Custom properties specified on the node exposed when enumerating computed style.+Pass Inherited custom properties specified on the parent exposed when enumerating computed style.+Pass Unspecified properties with initial values exposed when enumerating computed style.Pass Non-inherited custom properties specified on the parent without initial values not exposed when enumerating computed style.Pass Unspecified properties without initial values not exposed when enumerating computed style.\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/computed/computed.tentative.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/computed/computed.tentative.txtindex fd1e514d1f7..93d0fb35ae3 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/computed/computed.tentative.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-typed-om/the-stylepropertymap/computed/computed.tentative.txt@@ -2,9 +2,8 @@ Harness status: OKFound 7 tests-6 Pass-1 Fail-Fail Computed StylePropertyMap contains every CSS property+7 Pass+Pass Computed StylePropertyMap contains every CSS propertyPass Computed StylePropertyMap contains CSS property declarations in style rulesPass Computed StylePropertyMap contains custom property declarations in style rulesPass Computed StylePropertyMap contains CSS property declarations in inline styles