diff options
author | Andreas Kling <kling@serenityos.org> | 2021-04-22 22:14:55 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-22 22:14:55 +0200 |
commit | b74bf31a53560c1d0cc2d928d0db52e4f6734610 (patch) | |
tree | e4f8436975ef3bffef66d54a2e6b1e93f3f2ff93 /Userland/Libraries | |
parent | 43d16fa5b67d289b105aca45e43479144ad5e3fb (diff) | |
download | serenity-b74bf31a53560c1d0cc2d928d0db52e4f6734610.zip |
LibWeb: Implement document.anchors
This returns an HTMLCollection of all <a> elements in the document that
have a "name" attribute.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.cpp | 10 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.h | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.idl | 1 |
3 files changed, 12 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index e4f9ae1f47..de8102c8c5 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -32,6 +32,7 @@ #include <LibWeb/Dump.h> #include <LibWeb/HTML/AttributeNames.h> #include <LibWeb/HTML/EventNames.h> +#include <LibWeb/HTML/HTMLAnchorElement.h> #include <LibWeb/HTML/HTMLBodyElement.h> #include <LibWeb/HTML/HTMLFrameSetElement.h> #include <LibWeb/HTML/HTMLHeadElement.h> @@ -517,6 +518,15 @@ NonnullRefPtr<HTMLCollection> Document::applets() return HTMLCollection::create(*this, [] { return false; }); } +NonnullRefPtr<HTMLCollection> Document::anchors() +{ + // 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<HTML::HTMLAnchorElement>(element) && element.has_attribute(HTML::AttributeNames::name); + }); +} + 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 a470b0379b..f070ac17fd 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -141,6 +141,7 @@ public: NonnullRefPtr<HTMLCollection> get_elements_by_class_name(FlyString const&); NonnullRefPtr<HTMLCollection> applets(); + NonnullRefPtr<HTMLCollection> anchors(); 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 cc31659525..07b11682dd 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.idl +++ b/Userland/Libraries/LibWeb/DOM/Document.idl @@ -19,6 +19,7 @@ interface Document : Node { HTMLCollection getElementsByClassName(DOMString className); readonly attribute HTMLCollection applets; + readonly attribute HTMLCollection anchors; Element createElement(DOMString tagName); Element createElementNS(DOMString? namespace, DOMString qualifiedName); |