LibWeb/DOM: Implement Node.lookupPrefix

Adds https://dom.spec.whatwg.org/#dom-node-lookupprefix
This commit is contained in:
Ángel Carias 2024-07-27 12:51:36 -06:00 committed by Tim Ledbetter
commit 9624e0d2a2
Notes: github-actions[bot] 2024-07-27 23:52:45 +00:00
7 changed files with 109 additions and 1 deletions

View file

@ -2156,6 +2156,30 @@ void Element::set_prefix(Optional<FlyString> value)
m_qualified_name.set_prefix(move(value));
}
// https://dom.spec.whatwg.org/#locate-a-namespace-prefix
Optional<String> Element::locate_a_namespace_prefix(Optional<String> const& namespace_) const
{
// 1. If elements namespace is namespace and its namespace prefix is non-null, then return its namespace prefix.
if (this->namespace_uri() == namespace_ && this->prefix().has_value())
return this->prefix()->to_string();
// 2. If element has an attribute whose namespace prefix is "xmlns" and value is namespace, then return elements first such attributes local name.
if (auto* attributes = this->attributes()) {
for (size_t i = 0; i < attributes->length(); ++i) {
auto& attr = *attributes->item(i);
if (attr.prefix() == "xmlns" && attr.value() == namespace_)
return attr.local_name().to_string();
}
}
// 3. If elements parent element is not null, then return the result of running locate a namespace prefix on that element using namespace.
if (auto* parent = this->parent_element())
return parent->locate_a_namespace_prefix(namespace_);
// 4. Return null
return {};
}
void Element::for_each_attribute(Function<void(Attr const&)> callback) const
{
for (size_t i = 0; i < m_attributes->length(); ++i)