Serenity Operating System
1/*
2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/Forward.h>
10#include <LibJS/Heap/GCPtr.h>
11#include <LibWeb/Forward.h>
12#include <LibWeb/HTML/AttributeNames.h>
13#include <LibWeb/TreeNode.h>
14
15namespace Web::DOM {
16
17template<typename NodeType>
18class NonElementParentNode {
19public:
20 JS::GCPtr<Element const> get_element_by_id(DeprecatedFlyString const& id) const
21 {
22 JS::GCPtr<Element const> found_element;
23 static_cast<NodeType const*>(this)->template for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
24 if (element.attribute(HTML::AttributeNames::id) == id) {
25 found_element = &element;
26 return IterationDecision::Break;
27 }
28 return IterationDecision::Continue;
29 });
30 return found_element;
31 }
32
33 JS::GCPtr<Element> get_element_by_id(DeprecatedFlyString const& id)
34 {
35 JS::GCPtr<Element> found_element;
36 static_cast<NodeType*>(this)->template for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
37 if (element.attribute(HTML::AttributeNames::id) == id) {
38 found_element = &element;
39 return IterationDecision::Break;
40 }
41 return IterationDecision::Continue;
42 });
43 return found_element;
44 }
45
46protected:
47 NonElementParentNode() = default;
48};
49
50}