ladybird/Userland/Libraries/LibWeb/DOM/NonElementParentNode.h
Andreas Kling 98f88d49de 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.
2024-07-21 11:42:53 +01:00

38 lines
946 B
C++

/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/FlyString.h>
#include <AK/Forward.h>
#include <LibJS/Heap/GCPtr.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/AttributeNames.h>
#include <LibWeb/TreeNode.h>
namespace Web::DOM {
template<typename NodeType>
class NonElementParentNode {
public:
JS::GCPtr<Element> get_element_by_id(FlyString const& id) const
{
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;
}
return TraversalDecision::Continue;
});
return found_element;
}
protected:
NonElementParentNode() = default;
};
}