1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:58:12 +00:00

LibWeb: Use FlyString where possible in NamedNodeMap

We cannot port over Optional<FlyString> until the IDL generator supports
passing that through as an argument (as opposed to an Optional<String>).

Change to FlyString where possible, and resolve any fallout as a result.
This commit is contained in:
Shannon Booth 2024-01-02 21:23:53 +13:00 committed by Andreas Kling
parent 0bb0c7dac2
commit f32185420d
8 changed files with 40 additions and 45 deletions

View file

@ -107,7 +107,7 @@ void Element::visit_edges(Cell::Visitor& visitor)
}
// https://dom.spec.whatwg.org/#dom-element-getattribute
Optional<String> Element::get_attribute(StringView name) const
Optional<String> Element::get_attribute(FlyString const& name) const
{
// 1. Let attr be the result of getting an attribute given qualifiedName and this.
auto const* attribute = m_attributes->get_attribute(name);
@ -120,7 +120,7 @@ Optional<String> Element::get_attribute(StringView name) const
return attribute->value();
}
ByteString Element::deprecated_get_attribute(StringView name) const
ByteString Element::deprecated_get_attribute(FlyString const& name) const
{
auto maybe_attribute = get_attribute(name);
if (!maybe_attribute.has_value())
@ -283,20 +283,20 @@ WebIDL::ExceptionOr<JS::GCPtr<Attr>> Element::set_attribute_node_ns(Attr& attr)
}
// https://dom.spec.whatwg.org/#dom-element-removeattribute
void Element::remove_attribute(StringView name)
void Element::remove_attribute(FlyString const& name)
{
// The removeAttribute(qualifiedName) method steps are to remove an attribute given qualifiedName and this, and then return undefined.
m_attributes->remove_attribute(name);
}
// https://dom.spec.whatwg.org/#dom-element-hasattribute
bool Element::has_attribute(StringView name) const
bool Element::has_attribute(FlyString const& name) const
{
return m_attributes->get_attribute(name) != nullptr;
}
// https://dom.spec.whatwg.org/#dom-element-hasattributens
bool Element::has_attribute_ns(Optional<String> const& namespace_, StringView name) const
bool Element::has_attribute_ns(Optional<String> const& namespace_, FlyString const& name) const
{
StringView namespace_view;
if (namespace_.has_value())

View file

@ -91,18 +91,16 @@ public:
// NOTE: This is for the JS bindings
Optional<FlyString> const& namespace_uri() const { return m_qualified_name.namespace_(); }
// FIXME: This should be taking a 'FlyString const&' / 'Optional<FlyString> const&'
bool has_attribute(StringView name) const;
bool has_attribute_ns(Optional<String> const& namespace_, StringView name) const;
bool has_attribute(FlyString const& name) const;
// FIXME: This should be taking a 'Optional<FlyString> const&'
bool has_attribute_ns(Optional<String> const& namespace_, FlyString const& name) const;
bool has_attributes() const;
// FIXME: This should be taking a 'FlyString const&'
ByteString deprecated_attribute(StringView name) const { return deprecated_get_attribute(name); }
Optional<String> attribute(StringView name) const { return get_attribute(name); }
ByteString deprecated_attribute(FlyString const& name) const { return deprecated_get_attribute(name); }
Optional<String> attribute(FlyString const& name) const { return get_attribute(name); }
// FIXME: This should be taking a 'FlyString const&' / 'Optional<FlyString> const&'
Optional<String> get_attribute(StringView name) const;
ByteString deprecated_get_attribute(StringView name) const;
Optional<String> get_attribute(FlyString const& name) const;
ByteString deprecated_get_attribute(FlyString const& name) const;
String get_attribute_value(FlyString const& local_name, Optional<FlyString> const& namespace_ = {}) const;
WebIDL::ExceptionOr<void> set_attribute(FlyString const& name, String const& value);
@ -114,9 +112,7 @@ public:
WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute_node_ns(Attr&);
void append_attribute(Attr&);
// FIXME: This should take a 'FlyString cosnt&'
void remove_attribute(StringView name);
void remove_attribute(FlyString const& name);
WebIDL::ExceptionOr<bool> toggle_attribute(FlyString const& name, Optional<bool> force);
size_t attribute_list_size() const;

View file

@ -85,13 +85,13 @@ Attr const* NamedNodeMap::item(u32 index) const
}
// https://dom.spec.whatwg.org/#dom-namednodemap-getnameditem
Attr const* NamedNodeMap::get_named_item(StringView qualified_name) const
Attr const* NamedNodeMap::get_named_item(FlyString const& qualified_name) const
{
return get_attribute(qualified_name);
}
// https://dom.spec.whatwg.org/#dom-namednodemap-getnameditemns
Attr const* NamedNodeMap::get_named_item_ns(Optional<String> const& namespace_, StringView local_name) const
Attr const* NamedNodeMap::get_named_item_ns(Optional<String> const& namespace_, FlyString const& local_name) const
{
// FIXME: This conversion is quite ugly.
StringView namespace_view;
@ -114,7 +114,7 @@ WebIDL::ExceptionOr<JS::GCPtr<Attr>> NamedNodeMap::set_named_item_ns(Attr& attri
}
// https://dom.spec.whatwg.org/#dom-namednodemap-removenameditem
WebIDL::ExceptionOr<Attr const*> NamedNodeMap::remove_named_item(StringView qualified_name)
WebIDL::ExceptionOr<Attr const*> NamedNodeMap::remove_named_item(FlyString const& qualified_name)
{
// 1. Let attr be the result of removing an attribute given qualifiedName and element.
auto const* attribute = remove_attribute(qualified_name);
@ -128,7 +128,7 @@ WebIDL::ExceptionOr<Attr const*> NamedNodeMap::remove_named_item(StringView qual
}
// https://dom.spec.whatwg.org/#dom-namednodemap-removenameditemns
WebIDL::ExceptionOr<Attr const*> NamedNodeMap::remove_named_item_ns(Optional<String> const& namespace_, StringView local_name)
WebIDL::ExceptionOr<Attr const*> NamedNodeMap::remove_named_item_ns(Optional<String> const& namespace_, FlyString const& local_name)
{
// FIXME: This conversion is quite ugly.
StringView namespace_view;
@ -147,13 +147,13 @@ WebIDL::ExceptionOr<Attr const*> NamedNodeMap::remove_named_item_ns(Optional<Str
}
// https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
Attr* NamedNodeMap::get_attribute(StringView qualified_name, size_t* item_index)
Attr* NamedNodeMap::get_attribute(FlyString const& qualified_name, size_t* item_index)
{
return const_cast<Attr*>(const_cast<NamedNodeMap const*>(this)->get_attribute(qualified_name, item_index));
}
// https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
Attr const* NamedNodeMap::get_attribute(StringView qualified_name, size_t* item_index) const
Attr const* NamedNodeMap::get_attribute(FlyString const& qualified_name, size_t* item_index) const
{
if (item_index)
*item_index = 0;
@ -166,10 +166,10 @@ Attr const* NamedNodeMap::get_attribute(StringView qualified_name, size_t* item_
for (auto const& attribute : m_attributes) {
if (compare_as_lowercase) {
if (attribute->name().equals_ignoring_ascii_case(qualified_name))
return attribute.ptr();
return attribute;
} else {
if (attribute->name() == qualified_name)
return attribute.ptr();
return attribute;
}
if (item_index)
@ -197,13 +197,13 @@ Attr* NamedNodeMap::get_attribute_ns(Optional<FlyString> const& namespace_, FlyS
}
// https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace
Attr* NamedNodeMap::get_attribute_ns(StringView namespace_, StringView local_name, size_t* item_index)
Attr* NamedNodeMap::get_attribute_ns(StringView namespace_, FlyString const& local_name, size_t* item_index)
{
return const_cast<Attr*>(const_cast<NamedNodeMap const*>(this)->get_attribute_ns(namespace_, local_name, item_index));
}
// https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace
Attr const* NamedNodeMap::get_attribute_ns(StringView namespace_, StringView local_name, size_t* item_index) const
Attr const* NamedNodeMap::get_attribute_ns(StringView namespace_, FlyString const& local_name, size_t* item_index) const
{
if (item_index)
*item_index = 0;
@ -304,7 +304,7 @@ void NamedNodeMap::remove_attribute_at_index(size_t attribute_index)
}
// https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-name
Attr const* NamedNodeMap::remove_attribute(StringView qualified_name)
Attr const* NamedNodeMap::remove_attribute(FlyString const& qualified_name)
{
size_t item_index = 0;
@ -320,7 +320,7 @@ Attr const* NamedNodeMap::remove_attribute(StringView qualified_name)
}
// https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-namespace
Attr const* NamedNodeMap::remove_attribute_ns(StringView namespace_, StringView local_name)
Attr const* NamedNodeMap::remove_attribute_ns(StringView namespace_, FlyString const& local_name)
{
size_t item_index = 0;

View file

@ -35,19 +35,19 @@ public:
// Methods defined by the spec for JavaScript:
Attr const* item(u32 index) const;
Attr const* get_named_item(StringView qualified_name) const;
Attr const* get_named_item_ns(Optional<String> const& namespace_, StringView local_name) const;
Attr const* get_named_item(FlyString const& qualified_name) const;
Attr const* get_named_item_ns(Optional<String> const& namespace_, FlyString const& local_name) const;
WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_named_item(Attr& attribute);
WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_named_item_ns(Attr& attribute);
WebIDL::ExceptionOr<Attr const*> remove_named_item(StringView qualified_name);
WebIDL::ExceptionOr<Attr const*> remove_named_item_ns(Optional<String> const& namespace_, StringView local_name);
WebIDL::ExceptionOr<Attr const*> remove_named_item(FlyString const& qualified_name);
WebIDL::ExceptionOr<Attr const*> remove_named_item_ns(Optional<String> const& namespace_, FlyString const& local_name);
// Methods defined by the spec for internal use:
// FIXME: These should be taking `FlyString const&' / 'Optional<FlyString> const&'
Attr* get_attribute(StringView qualified_name, size_t* item_index = nullptr);
Attr* get_attribute_ns(StringView namespace_, StringView local_name, size_t* item_index = nullptr);
Attr const* get_attribute(StringView qualified_name, size_t* item_index = nullptr) const;
Attr const* get_attribute_ns(StringView namespace_, StringView local_name, size_t* item_index = nullptr) const;
// FIXME: These should all be taking 'Optional<FlyString> const&' for namespace instead of a StringView
Attr* get_attribute(FlyString const& qualified_name, size_t* item_index = nullptr);
Attr* get_attribute_ns(StringView namespace_, FlyString const& local_name, size_t* item_index = nullptr);
Attr const* get_attribute(FlyString const& qualified_name, size_t* item_index = nullptr) const;
Attr const* get_attribute_ns(StringView namespace_, FlyString const& local_name, size_t* item_index = nullptr) const;
WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute(Attr& attribute);
void replace_attribute(Attr& old_attribute, Attr& new_attribute, size_t old_attribute_index);
void append_attribute(Attr& attribute);
@ -55,9 +55,8 @@ public:
Attr* get_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& local_name, size_t* item_index = nullptr);
Attr const* get_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& local_name, size_t* item_index = nullptr) const;
// FIXME: This should take a 'FlyString cosnt&'
Attr const* remove_attribute(StringView qualified_name);
Attr const* remove_attribute_ns(StringView namespace_, StringView local_name);
Attr const* remove_attribute(FlyString const& qualified_name);
Attr const* remove_attribute_ns(StringView namespace_, FlyString const& local_name);
private:
explicit NamedNodeMap(Element&);

View file

@ -190,7 +190,7 @@ WebIDL::ExceptionOr<Bindings::LegacyPlatformObject::DidDeletionFail> DOMStringMa
}
// Remove an attribute by name given name and the DOMStringMap's associated element.
auto data_name = builder.to_byte_string();
auto data_name = MUST(builder.to_string());
m_associated_element->remove_attribute(data_name);
// The spec doesn't have the step. This indicates that the deletion was successful.

View file

@ -26,7 +26,7 @@ public:
virtual Optional<String> aria_level() const override
{
// TODO: aria-level = the number in the element's tag name
return get_attribute("aria-level"sv);
return get_attribute("aria-level"_fly_string);
}
private:

View file

@ -94,7 +94,7 @@ JS::GCPtr<SVGGradientElement const> SVGGradientElement::linked_gradient() const
// FIXME: This entire function is an ad-hoc hack!
// It can only resolve #<ids> in the same document.
auto link = has_attribute(AttributeNames::href) ? deprecated_get_attribute(AttributeNames::href) : deprecated_get_attribute("xlink:href"sv);
auto link = has_attribute(AttributeNames::href) ? deprecated_get_attribute(AttributeNames::href) : deprecated_get_attribute("xlink:href"_fly_string);
if (auto href = link; !href.is_empty()) {
auto url = document().parse_url(href);
auto id = url.fragment();

View file

@ -107,7 +107,7 @@ void SVGUseElement::svg_element_removed(SVGElement& svg_element)
return;
}
if (svg_element.deprecated_attribute("id"sv).matches(m_referenced_id.value())) {
if (svg_element.deprecated_attribute("id"_fly_string).matches(m_referenced_id.value())) {
shadow_root()->remove_all_children();
}
}