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:
parent
e4e64c15aa
commit
98f88d49de
Notes:
github-actions[bot]
2024-07-21 11:26:29 +00:00
Author: https://github.com/awesomekling
Commit: 98f88d49de
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/746
Reviewed-by: https://github.com/tcl3 ✅
3 changed files with 11 additions and 16 deletions
|
@ -0,0 +1 @@
|
|||
getElementById('foo') => 1
|
7
Tests/LibWeb/Text/input/DOM/getElementById-multiple.html
Normal file
7
Tests/LibWeb/Text/input/DOM/getElementById-multiple.html
Normal 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>
|
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue