LibWeb: Make CSS::string_from_property_id() return FlyString const&

This avoids costly conversions from StringView to FlyString in CSS
parsing and variable expansion.
This commit is contained in:
Andreas Kling 2024-03-15 20:32:45 +01:00
parent 1cea4e6407
commit c0e0cb86e1
6 changed files with 21 additions and 17 deletions

View file

@ -173,7 +173,7 @@ bool is_animatable_property(PropertyID);
Optional<PropertyID> property_id_from_camel_case_string(StringView);
Optional<PropertyID> property_id_from_string(StringView);
StringView string_from_property_id(PropertyID);
[[nodiscard]] FlyString const& string_from_property_id(PropertyID);
bool is_inherited_property(PropertyID);
NonnullRefPtr<StyleValue> property_initial_value(JS::Realm&, PropertyID);
@ -401,7 +401,7 @@ Optional<PropertyID> property_id_from_string(StringView string)
return {};
}
StringView string_from_property_id(PropertyID property_id) {
FlyString const& string_from_property_id(PropertyID property_id) {
switch (property_id) {
)~~~");
@ -412,14 +412,18 @@ StringView string_from_property_id(PropertyID property_id) {
member_generator.set("name", name);
member_generator.set("name:titlecase", title_casify(name));
member_generator.append(R"~~~(
case PropertyID::@name:titlecase@:
return "@name@"sv;
case PropertyID::@name:titlecase@: {
static FlyString name = "@name@"_fly_string;
return name;
}
)~~~");
});
generator.append(R"~~~(
default:
return "(invalid CSS::PropertyID)"sv;
default: {
static FlyString invalid_property_id_string = "(invalid CSS::PropertyID)"_fly_string;
return invalid_property_id_string;
}
}
}

View file

@ -56,7 +56,7 @@ String PropertyOwningCSSStyleDeclaration::item(size_t index) const
{
if (index >= m_properties.size())
return {};
return MUST(String::from_utf8(CSS::string_from_property_id(m_properties[index].property_id)));
return CSS::string_from_property_id(m_properties[index].property_id).to_string();
}
JS::NonnullGCPtr<ElementInlineCSSStyleDeclaration> ElementInlineCSSStyleDeclaration::create(DOM::Element& element, Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties)

View file

