LibWeb: Only return HTML elements from getElementsByName()

Fixes two WPT tests:
document.getElementsByName-namespace-xhtml.xhtml and
document.getElementsByName-namespace.html
This commit is contained in:
ronak69 2024-10-14 13:16:10 +00:00 committed by Andreas Kling
parent d5fd29adb7
commit 917a2a3c86
Notes: github-actions[bot] 2024-10-14 16:00:15 +00:00
3 changed files with 25 additions and 2 deletions

View file

@ -0,0 +1,4 @@
1 == 1
P == P
1 == 1
P == P

View file

@ -0,0 +1,18 @@
<!DOCTYPE html>
<p name="math"><math name="math">
<mi>a</mi>
<mo>+</mo>
<mi>b</mi>
</math>
<p name="svg"><svg width="300" height="100" name="svg">
<rect width="300" height="100" fill="rgb(0,0,255)"/>
</svg>
<script src="../include.js"></script>
<script>
test(() => {
println(`${document.getElementsByName("math").length} == 1`);
println(`${document.getElementsByName("math")[0].tagName} == P`);
println(`${document.getElementsByName("svg").length} == 1`);
println(`${document.getElementsByName("svg")[0].tagName} == P`);
});
</script>

View file

@ -1488,12 +1488,13 @@ void Document::set_hovered_node(Node* node)
} }
} }
// https://html.spec.whatwg.org/multipage/dom.html#dom-document-getelementsbyname
JS::NonnullGCPtr<NodeList> Document::get_elements_by_name(FlyString const& name) JS::NonnullGCPtr<NodeList> Document::get_elements_by_name(FlyString const& name)
{ {
return LiveNodeList::create(realm(), *this, LiveNodeList::Scope::Descendants, [name](auto const& node) { return LiveNodeList::create(realm(), *this, LiveNodeList::Scope::Descendants, [name](auto const& node) {
if (!is<Element>(node)) if (!is<HTML::HTMLElement>(node))
return false; return false;
return verify_cast<Element>(node).name() == name; return verify_cast<HTML::HTMLElement>(node).name() == name;
}); });
} }