From 43b36731985f26c9349721e6bfffe73f53e51eb0 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 7 Jun 2023 15:13:42 -0400 Subject: [PATCH] LibWeb: Add an accessor for the document's title element --- Userland/Libraries/LibWeb/DOM/Document.cpp | 15 +++++++++++++++ Userland/Libraries/LibWeb/DOM/Document.h | 6 ++++++ 2 files changed, 21 insertions(+) diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 2e5491a750..25c3a9ec46 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -631,6 +631,21 @@ HTML::HTMLHeadElement* Document::head() return html->first_child_of_type(); } +// https://html.spec.whatwg.org/multipage/dom.html#the-title-element-2 +JS::GCPtr Document::title_element() +{ + // The title element of a document is the first title element in the document (in tree order), if there is one, or + // null otherwise. + JS::GCPtr title_element = nullptr; + + for_each_in_subtree_of_type([&](auto& title_element_in_tree) { + title_element = title_element_in_tree; + return IterationDecision::Break; + }); + + return title_element; +} + HTML::HTMLElement* Document::body() { auto* html = html_element(); diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index 088b3dafa6..8cb36f505f 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -147,6 +147,7 @@ public: HTML::HTMLHtmlElement* html_element(); HTML::HTMLHeadElement* head(); + JS::GCPtr title_element(); HTML::HTMLElement* body(); HTML::HTMLHtmlElement const* html_element() const @@ -159,6 +160,11 @@ public: return const_cast(this)->head(); } + JS::GCPtr title_element() const + { + return const_cast(this)->title_element(); + } + HTML::HTMLElement const* body() const { return const_cast(this)->body();