LibWeb: Add an accessor for the document's title element

This commit is contained in:
Timothy Flynn 2023-06-07 15:13:42 -04:00 committed by Andreas Kling
parent e8fde316f6
commit 43b3673198
Notes: sideshowbarker 2024-07-17 02:06:40 +09:00
2 changed files with 21 additions and 0 deletions

View File

@ -631,6 +631,21 @@ HTML::HTMLHeadElement* Document::head()
return html->first_child_of_type<HTML::HTMLHeadElement>();
}
// https://html.spec.whatwg.org/multipage/dom.html#the-title-element-2
JS::GCPtr<HTML::HTMLTitleElement> 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<HTML::HTMLTitleElement> title_element = nullptr;
for_each_in_subtree_of_type<HTML::HTMLTitleElement>([&](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();

View File

@ -147,6 +147,7 @@ public:
HTML::HTMLHtmlElement* html_element();
HTML::HTMLHeadElement* head();
JS::GCPtr<HTML::HTMLTitleElement> title_element();
HTML::HTMLElement* body();
HTML::HTMLHtmlElement const* html_element() const
@ -159,6 +160,11 @@ public:
return const_cast<Document*>(this)->head();
}
JS::GCPtr<HTML::HTMLTitleElement const> title_element() const
{
return const_cast<Document*>(this)->title_element();
}
HTML::HTMLElement const* body() const
{
return const_cast<Document*>(this)->body();