Skip to content

Commit

Permalink
LibWeb: Simplify getElementsByTagName{,NS}() filters
Browse files Browse the repository at this point in the history
Everything below the collection root is a descendant of the collection
root, so there's no need to check is_descendant_of() :^)
  • Loading branch information
awesomekling committed Sep 17, 2022
1 parent b371b31 commit bd0648a
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions Userland/Libraries/LibWeb/DOM/ParentNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,14 @@ JS::NonnullGCPtr<HTMLCollection> ParentNode::get_elements_by_tag_name(FlyString
{
// 1. If qualifiedName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches only descendant elements.
if (qualified_name == "*") {
return HTMLCollection::create(*this, [this](Element const& element) {
return element.is_descendant_of(*this);
return HTMLCollection::create(*this, [](Element const&) {
return true;
});
}

// FIXME: 2. Otherwise, if root’s node document is an HTML document, return a HTMLCollection rooted at root, whose filter matches the following descendant elements:
// (It is currently always a HTML document)
return HTMLCollection::create(*this, [this, qualified_name](Element const& element) {
if (!element.is_descendant_of(*this))
return false;

return HTMLCollection::create(*this, [qualified_name](Element const& element) {
// - Whose namespace is the HTML namespace and whose qualified name is qualifiedName, in ASCII lowercase.
if (element.namespace_() == Namespace::HTML)
return element.qualified_name().to_lowercase() == qualified_name.to_lowercase();
Expand All @@ -132,28 +129,28 @@ JS::NonnullGCPtr<HTMLCollection> ParentNode::get_elements_by_tag_name_ns(FlyStri

// 2. If both namespace and localName are "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements.
if (namespace_ == "*" && local_name == "*") {
return HTMLCollection::create(*this, [this](Element const& element) {
return element.is_descendant_of(*this);
return HTMLCollection::create(*this, [](Element const&) {
return true;
});
}

// 3. Otherwise, if namespace is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements whose local name is localName.
if (namespace_ == "*") {
return HTMLCollection::create(*this, [this, local_name](Element const& element) {
return element.is_descendant_of(*this) && element.local_name() == local_name;
return HTMLCollection::create(*this, [local_name](Element const& element) {
return element.local_name() == local_name;
});
}

// 4. Otherwise, if localName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements whose namespace is namespace.
if (local_name == "*") {
return HTMLCollection::create(*this, [this, namespace_](Element const& element) {
return element.is_descendant_of(*this) && element.namespace_() == namespace_;
return HTMLCollection::create(*this, [namespace_](Element const& element) {
return element.namespace_() == namespace_;
});
}

// 5. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose namespace is namespace and local name is localName.
return HTMLCollection::create(*this, [this, namespace_, local_name](Element const& element) {
return element.is_descendant_of(*this) && element.namespace_() == namespace_ && element.local_name() == local_name;
return HTMLCollection::create(*this, [namespace_, local_name](Element const& element) {
return element.namespace_() == namespace_ && element.local_name() == local_name;
});
}

Expand Down

0 comments on commit bd0648a

Please sign in to comment.