1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:38:11 +00:00

LibWeb: Implement Element.getElementsBy{Tag,Class}Name()

Just like the Document variants, but using the given Element as
for_each_in_subtree_of_type() root.
This commit is contained in:
Linus Groh 2021-02-07 23:44:01 +01:00 committed by Andreas Kling
parent 2a38f008bf
commit 79bab28f5e
3 changed files with 34 additions and 0 deletions

View file

@ -46,6 +46,7 @@
#include <LibWeb/Layout/TableRowGroupBox.h>
#include <LibWeb/Layout/TreeBuilder.h>
#include <LibWeb/Layout/WidgetBox.h>
#include <LibWeb/Namespace.h>
namespace Web::DOM {
@ -344,4 +345,31 @@ bool Element::is_focused() const
return document().focused_element() == this;
}
NonnullRefPtrVector<Element> Element::get_elements_by_tag_name(const FlyString& tag_name) const
{
// FIXME: Support "*" for tag_name
// https://dom.spec.whatwg.org/#concept-getelementsbytagname
NonnullRefPtrVector<Element> elements;
for_each_in_subtree_of_type<Element>([&](auto& element) {
if (element.namespace_() == Namespace::HTML
? element.local_name().to_lowercase() == tag_name.to_lowercase()
: element.local_name() == tag_name) {
elements.append(element);
}
return IterationDecision::Continue;
});
return elements;
}
NonnullRefPtrVector<Element> Element::get_elements_by_class_name(const FlyString& class_name) const
{
NonnullRefPtrVector<Element> elements;
for_each_in_subtree_of_type<Element>([&](auto& element) {
if (element.has_class(class_name, m_document->in_quirks_mode() ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive))
elements.append(element);
return IterationDecision::Continue;
});
return elements;
}
}