From 6b3293a74bd73d139209e4bfecef5081fa8b29c2 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 18 Sep 2022 00:39:42 +0200 Subject: [PATCH] LibWeb: Support getElementsByTagName() properly in non-HTML documents --- Userland/Libraries/LibWeb/DOM/ParentNode.cpp | 24 ++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/ParentNode.cpp b/Userland/Libraries/LibWeb/DOM/ParentNode.cpp index 27b1f2d82e..d3610752b5 100644 --- a/Userland/Libraries/LibWeb/DOM/ParentNode.cpp +++ b/Userland/Libraries/LibWeb/DOM/ParentNode.cpp @@ -1,11 +1,13 @@ /* * Copyright (c) 2020, Luke Wilde + * Copyright (c) 2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include +#include #include #include #include @@ -104,18 +106,22 @@ JS::NonnullGCPtr ParentNode::get_elements_by_tag_name(FlyString }); } - // FIXME: 2. Otherwise, if root’s node document is an HTML document, return a HTMLCollection rooted at root, whose filter matches the following descendant elements: - // (It is currently always a HTML document) - return HTMLCollection::create(*this, [qualified_name](Element const& element) { - // - Whose namespace is the HTML namespace and whose qualified name is qualifiedName, in ASCII lowercase. - if (element.namespace_() == Namespace::HTML) - return element.qualified_name().to_lowercase() == qualified_name.to_lowercase(); + // 2. Otherwise, if root’s node document is an HTML document, return a HTMLCollection rooted at root, whose filter matches the following descendant elements: + if (root().document().document_type() == Document::Type::HTML) { + return HTMLCollection::create(*this, [qualified_name](Element const& element) { + // - Whose namespace is the HTML namespace and whose qualified name is qualifiedName, in ASCII lowercase. + if (element.namespace_() == Namespace::HTML) + return element.qualified_name().to_lowercase() == qualified_name.to_lowercase(); - // - Whose namespace is not the HTML namespace and whose qualified name is qualifiedName. + // - Whose namespace is not the HTML namespace and whose qualified name is qualifiedName. + return element.qualified_name() == qualified_name; + }); + } + + // 3. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose qualified name is qualifiedName. + return HTMLCollection::create(*this, [qualified_name](Element const& element) { return element.qualified_name() == qualified_name; }); - - // FIXME: 3. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose qualified name is qualifiedName. } // https://dom.spec.whatwg.org/#concept-getelementsbytagnamens