1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 22:58:12 +00:00

LibWeb: Add naive support for document.querySelectorAll()

This currently returns a JS::Array of elements matching a selector.
The more correct behavior would be to return a static NodeList, but as
we don't have NodeLists right now, that'll be a task for the future.
This commit is contained in:
Andreas Kling 2020-03-29 22:24:23 +02:00
parent c56acba75e
commit 0f7bcd4111
11 changed files with 182 additions and 78 deletions

View file

@ -34,6 +34,7 @@
#include <LibJS/Runtime/Function.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibWeb/Bindings/DocumentWrapper.h>
#include <LibWeb/CSS/SelectorEngine.h>
#include <LibWeb/CSS/StyleResolver.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/DocumentType.h>
@ -43,10 +44,12 @@
#include <LibWeb/DOM/HTMLHeadElement.h>
#include <LibWeb/DOM/HTMLHtmlElement.h>
#include <LibWeb/DOM/HTMLTitleElement.h>
#include <LibWeb/Dump.h>
#include <LibWeb/Frame.h>
#include <LibWeb/HtmlView.h>
#include <LibWeb/Layout/LayoutDocument.h>
#include <LibWeb/Layout/LayoutTreeBuilder.h>
#include <LibWeb/Parser/CSSParser.h>
#include <stdio.h>
namespace Web {
@ -306,6 +309,23 @@ Vector<const Element*> Document::get_elements_by_name(const String& name) const
return elements;
}
NonnullRefPtrVector<Element> Document::query_selector_all(const StringView& selector_text)
{
auto selector = parse_selector(selector_text);
if (!selector.has_value())
return {};
NonnullRefPtrVector<Element> elements;
for_each_in_subtree_of_type<Element>([&](auto& element) {
if (SelectorEngine::matches(selector.value(), element)) {
elements.append(element);
}
return IterationDecision::Continue;
});
return elements;
}
Color Document::link_color() const
{
if (m_link_color.has_value())