1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-09 17:44:56 +09:00

LibWeb: Make getElementById() always return first match in tree order

We had a const and non-const version of this function, with slightly
different behavior (oops!)

This patch consolidates the implementations and keeps only the correct
behavior in there.

Fixes an issue where comments were not collapsible on Hacker News.
This commit is contained in:
Andreas Kling 2024-07-21 11:43:59 +02:00 committed by Tim Ledbetter
parent e4e64c15aa
commit 98f88d49de
Notes: github-actions[bot] 2024-07-21 11:26:29 +00:00
3 changed files with 11 additions and 16 deletions

View file

@ -0,0 +1 @@
getElementById('foo') => 1

View file

@ -0,0 +1,7 @@
<div id="foo" n="1"></div><div id="foo" n="2"></div>
<script src="../include.js"></script>
<script>
test(() => {
println("getElementById('foo') => " + document.getElementById("foo").getAttribute("n"));
});
</script>

View file

@ -18,10 +18,10 @@ namespace Web::DOM {
template<typename NodeType>
class NonElementParentNode {
public:
JS::GCPtr<Element const> get_element_by_id(FlyString const& id) const
JS::GCPtr<Element> get_element_by_id(FlyString const& id) const
{
JS::GCPtr<Element const> found_element;
static_cast<NodeType const*>(this)->template for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
JS::GCPtr<Element> found_element;
const_cast<NodeType*>(static_cast<NodeType const*>(this))->template for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
if (element.id() == id) {
found_element = &element;
return TraversalDecision::Break;
@ -31,19 +31,6 @@ public:
return found_element;
}
JS::GCPtr<Element> get_element_by_id(FlyString const& id)
{
JS::GCPtr<Element> found_element;
static_cast<NodeType*>(this)->template for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
if (element.id() == id) {
found_element = &element;
return TraversalDecision::Continue;
}
return TraversalDecision::Continue;
});
return found_element;
}
protected:
NonElementParentNode() = default;
};