1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:27:43 +00:00

LibWeb: Port StyleComputer to new Strings

This commit is contained in:
Sam Atkins 2023-02-17 14:19:16 +00:00 committed by Linus Groh
parent 1c77867c78
commit 6cc5e09c71
2 changed files with 23 additions and 23 deletions

View file

@ -155,14 +155,14 @@ Vector<MatchingRule> StyleComputer::collect_matching_rules(DOM::Element const& e
rules_to_run.extend(it->value); rules_to_run.extend(it->value);
} else { } else {
for (auto const& class_name : element.class_names()) { for (auto const& class_name : element.class_names()) {
if (auto it = m_rule_cache->rules_by_class.find(class_name); it != m_rule_cache->rules_by_class.end()) if (auto it = m_rule_cache->rules_by_class.find(FlyString::from_utf8(class_name).release_value_but_fixme_should_propagate_errors()); it != m_rule_cache->rules_by_class.end())
rules_to_run.extend(it->value); rules_to_run.extend(it->value);
} }
if (auto id = element.get_attribute(HTML::AttributeNames::id); !id.is_null()) { if (auto id = element.get_attribute(HTML::AttributeNames::id); !id.is_null()) {
if (auto it = m_rule_cache->rules_by_id.find(id); it != m_rule_cache->rules_by_id.end()) if (auto it = m_rule_cache->rules_by_id.find(FlyString::from_utf8(id).release_value_but_fixme_should_propagate_errors()); it != m_rule_cache->rules_by_id.end())
rules_to_run.extend(it->value); rules_to_run.extend(it->value);
} }
if (auto it = m_rule_cache->rules_by_tag_name.find(element.local_name()); it != m_rule_cache->rules_by_tag_name.end()) if (auto it = m_rule_cache->rules_by_tag_name.find(FlyString::from_utf8(element.local_name()).release_value_but_fixme_should_propagate_errors()); it != m_rule_cache->rules_by_tag_name.end())
rules_to_run.extend(it->value); rules_to_run.extend(it->value);
rules_to_run.extend(m_rule_cache->other_rules); rules_to_run.extend(m_rule_cache->other_rules);
} }
@ -587,16 +587,16 @@ static void set_property_expanding_shorthands(StyleProperties& style, CSS::Prope
style.set_property(property_id, value); style.set_property(property_id, value);
} }
static RefPtr<StyleValue> get_custom_property(DOM::Element const& element, DeprecatedFlyString const& custom_property_name) static RefPtr<StyleValue> get_custom_property(DOM::Element const& element, FlyString const& custom_property_name)
{ {
for (auto const* current_element = &element; current_element; current_element = current_element->parent_element()) { 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); it != current_element->custom_properties().end()) if (auto it = current_element->custom_properties().find(custom_property_name.to_string().to_deprecated_string()); it != current_element->custom_properties().end())
return it->value.value; return it->value.value;
} }
return nullptr; return nullptr;
} }
bool StyleComputer::expand_variables(DOM::Element& element, StringView property_name, HashMap<DeprecatedFlyString, NonnullRefPtr<PropertyDependencyNode>>& dependencies, Parser::TokenStream<Parser::ComponentValue>& source, Vector<Parser::ComponentValue>& dest) const bool StyleComputer::expand_variables(DOM::Element& element, StringView property_name, HashMap<FlyString, NonnullRefPtr<PropertyDependencyNode>>& dependencies, Parser::TokenStream<Parser::ComponentValue>& source, Vector<Parser::ComponentValue>& dest) const
{ {
// Arbitrary large value chosen to avoid the billion-laughs attack. // Arbitrary large value chosen to avoid the billion-laughs attack.
// https://www.w3.org/TR/css-variables-1/#long-variables // https://www.w3.org/TR/css-variables-1/#long-variables
@ -606,10 +606,10 @@ bool StyleComputer::expand_variables(DOM::Element& element, StringView property_
return false; return false;
} }
auto get_dependency_node = [&](auto name) -> NonnullRefPtr<PropertyDependencyNode> { auto get_dependency_node = [&](FlyString name) -> NonnullRefPtr<PropertyDependencyNode> {
if (auto existing = dependencies.get(name); existing.has_value()) if (auto existing = dependencies.get(name); existing.has_value())
return *existing.value(); return *existing.value();
auto new_node = PropertyDependencyNode::create(name); auto new_node = PropertyDependencyNode::create(name.to_string());
dependencies.set(name, new_node); dependencies.set(name, new_node);
return new_node; return new_node;
}; };
@ -648,13 +648,13 @@ bool StyleComputer::expand_variables(DOM::Element& element, StringView property_
// but rebuilding it every time. // but rebuilding it every time.
if (custom_property_name == property_name) if (custom_property_name == property_name)
return false; return false;
auto parent = get_dependency_node(property_name); auto parent = get_dependency_node(FlyString::from_utf8(property_name).release_value_but_fixme_should_propagate_errors());
auto child = get_dependency_node(custom_property_name); auto child = get_dependency_node(FlyString::from_utf8(custom_property_name).release_value_but_fixme_should_propagate_errors());
parent->add_child(child); parent->add_child(child);
if (parent->has_cycles()) if (parent->has_cycles())
return false; return false;
if (auto custom_property_value = get_custom_property(element, custom_property_name)) { if (auto custom_property_value = get_custom_property(element, FlyString::from_utf8(custom_property_name).release_value_but_fixme_should_propagate_errors())) {
VERIFY(custom_property_value->is_unresolved()); VERIFY(custom_property_value->is_unresolved());
Parser::TokenStream custom_property_tokens { custom_property_value->as_unresolved().values() }; Parser::TokenStream custom_property_tokens { custom_property_value->as_unresolved().values() };
if (!expand_variables(element, custom_property_name, dependencies, custom_property_tokens, dest)) if (!expand_variables(element, custom_property_name, dependencies, custom_property_tokens, dest))
@ -778,7 +778,7 @@ RefPtr<StyleValue> StyleComputer::resolve_unresolved_style_value(DOM::Element& e
Parser::TokenStream unresolved_values_without_variables_expanded { unresolved.values() }; Parser::TokenStream unresolved_values_without_variables_expanded { unresolved.values() };
Vector<Parser::ComponentValue> values_with_variables_expanded; Vector<Parser::ComponentValue> values_with_variables_expanded;
HashMap<DeprecatedFlyString, NonnullRefPtr<PropertyDependencyNode>> dependencies; HashMap<FlyString, NonnullRefPtr<PropertyDependencyNode>> dependencies;
if (!expand_variables(element, string_from_property_id(property_id), dependencies, unresolved_values_without_variables_expanded, values_with_variables_expanded)) if (!expand_variables(element, string_from_property_id(property_id), dependencies, unresolved_values_without_variables_expanded, values_with_variables_expanded))
return {}; return {};
@ -1398,7 +1398,7 @@ ErrorOr<NonnullRefPtr<StyleProperties>> StyleComputer::compute_style(DOM::Elemen
return style; return style;
} }
PropertyDependencyNode::PropertyDependencyNode(DeprecatedString name) PropertyDependencyNode::PropertyDependencyNode(String name)
: m_name(move(name)) : m_name(move(name))
{ {
} }
@ -1467,19 +1467,19 @@ void StyleComputer::build_rule_cache()
if (!added_to_bucket) { if (!added_to_bucket) {
for (auto const& simple_selector : selector.compound_selectors().last().simple_selectors) { for (auto const& simple_selector : selector.compound_selectors().last().simple_selectors) {
if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Id) { if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Id) {
m_rule_cache->rules_by_id.ensure(simple_selector.name().to_string().to_deprecated_string()).append(move(matching_rule)); m_rule_cache->rules_by_id.ensure(simple_selector.name()).append(move(matching_rule));
++num_id_rules; ++num_id_rules;
added_to_bucket = true; added_to_bucket = true;
break; break;
} }
if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Class) { if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Class) {
m_rule_cache->rules_by_class.ensure(simple_selector.name().to_string().to_deprecated_string()).append(move(matching_rule)); m_rule_cache->rules_by_class.ensure(simple_selector.name()).append(move(matching_rule));
++num_class_rules; ++num_class_rules;
added_to_bucket = true; added_to_bucket = true;
break; break;
} }
if (simple_selector.type == CSS::Selector::SimpleSelector::Type::TagName) { if (simple_selector.type == CSS::Selector::SimpleSelector::Type::TagName) {
m_rule_cache->rules_by_tag_name.ensure(simple_selector.name().to_string().to_deprecated_string()).append(move(matching_rule)); m_rule_cache->rules_by_tag_name.ensure(simple_selector.name()).append(move(matching_rule));
++num_tag_name_rules; ++num_tag_name_rules;
added_to_bucket = true; added_to_bucket = true;
break; break;

View file

@ -31,7 +31,7 @@ struct MatchingRule {
class PropertyDependencyNode : public RefCounted<PropertyDependencyNode> { class PropertyDependencyNode : public RefCounted<PropertyDependencyNode> {
public: public:
static NonnullRefPtr<PropertyDependencyNode> create(DeprecatedString name) static NonnullRefPtr<PropertyDependencyNode> create(String name)
{ {
return adopt_ref(*new PropertyDependencyNode(move(name))); return adopt_ref(*new PropertyDependencyNode(move(name)));
} }
@ -40,9 +40,9 @@ public:
bool has_cycles(); bool has_cycles();
private: private:
explicit PropertyDependencyNode(DeprecatedString name); explicit PropertyDependencyNode(String name);
DeprecatedString m_name; String m_name;
NonnullRefPtrVector<PropertyDependencyNode> m_children; NonnullRefPtrVector<PropertyDependencyNode> m_children;
bool m_marked { false }; bool m_marked { false };
}; };
@ -87,7 +87,7 @@ private:
void compute_defaulted_property_value(StyleProperties&, DOM::Element const*, CSS::PropertyID, Optional<CSS::Selector::PseudoElement>) const; void compute_defaulted_property_value(StyleProperties&, DOM::Element const*, CSS::PropertyID, Optional<CSS::Selector::PseudoElement>) const;
RefPtr<StyleValue> resolve_unresolved_style_value(DOM::Element&, PropertyID, UnresolvedStyleValue const&) const; RefPtr<StyleValue> resolve_unresolved_style_value(DOM::Element&, PropertyID, UnresolvedStyleValue const&) const;
bool expand_variables(DOM::Element&, StringView property_name, HashMap<DeprecatedFlyString, NonnullRefPtr<PropertyDependencyNode>>& dependencies, Parser::TokenStream<Parser::ComponentValue>& source, Vector<Parser::ComponentValue>& dest) const; bool expand_variables(DOM::Element&, StringView property_name, HashMap<FlyString, NonnullRefPtr<PropertyDependencyNode>>& dependencies, Parser::TokenStream<Parser::ComponentValue>& source, Vector<Parser::ComponentValue>& dest) const;
bool expand_unresolved_values(DOM::Element&, StringView property_name, Parser::TokenStream<Parser::ComponentValue>& source, Vector<Parser::ComponentValue>& dest) const; bool expand_unresolved_values(DOM::Element&, StringView property_name, Parser::TokenStream<Parser::ComponentValue>& source, Vector<Parser::ComponentValue>& dest) const;
template<typename Callback> template<typename Callback>
@ -109,9 +109,9 @@ private:
DOM::Document& m_document; DOM::Document& m_document;
struct RuleCache { struct RuleCache {
HashMap<DeprecatedFlyString, Vector<MatchingRule>> rules_by_id; HashMap<FlyString, Vector<MatchingRule>> rules_by_id;
HashMap<DeprecatedFlyString, Vector<MatchingRule>> rules_by_class; HashMap<FlyString, Vector<MatchingRule>> rules_by_class;
HashMap<DeprecatedFlyString, Vector<MatchingRule>> rules_by_tag_name; HashMap<FlyString, Vector<MatchingRule>> rules_by_tag_name;
HashMap<Selector::PseudoElement, Vector<MatchingRule>> rules_by_pseudo_element; HashMap<Selector::PseudoElement, Vector<MatchingRule>> rules_by_pseudo_element;
Vector<MatchingRule> other_rules; Vector<MatchingRule> other_rules;
}; };