/* * Copyright (c) 2020, Luke Wilde * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include namespace Web::DOM { ExceptionOr> ParentNode::query_selector(StringView selector_text) { auto maybe_selectors = parse_selector(CSS::ParsingContext(*this), selector_text); if (!maybe_selectors.has_value()) return DOM::SyntaxError::create("Failed to parse selector"); auto selectors = maybe_selectors.value(); RefPtr result; for_each_in_inclusive_subtree_of_type([&](auto& element) { for (auto& selector : selectors) { if (SelectorEngine::matches(selector, element)) { result = element; return IterationDecision::Break; } } return IterationDecision::Continue; }); return result; } ExceptionOr> ParentNode::query_selector_all(StringView selector_text) { auto maybe_selectors = parse_selector(CSS::ParsingContext(*this), selector_text); if (!maybe_selectors.has_value()) return DOM::SyntaxError::create("Failed to parse selector"); auto selectors = maybe_selectors.value(); NonnullRefPtrVector elements; for_each_in_inclusive_subtree_of_type([&](auto& element) { for (auto& selector : selectors) { if (SelectorEngine::matches(selector, element)) { elements.append(element); } } return IterationDecision::Continue; }); return elements; } RefPtr ParentNode::first_element_child() { return first_child_of_type(); } RefPtr ParentNode::last_element_child() { return last_child_of_type(); } // https://dom.spec.whatwg.org/#dom-parentnode-childelementcount u32 ParentNode::child_element_count() const { u32 count = 0; for (auto* child = first_child(); child; child = child->next_sibling()) { if (is(child)) ++count; } return count; } // https://dom.spec.whatwg.org/#dom-parentnode-children NonnullRefPtr ParentNode::children() { // The children getter steps are to return an HTMLCollection collection rooted at this matching only element children. // FIXME: This should return the same HTMLCollection object every time, // but that would cause a reference cycle since HTMLCollection refs the root. return HTMLCollection::create(*this, [this](Element const& element) { return is_parent_of(element); }); } }