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

LibWeb: Port Element::name to a cached FlyString

Also removing some handling of OOM while we are in the area.
This commit is contained in:
Shannon Booth 2024-01-13 20:12:23 +13:00 committed by Andreas Kling
parent cb9118efe3
commit 41f84deb9f
6 changed files with 22 additions and 24 deletions

View file

@ -1252,9 +1252,8 @@ void Document::set_hovered_node(Node* node)
JS::NonnullGCPtr<HTMLCollection> Document::get_elements_by_name(String const& name)
{
auto deprecated_name = name.to_byte_string();
return HTMLCollection::create(*this, HTMLCollection::Scope::Descendants, [deprecated_name](Element const& element) {
return element.name() == deprecated_name;
return HTMLCollection::create(*this, HTMLCollection::Scope::Descendants, [name](Element const& element) {
return element.name() == name;
});
}

View file

@ -459,6 +459,11 @@ void Element::attribute_changed(FlyString const& name, Optional<String> const& v
m_id = {};
else
m_id = value_or_empty;
} else if (name == HTML::AttributeNames::name) {
if (!value.has_value())
m_name = {};
else
m_name = value_or_empty;
} else if (name == HTML::AttributeNames::class_) {
auto new_classes = value_or_empty.bytes_as_string_view().split_view_if(Infra::is_ascii_whitespace);
m_classes.clear();

View file

@ -174,8 +174,6 @@ public:
Layout::NodeWithStyle* layout_node();
Layout::NodeWithStyle const* layout_node() const;
ByteString name() const { return deprecated_attribute(HTML::AttributeNames::name); }
CSS::StyleProperties* computed_css_values() { return m_computed_css_values.ptr(); }
CSS::StyleProperties const* computed_css_values() const { return m_computed_css_values.ptr(); }
void set_computed_css_values(RefPtr<CSS::StyleProperties>);
@ -368,6 +366,7 @@ public:
Directionality directionality() const;
Optional<FlyString> const& id() const { return m_id; }
Optional<FlyString> const& name() const { return m_name; }
virtual JS::GCPtr<JS::HeapFunction<void()>> take_lazy_load_resumption_steps(Badge<DOM::Document>)
{
@ -417,6 +416,7 @@ private:
Optional<Dir> m_dir;
Optional<FlyString> m_id;
Optional<FlyString> m_name;
using PseudoElementLayoutNodes = Array<JS::GCPtr<Layout::Node>, to_underlying(CSS::Selector::PseudoElement::Type::KnownPseudoElementCount)>;
OwnPtr<PseudoElementLayoutNodes> m_pseudo_element_nodes;

View file

@ -84,17 +84,15 @@ Element* HTMLCollection::item(size_t index) const
}
// https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem-key
Element* HTMLCollection::named_item(FlyString const& name_) const
Element* HTMLCollection::named_item(FlyString const& name) const
{
auto name = name_.to_deprecated_fly_string();
// 1. If key is the empty string, return null.
if (name.is_empty())
return nullptr;
auto elements = collect_matching_elements();
// 2. Return the first element in the collection for which at least one of the following is true:
// - it has an ID which is key;
if (auto it = elements.find_if([&](auto& entry) { return entry->id().has_value() && entry->id().value() == name_; }); it != elements.end())
if (auto it = elements.find_if([&](auto& entry) { return entry->id().has_value() && entry->id().value() == name; }); it != elements.end())
return *it;
// - it is in the HTML namespace and has a name attribute whose value is key;
if (auto it = elements.find_if([&](auto& entry) { return entry->namespace_uri() == Namespace::HTML && entry->name() == name; }); it != elements.end())

View file

@ -39,8 +39,6 @@ Variant<Empty, Element*, JS::Handle<RadioNodeList>> HTMLFormControlsCollection::
if (name.is_empty())
return {};
auto const deprecated_name = name.to_deprecated_fly_string();
// 2. If, at the time the method is called, there is exactly one node in the collection that has either an id attribute or a name attribute equal to name, then return that node and stop the algorithm.
// 3. Otherwise, if there are no nodes in the collection that have either an id attribute or a name attribute equal to name, then return null and stop the algorithm.
Element* matching_element = nullptr;
@ -48,7 +46,7 @@ Variant<Empty, Element*, JS::Handle<RadioNodeList>> HTMLFormControlsCollection::
auto collection = collect_matching_elements();
for (auto const& element : collection) {
if (element->id() != name && element->name() != deprecated_name)
if (element->id() != name && element->name() != name)
continue;
if (matching_element) {
@ -68,12 +66,12 @@ Variant<Empty, Element*, JS::Handle<RadioNodeList>> HTMLFormControlsCollection::
// 4. Otherwise, create a new RadioNodeList object representing a live view of the HTMLFormControlsCollection object, further filtered so that the only nodes in the
// RadioNodeList object are those that have either an id attribute or a name attribute equal to name. The nodes in the RadioNodeList object must be sorted in tree
// order. Return that RadioNodeList object.
return JS::make_handle(RadioNodeList::create(realm(), root(), LiveNodeList::Scope::Descendants, [name, deprecated_name](Node const& node) {
return JS::make_handle(RadioNodeList::create(realm(), root(), LiveNodeList::Scope::Descendants, [name](Node const& node) {
if (!is<Element>(node))
return false;
auto const& element = verify_cast<Element>(node);
return element.id() == name || element.name() == deprecated_name;
return element.id() == name || element.name() == name;
}));
}