@ -7019,7 +7019,7 @@ static RefPtr<StyleValue const> get_custom_property(DOM::Element const& element,
return nullptr;
}
bool Parser::expand_variables(DOM::Element& element, Optional<Selector::PseudoElement::Type> pseudo_element, StringView property_name, HashMap<FlyString, NonnullRefPtr<PropertyDependencyNode>>& dependencies, TokenStream<ComponentValue>& source, Vector<ComponentValue>& dest)
bool Parser::expand_variables(DOM::Element& element, Optional<Selector::PseudoElement::Type> pseudo_element, FlyString const& property_name, HashMap<FlyString, NonnullRefPtr<PropertyDependencyNode>>& dependencies, TokenStream<ComponentValue>& source, Vector<ComponentValue>& dest)
{
// Arbitrary large value chosen to avoid the billion-laughs attack.
// https://www.w3.org/TR/css-variables-1/#long-variables
@ -7081,7 +7081,7 @@ bool Parser::expand_variables(DOM::Element& element, Optional<Selector::PseudoEl
// but rebuilding it every time.
if (custom_property_name == property_name)
return false;
auto parent = get_dependency_node(MUST(FlyString::from_utf8(property_name)));
auto parent = get_dependency_node(property_name);
auto child = get_dependency_node(custom_property_name);
parent->add_child(child);
if (parent->has_cycles())
@ -7109,7 +7109,7 @@ bool Parser::expand_variables(DOM::Element& element, Optional<Selector::PseudoEl
return true;
}
bool Parser::expand_unresolved_values(DOM::Element& element, StringView property_name, TokenStream<ComponentValue>& source, Vector<ComponentValue>& dest)
bool Parser::expand_unresolved_values(DOM::Element& element, FlyString const& property_name, TokenStream<ComponentValue>& source, Vector<ComponentValue>& dest)
{
while (source.has_next_token()) {
auto const& value = source.next_token();
@ -7180,7 +7180,7 @@ bool Parser::expand_unresolved_values(DOM::Element& element, StringView property
}
// https://drafts.csswg.org/css-values-5/#attr-substitution
bool Parser::substitute_attr_function(DOM::Element& element, StringView property_name, Function const& attr_function, Vector<ComponentValue>& dest)
bool Parser::substitute_attr_function(DOM::Element& element, FlyString const& property_name, Function const& attr_function, Vector<ComponentValue>& dest)
{
// First, parse the arguments to attr():
// attr() = attr( <q-name> <attr-type>? , <declaration-value>?)

View file

@ -307,9 +307,9 @@ private:
Optional<Supports::Feature> parse_supports_feature(TokenStream<ComponentValue>&);
NonnullRefPtr<StyleValue> resolve_unresolved_style_value(DOM::Element&, Optional<Selector::PseudoElement::Type>, PropertyID, UnresolvedStyleValue const&);
bool expand_variables(DOM::Element&, Optional<Selector::PseudoElement::Type>, StringView property_name, HashMap<FlyString, NonnullRefPtr<PropertyDependencyNode>>& dependencies, TokenStream<ComponentValue>& source, Vector<ComponentValue>& dest);
bool expand_unresolved_values(DOM::Element&, StringView property_name, TokenStream<ComponentValue>& source, Vector<ComponentValue>& dest);
bool substitute_attr_function(DOM::Element& element, StringView property_name, Function const& attr_function, Vector<ComponentValue>& dest);
bool expand_variables(DOM::Element&, Optional<Selector::PseudoElement::Type>, FlyString const& property_name, HashMap<FlyString, NonnullRefPtr<PropertyDependencyNode>>& dependencies, TokenStream<ComponentValue>& source, Vector<ComponentValue>& dest);
bool expand_unresolved_values(DOM::Element&, FlyString const& property_name, TokenStream<ComponentValue>& source, Vector<ComponentValue>& dest);
bool substitute_attr_function(DOM::Element& element, FlyString const& property_name, Function const& attr_function, Vector<ComponentValue>& dest);
static bool has_ignored_vendor_prefix(StringView);

View file

@ -79,7 +79,7 @@ String ResolvedCSSStyleDeclaration::item(size_t index) const
if (index > length())
return {};
auto property_id = static_cast<PropertyID>(index + to_underlying(first_longhand_property_id));
return MUST(String::from_utf8(string_from_property_id(property_id)));
return string_from_property_id(property_id).to_string();
}
static NonnullRefPtr<StyleValue const> style_value_for_background_property(Layout::NodeWithStyle const& layout_node, Function<NonnullRefPtr<StyleValue const>(BackgroundLayerData const&)> callback, Function<NonnullRefPtr<StyleValue const>()> default_value)

View file

@ -384,12 +384,12 @@ void dump_tree(StringBuilder& builder, Layout::Node const& layout_node, bool sho
if (show_specified_style && layout_node.dom_node() && layout_node.dom_node()->is_element() && verify_cast<DOM::Element>(layout_node.dom_node())->computed_css_values()) {
struct NameAndValue {
String name;
FlyString name;
String value;
};
Vector<NameAndValue> properties;
verify_cast<DOM::Element>(*layout_node.dom_node()).computed_css_values()->for_each_property([&](auto property_id, auto& value) {
properties.append({ MUST(String::from_utf8(CSS::string_from_property_id(property_id))), value.to_string() });
properties.append({ CSS::string_from_property_id(property_id), value.to_string() });
});
quick_sort(properties, [](auto& a, auto& b) { return a.name < b.name; });