From ea2b733862215b35a3192989b6633fe0b73bd4e0 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 5 Nov 2023 16:19:16 +1300 Subject: [PATCH] LibWeb: Port custom properties to FlyString --- .../Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp | 10 +++++----- .../Libraries/LibWeb/CSS/CSSStyleDeclaration.h | 16 ++++++++-------- .../Libraries/LibWeb/CSS/Parser/Declaration.h | 2 +- Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 10 +++++----- Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 2 +- Userland/Libraries/LibWeb/CSS/StyleComputer.cpp | 2 +- Userland/Libraries/LibWeb/CSS/StyleProperty.h | 4 ++-- Userland/Libraries/LibWeb/DOM/Element.cpp | 4 ++-- Userland/Libraries/LibWeb/DOM/Element.h | 9 ++++----- .../Services/WebContent/ConnectionFromClient.cpp | 4 ++-- 10 files changed, 31 insertions(+), 32 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp index b45064a339..660dbabf78 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp @@ -27,12 +27,12 @@ void CSSStyleDeclaration::initialize(JS::Realm& realm) set_prototype(&Bindings::ensure_web_prototype(realm, "CSSStyleDeclaration")); } -JS::NonnullGCPtr PropertyOwningCSSStyleDeclaration::create(JS::Realm& realm, Vector properties, HashMap custom_properties) +JS::NonnullGCPtr PropertyOwningCSSStyleDeclaration::create(JS::Realm& realm, Vector properties, HashMap custom_properties) { return realm.heap().allocate(realm, realm, move(properties), move(custom_properties)); } -PropertyOwningCSSStyleDeclaration::PropertyOwningCSSStyleDeclaration(JS::Realm& realm, Vector properties, HashMap custom_properties) +PropertyOwningCSSStyleDeclaration::PropertyOwningCSSStyleDeclaration(JS::Realm& realm, Vector properties, HashMap custom_properties) : CSSStyleDeclaration(realm) , m_properties(move(properties)) , m_custom_properties(move(custom_properties)) @@ -55,13 +55,13 @@ String PropertyOwningCSSStyleDeclaration::item(size_t index) const return MUST(String::from_utf8(CSS::string_from_property_id(m_properties[index].property_id))); } -JS::NonnullGCPtr ElementInlineCSSStyleDeclaration::create(DOM::Element& element, Vector properties, HashMap custom_properties) +JS::NonnullGCPtr ElementInlineCSSStyleDeclaration::create(DOM::Element& element, Vector properties, HashMap custom_properties) { auto& realm = element.realm(); return realm.heap().allocate(realm, element, move(properties), move(custom_properties)); } -ElementInlineCSSStyleDeclaration::ElementInlineCSSStyleDeclaration(DOM::Element& element, Vector properties, HashMap custom_properties) +ElementInlineCSSStyleDeclaration::ElementInlineCSSStyleDeclaration(DOM::Element& element, Vector properties, HashMap custom_properties) : PropertyOwningCSSStyleDeclaration(element.realm(), move(properties), move(custom_properties)) , m_element(element.make_weak_ptr()) { @@ -446,7 +446,7 @@ void PropertyOwningCSSStyleDeclaration::empty_the_declarations() m_custom_properties.clear(); } -void PropertyOwningCSSStyleDeclaration::set_the_declarations(Vector properties, HashMap custom_properties) +void PropertyOwningCSSStyleDeclaration::set_the_declarations(Vector properties, HashMap custom_properties) { m_properties = move(properties); m_custom_properties = move(custom_properties); diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h index 25b0b2e071..f5e8b127da 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h @@ -54,7 +54,7 @@ class PropertyOwningCSSStyleDeclaration : public CSSStyleDeclaration { public: [[nodiscard]] static JS::NonnullGCPtr - create(JS::Realm&, Vector, HashMap custom_properties); + create(JS::Realm&, Vector, HashMap custom_properties); virtual ~PropertyOwningCSSStyleDeclaration() override = default; @@ -67,20 +67,20 @@ public: virtual WebIDL::ExceptionOr remove_property(PropertyID) override; Vector const& properties() const { return m_properties; } - HashMap const& custom_properties() const { return m_custom_properties; } - Optional custom_property(DeprecatedString const& custom_property_name) const { return m_custom_properties.get(custom_property_name); } + HashMap const& custom_properties() const { return m_custom_properties; } + Optional custom_property(FlyString const& custom_property_name) const { return m_custom_properties.get(custom_property_name); } size_t custom_property_count() const { return m_custom_properties.size(); } virtual DeprecatedString serialized() const final override; virtual WebIDL::ExceptionOr set_css_text(StringView) override; protected: - PropertyOwningCSSStyleDeclaration(JS::Realm&, Vector, HashMap); + PropertyOwningCSSStyleDeclaration(JS::Realm&, Vector, HashMap); virtual void update_style_attribute() { } void empty_the_declarations(); - void set_the_declarations(Vector properties, HashMap custom_properties); + void set_the_declarations(Vector properties, HashMap custom_properties); private: bool set_a_css_declaration(PropertyID, NonnullRefPtr, Important); @@ -88,14 +88,14 @@ private: virtual void visit_edges(Cell::Visitor&) override; Vector m_properties; - HashMap m_custom_properties; + HashMap m_custom_properties; }; class ElementInlineCSSStyleDeclaration final : public PropertyOwningCSSStyleDeclaration { WEB_PLATFORM_OBJECT(ElementInlineCSSStyleDeclaration, PropertyOwningCSSStyleDeclaration); public: - [[nodiscard]] static JS::NonnullGCPtr create(DOM::Element&, Vector, HashMap custom_properties); + [[nodiscard]] static JS::NonnullGCPtr create(DOM::Element&, Vector, HashMap custom_properties); virtual ~ElementInlineCSSStyleDeclaration() override = default; @@ -107,7 +107,7 @@ public: virtual WebIDL::ExceptionOr set_css_text(StringView) override; private: - ElementInlineCSSStyleDeclaration(DOM::Element&, Vector properties, HashMap custom_properties); + ElementInlineCSSStyleDeclaration(DOM::Element&, Vector properties, HashMap custom_properties); virtual void visit_edges(Cell::Visitor&) override; diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Declaration.h b/Userland/Libraries/LibWeb/CSS/Parser/Declaration.h index 0a313a4157..8f0c04e396 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Declaration.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Declaration.h @@ -19,7 +19,7 @@ public: Declaration(FlyString name, Vector values, Important); ~Declaration(); - StringView name() const { return m_name; } + FlyString const& name() const { return m_name; } Vector const& values() const { return m_values; } Important importance() const { return m_important; } diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 0136a9b3cd..a9cb8fb9b6 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -1667,15 +1667,15 @@ PropertyOwningCSSStyleDeclaration* Parser::convert_to_style_declaration(Vector Parser::convert_to_style_property(Declaration const& declaration) { - auto property_name = declaration.name(); + auto const& property_name = declaration.name(); auto property_id = property_id_from_string(property_name); if (!property_id.has_value()) { - if (property_name.starts_with("--"sv)) { + if (property_name.bytes_as_string_view().starts_with("--"sv)) { property_id = PropertyID::Custom; } else if (has_ignored_vendor_prefix(property_name)) { return {}; - } else if (!property_name.starts_with('-')) { + } else if (!property_name.bytes_as_string_view().starts_with('-')) { dbgln_if(CSS_PARSER_DEBUG, "Unrecognized CSS property '{}'", property_name); return {}; } @@ -6735,12 +6735,12 @@ NonnullRefPtr Parser::resolve_unresolved_style_value(DOM::Element& e static RefPtr get_custom_property(DOM::Element const& element, Optional pseudo_element, FlyString const& custom_property_name) { if (pseudo_element.has_value()) { - if (auto it = element.custom_properties(pseudo_element).find(custom_property_name.to_string().to_deprecated_string()); it != element.custom_properties(pseudo_element).end()) + if (auto it = element.custom_properties(pseudo_element).find(custom_property_name.to_string()); it != element.custom_properties(pseudo_element).end()) return it->value.value; } for (auto const* current_element = &element; current_element; current_element = current_element->parent_element()) { - if (auto it = current_element->custom_properties({}).find(custom_property_name.to_string().to_deprecated_string()); it != current_element->custom_properties({}).end()) + if (auto it = current_element->custom_properties({}).find(custom_property_name.to_string()); it != current_element->custom_properties({}).end()) return it->value.value; } return nullptr; diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 5886ad8cd4..b05ef64de4 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -299,7 +299,7 @@ private: struct PropertiesAndCustomProperties { Vector properties; - HashMap custom_properties; + HashMap custom_properties; }; PropertiesAndCustomProperties extract_properties(Vector const&); diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index 5c21b856bc..d0e376c91c 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -724,7 +724,7 @@ static ErrorOr cascade_custom_properties(DOM::Element& element, Optionalcustom_properties().size(); } - HashMap custom_properties; + HashMap custom_properties; TRY(custom_properties.try_ensure_capacity(needed_capacity)); for (auto const& matching_rule : matching_rules) { diff --git a/Userland/Libraries/LibWeb/CSS/StyleProperty.h b/Userland/Libraries/LibWeb/CSS/StyleProperty.h index 8372325104..76377bb60e 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleProperty.h +++ b/Userland/Libraries/LibWeb/CSS/StyleProperty.h @@ -6,7 +6,7 @@ #pragma once -#include +#include #include namespace Web::CSS { @@ -22,7 +22,7 @@ struct StyleProperty { Important important { Important::No }; CSS::PropertyID property_id; NonnullRefPtr value; - DeprecatedString custom_name {}; + FlyString custom_name {}; }; } diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 6ac6a30a36..c2ba81f298 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -1918,7 +1918,7 @@ void Element::set_computed_css_values(RefPtr style) m_computed_css_values = move(style); } -void Element::set_custom_properties(Optional pseudo_element, HashMap custom_properties) +void Element::set_custom_properties(Optional pseudo_element, HashMap custom_properties) { if (!pseudo_element.has_value()) { m_custom_properties = move(custom_properties); @@ -1927,7 +1927,7 @@ void Element::set_custom_properties(Optional pseud m_pseudo_element_custom_properties[to_underlying(pseudo_element.value())] = move(custom_properties); } -HashMap const& Element::custom_properties(Optional pseudo_element) const +HashMap const& Element::custom_properties(Optional pseudo_element) const { if (!pseudo_element.has_value()) return m_custom_properties; diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index bcbda3d40e..f40114b7bf 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -6,7 +6,6 @@ #pragma once -#include #include #include #include @@ -205,8 +204,8 @@ public: ShadowRoot const* shadow_root_internal() const { return m_shadow_root.ptr(); } void set_shadow_root(JS::GCPtr); - void set_custom_properties(Optional, HashMap custom_properties); - [[nodiscard]] HashMap const& custom_properties(Optional) const; + void set_custom_properties(Optional, HashMap custom_properties); + [[nodiscard]] HashMap const& custom_properties(Optional) const; int queue_an_element_task(HTML::Task::Source, JS::SafeFunction); @@ -400,8 +399,8 @@ private: JS::GCPtr m_shadow_root; RefPtr m_computed_css_values; - HashMap m_custom_properties; - Array, to_underlying(CSS::Selector::PseudoElement::PseudoElementCount)> m_pseudo_element_custom_properties; + HashMap m_custom_properties; + Array, to_underlying(CSS::Selector::PseudoElement::PseudoElementCount)> m_pseudo_element_custom_properties; Vector m_classes; Optional m_dir; diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index 763e0b501a..5a0f35c72b 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -545,14 +545,14 @@ Messages::WebContentServer::InspectDomNodeResponse ConnectionFromClient::inspect auto serialize_custom_properties_json = [](Web::DOM::Element const& element, Optional pseudo_element) -> DeprecatedString { StringBuilder builder; auto serializer = MUST(JsonObjectSerializer<>::try_create(builder)); - HashTable seen_properties; + HashTable seen_properties; auto const* element_to_check = &element; while (element_to_check) { for (auto const& property : element_to_check->custom_properties(pseudo_element)) { if (!seen_properties.contains(property.key)) { seen_properties.set(property.key); - MUST(serializer.add(property.key, property.value.value->to_string().to_deprecated_string())); + MUST(serializer.add(property.key, property.value.value->to_string())); } }