1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:37:35 +00:00

LibWeb: Port custom properties to FlyString

This commit is contained in:
Shannon Booth 2023-11-05 16:19:16 +13:00 committed by Andreas Kling
parent 346825bd78
commit ea2b733862
10 changed files with 31 additions and 32 deletions

View file

@ -27,12 +27,12 @@ void CSSStyleDeclaration::initialize(JS::Realm& realm)
set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSStyleDeclarationPrototype>(realm, "CSSStyleDeclaration"));
}
JS::NonnullGCPtr<PropertyOwningCSSStyleDeclaration> PropertyOwningCSSStyleDeclaration::create(JS::Realm& realm, Vector<StyleProperty> properties, HashMap<DeprecatedString, StyleProperty> custom_properties)
JS::NonnullGCPtr<PropertyOwningCSSStyleDeclaration> PropertyOwningCSSStyleDeclaration::create(JS::Realm& realm, Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties)
{
return realm.heap().allocate<PropertyOwningCSSStyleDeclaration>(realm, realm, move(properties), move(custom_properties));
}
PropertyOwningCSSStyleDeclaration::PropertyOwningCSSStyleDeclaration(JS::Realm& realm, Vector<StyleProperty> properties, HashMap<DeprecatedString, StyleProperty> custom_properties)
PropertyOwningCSSStyleDeclaration::PropertyOwningCSSStyleDeclaration(JS::Realm& realm, Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> 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> ElementInlineCSSStyleDeclaration::create(DOM::Element& element, Vector<StyleProperty> properties, HashMap<DeprecatedString, StyleProperty> custom_properties)
JS::NonnullGCPtr<ElementInlineCSSStyleDeclaration> ElementInlineCSSStyleDeclaration::create(DOM::Element& element, Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties)
{
auto& realm = element.realm();
return realm.heap().allocate<ElementInlineCSSStyleDeclaration>(realm, element, move(properties), move(custom_properties));
}
ElementInlineCSSStyleDeclaration::ElementInlineCSSStyleDeclaration(DOM::Element& element, Vector<StyleProperty> properties, HashMap<DeprecatedString, StyleProperty> custom_properties)
ElementInlineCSSStyleDeclaration::ElementInlineCSSStyleDeclaration(DOM::Element& element, Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties)
: PropertyOwningCSSStyleDeclaration(element.realm(), move(properties), move(custom_properties))
, m_element(element.make_weak_ptr<DOM::Element>())
{
@ -446,7 +446,7 @@ void PropertyOwningCSSStyleDeclaration::empty_the_declarations()
m_custom_properties.clear();
}
void PropertyOwningCSSStyleDeclaration::set_the_declarations(Vector<StyleProperty> properties, HashMap<DeprecatedString, StyleProperty> custom_properties)
void PropertyOwningCSSStyleDeclaration::set_the_declarations(Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties)
{
m_properties = move(properties);
m_custom_properties = move(custom_properties);

View file

@ -54,7 +54,7 @@ class PropertyOwningCSSStyleDeclaration : public CSSStyleDeclaration {
public:
[[nodiscard]] static JS::NonnullGCPtr<PropertyOwningCSSStyleDeclaration>
create(JS::Realm&, Vector<StyleProperty>, HashMap<DeprecatedString, StyleProperty> custom_properties);
create(JS::Realm&, Vector<StyleProperty>, HashMap<FlyString, StyleProperty> custom_properties);
virtual ~PropertyOwningCSSStyleDeclaration() override = default;
@ -67,20 +67,20 @@ public:
virtual WebIDL::ExceptionOr<String> remove_property(PropertyID) override;
Vector<StyleProperty> const& properties() const { return m_properties; }
HashMap<DeprecatedString, StyleProperty> const& custom_properties() const { return m_custom_properties; }
Optional<StyleProperty> custom_property(DeprecatedString const& custom_property_name) const { return m_custom_properties.get(custom_property_name); }
HashMap<FlyString, StyleProperty> const& custom_properties() const { return m_custom_properties; }
Optional<StyleProperty> 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<void> set_css_text(StringView) override;
protected:
PropertyOwningCSSStyleDeclaration(JS::Realm&, Vector<StyleProperty>, HashMap<DeprecatedString, StyleProperty>);
PropertyOwningCSSStyleDeclaration(JS::Realm&, Vector<StyleProperty>, HashMap<FlyString, StyleProperty>);
virtual void update_style_attribute() { }
void empty_the_declarations();
void set_the_declarations(Vector<StyleProperty> properties, HashMap<DeprecatedString, StyleProperty> custom_properties);
void set_the_declarations(Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties);
private:
bool set_a_css_declaration(PropertyID, NonnullRefPtr<StyleValue const>, Important);
@ -88,14 +88,14 @@ private:
virtual void visit_edges(Cell::Visitor&) override;
Vector<StyleProperty> m_properties;
HashMap<DeprecatedString, StyleProperty> m_custom_properties;
HashMap<FlyString, StyleProperty> m_custom_properties;
};
class ElementInlineCSSStyleDeclaration final : public PropertyOwningCSSStyleDeclaration {
WEB_PLATFORM_OBJECT(ElementInlineCSSStyleDeclaration, PropertyOwningCSSStyleDeclaration);
public:
[[nodiscard]] static JS::NonnullGCPtr<ElementInlineCSSStyleDeclaration> create(DOM::Element&, Vector<StyleProperty>, HashMap<DeprecatedString, StyleProperty> custom_properties);
[[nodiscard]] static JS::NonnullGCPtr<ElementInlineCSSStyleDeclaration> create(DOM::Element&, Vector<StyleProperty>, HashMap<FlyString, StyleProperty> custom_properties);
virtual ~ElementInlineCSSStyleDeclaration() override = default;
@ -107,7 +107,7 @@ public:
virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override;
private:
ElementInlineCSSStyleDeclaration(DOM::Element&, Vector<StyleProperty> properties, HashMap<DeprecatedString, StyleProperty> custom_properties);
ElementInlineCSSStyleDeclaration(DOM::Element&, Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties);
virtual void visit_edges(Cell::Visitor&) override;

View file

@ -19,7 +19,7 @@ public:
Declaration(FlyString name, Vector<ComponentValue> values, Important);
~Declaration();
StringView name() const { return m_name; }
FlyString const& name() const { return m_name; }
Vector<ComponentValue> const& values() const { return m_values; }
Important importance() const { return m_important; }

View file

@ -1667,15 +1667,15 @@ PropertyOwningCSSStyleDeclaration* Parser::convert_to_style_declaration(Vector<D
Optional<StyleProperty> 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<StyleValue> Parser::resolve_unresolved_style_value(DOM::Element& e
static RefPtr<StyleValue const> get_custom_property(DOM::Element const& element, Optional<CSS::Selector::PseudoElement> 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;

View file

@ -299,7 +299,7 @@ private:
struct PropertiesAndCustomProperties {
Vector<StyleProperty> properties;
HashMap<DeprecatedString, StyleProperty> custom_properties;
HashMap<FlyString, StyleProperty> custom_properties;
};
PropertiesAndCustomProperties extract_properties(Vector<DeclarationOrAtRule> const&);

View file

@ -724,7 +724,7 @@ static ErrorOr<void> cascade_custom_properties(DOM::Element& element, Optional<C
needed_capacity += inline_style->custom_properties().size();
}
HashMap<DeprecatedFlyString, StyleProperty> custom_properties;
HashMap<FlyString, StyleProperty> custom_properties;
TRY(custom_properties.try_ensure_capacity(needed_capacity));
for (auto const& matching_rule : matching_rules) {

View file

@ -6,7 +6,7 @@
#pragma once
#include <AK/DeprecatedString.h>
#include <AK/FlyString.h>
#include <LibWeb/CSS/PropertyID.h>
namespace Web::CSS {
@ -22,7 +22,7 @@ struct StyleProperty {
Important important { Important::No };
CSS::PropertyID property_id;
NonnullRefPtr<StyleValue const> value;
DeprecatedString custom_name {};
FlyString custom_name {};
};
}