diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 500927fd21..67066f2729 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2018-2021, Andreas Kling * Copyright (c) 2021, Linus Groh + * Copyright (c) 2021, Luke Wilde * * SPDX-License-Identifier: BSD-2-Clause */ @@ -33,10 +34,14 @@ #include #include #include +#include #include +#include +#include #include #include #include +#include #include #include #include @@ -511,6 +516,7 @@ NonnullRefPtr Document::get_elements_by_class_name(FlyString con }); } +// https://html.spec.whatwg.org/multipage/obsolete.html#dom-document-applets NonnullRefPtr Document::applets() { // FIXME: This should return the same HTMLCollection object every time, @@ -518,6 +524,7 @@ NonnullRefPtr Document::applets() return HTMLCollection::create(*this, [] { return false; }); } +// https://html.spec.whatwg.org/multipage/obsolete.html#dom-document-anchors NonnullRefPtr Document::anchors() { // FIXME: This should return the same HTMLCollection object every time, @@ -527,6 +534,62 @@ NonnullRefPtr Document::anchors() }); } +// https://html.spec.whatwg.org/multipage/dom.html#dom-document-images +NonnullRefPtr Document::images() +{ + // 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, [](Element const& element) { + return is(element); + }); +} + +// https://html.spec.whatwg.org/multipage/dom.html#dom-document-embeds +NonnullRefPtr Document::embeds() +{ + // 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, [](Element const& element) { + return is(element); + }); +} + +// https://html.spec.whatwg.org/multipage/dom.html#dom-document-plugins +NonnullRefPtr Document::plugins() +{ + return embeds(); +} + +// https://html.spec.whatwg.org/multipage/dom.html#dom-document-links +NonnullRefPtr Document::links() +{ + // 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, [](Element const& element) { + return (is(element) || is(element)) && element.has_attribute(HTML::AttributeNames::href); + }); +} + +// https://html.spec.whatwg.org/multipage/dom.html#dom-document-forms +NonnullRefPtr Document::forms() +{ + // 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, [](Element const& element) { + return is(element); + }); +} + +// https://html.spec.whatwg.org/multipage/dom.html#dom-document-scripts +NonnullRefPtr Document::scripts() +{ + // 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, [](Element const& element) { + return is(element); + }); +} + Color Document::link_color() const { if (m_link_color.has_value()) diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index d1c062507c..d31c43a036 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -142,6 +142,12 @@ public: NonnullRefPtr applets(); NonnullRefPtr anchors(); + NonnullRefPtr images(); + NonnullRefPtr embeds(); + NonnullRefPtr plugins(); + NonnullRefPtr links(); + NonnullRefPtr forms(); + NonnullRefPtr scripts(); const String& source() const { return m_source; } void set_source(const String& source) { m_source = source; } diff --git a/Userland/Libraries/LibWeb/DOM/Document.idl b/Userland/Libraries/LibWeb/DOM/Document.idl index 07b11682dd..bf9fdafaac 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.idl +++ b/Userland/Libraries/LibWeb/DOM/Document.idl @@ -20,6 +20,12 @@ interface Document : Node { readonly attribute HTMLCollection applets; readonly attribute HTMLCollection anchors; + readonly attribute HTMLCollection images; + readonly attribute HTMLCollection embeds; + readonly attribute HTMLCollection plugins; + readonly attribute HTMLCollection links; + readonly attribute HTMLCollection forms; + readonly attribute HTMLCollection scripts; Element createElement(DOMString tagName); Element createElementNS(DOMString? namespace, DOMString qualifiedName);