From 7c4c706ebe568661d828537bbe5863c9c5f4511f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 1 Dec 2020 15:47:50 +0100 Subject: [PATCH] LibWeb: Implement Document.getElementsByClassName() Note that we're taking a shortcut here and returning the elements as an Array instead of HTMLCollection. One day we'll have to bite the bullet and deal with HTMLCollection, but not today. --- Libraries/LibWeb/DOM/Document.cpp | 11 +++++++++++ Libraries/LibWeb/DOM/Document.h | 1 + Libraries/LibWeb/DOM/Document.idl | 1 + 3 files changed, 13 insertions(+) diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index 8016a40225..c57d7e06c7 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -458,6 +458,17 @@ NonnullRefPtrVector Document::get_elements_by_tag_name(const FlyString& return elements; } +NonnullRefPtrVector Document::get_elements_by_class_name(const FlyString& class_name) const +{ + NonnullRefPtrVector elements; + for_each_in_subtree_of_type([&](auto& element) { + if (element.has_class(class_name)) + elements.append(element); + return IterationDecision::Continue; + }); + return elements; +} + Color Document::link_color() const { if (m_link_color.has_value()) diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h index 77e9b12039..5c4d82b094 100644 --- a/Libraries/LibWeb/DOM/Document.h +++ b/Libraries/LibWeb/DOM/Document.h @@ -132,6 +132,7 @@ public: NonnullRefPtrVector get_elements_by_name(const String&) const; NonnullRefPtrVector get_elements_by_tag_name(const FlyString&) const; + NonnullRefPtrVector get_elements_by_class_name(const FlyString&) const; const String& source() const { return m_source; } void set_source(const String& source) { m_source = source; } diff --git a/Libraries/LibWeb/DOM/Document.idl b/Libraries/LibWeb/DOM/Document.idl index a6610cd8d1..ce50d7d7b2 100644 --- a/Libraries/LibWeb/DOM/Document.idl +++ b/Libraries/LibWeb/DOM/Document.idl @@ -9,6 +9,7 @@ interface Document : Node { Element? getElementById(DOMString id); ArrayFromVector getElementsByTagName(DOMString tagName); + ArrayFromVector getElementsByClassName(DOMString className); readonly attribute Element? firstElementChild; readonly attribute Element? lastElementChild;