mirror of
https://github.com/fergalmoran/ladybird.git
synced 2026-01-08 09:35:15 +00:00
LibWeb: Look for first ID _or_ name in HTMLCollection::named_item
Previously we would look for a matching ID, and then for a matching name. If there was an element in the collection which had a matching ID as well as an element with a matching name, we would always return the element with a matching ID irrespective of what order that element was in.
This commit is contained in:
@@ -100,23 +100,26 @@ 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& key) const
|
||||
{
|
||||
// 1. If key is the empty string, return null.
|
||||
if (name.is_empty())
|
||||
if (key.is_empty())
|
||||
return nullptr;
|
||||
|
||||
update_cache_if_needed();
|
||||
auto const& elements = m_cached_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())
|
||||
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())
|
||||
return *it;
|
||||
// or null if there is no such element.
|
||||
for (auto const& element : m_cached_elements) {
|
||||
// - it has an ID which is key;
|
||||
if (element->id() == key)
|
||||
return element;
|
||||
|
||||
// - it is in the HTML namespace and has a name attribute whose value is key;
|
||||
if (element->namespace_uri() == Namespace::HTML && element->name() == key)
|
||||
return element;
|
||||
}
|
||||
|
||||
// or null if there is no such element.
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user