Truly independent web browser
LibWeb: Report the title when a title element comes or goes
sideshowbarkerdf3a4efd8d8d
diff --git a/Libraries/LibWeb/HTML/HTMLTitleElement.cpp b/Libraries/LibWeb/HTML/HTMLTitleElement.cppindex 80c67de696c..dbb64cc57ee 100644--- a/Libraries/LibWeb/HTML/HTMLTitleElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLTitleElement.cpp@@ -21,10 +21,27 @@ HTMLTitleElement::HTMLTitleElement(DOM::Document& document, DOM::QualifiedName qHTMLTitleElement::~HTMLTitleElement() = default;+// NB: Inserting or removing a title element can change which one is the document's title element, and so the+// document's title, without any change to a title element's own children.+void HTMLTitleElement::inserted()+{+ HTMLElement::inserted();+ if (is_connected())+ report_title_change_to_page();+}++void HTMLTitleElement::removed_from(IsSubtreeRoot is_subtree_root, Node* old_ancestor, Node& old_root)+{+ HTMLElement::removed_from(is_subtree_root, old_ancestor, old_root);+ if (old_root.is_document())+ report_title_change_to_page();+}+void HTMLTitleElement::children_changed(ChildrenChangedMetadata const& metadata){HTMLElement::children_changed(metadata);- if (!m_suppresses_title_change_reports)+ // NB: A disconnected title element isn't the document's title element, so changing its text changes nothing.+ if (!m_suppresses_title_change_reports && is_connected())report_title_change_to_page();}diff --git a/Libraries/LibWeb/HTML/HTMLTitleElement.h b/Libraries/LibWeb/HTML/HTMLTitleElement.hindex 6f92cc4e1d0..a234577031b 100644--- a/Libraries/LibWeb/HTML/HTMLTitleElement.h+++ b/Libraries/LibWeb/HTML/HTMLTitleElement.h@@ -25,6 +25,8 @@ private:HTMLTitleElement(DOM::Document&, DOM::QualifiedName);virtual bool is_html_title_element() const override { return true; }+ virtual void inserted() override;+ virtual void removed_from(IsSubtreeRoot, Node* old_ancestor, Node& old_root) override;virtual void children_changed(ChildrenChangedMetadata const&) override;void report_title_change_to_page();diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txtindex 01a867165f0..103b935c49f 100644--- a/Tests/LibWebView/CMakeLists.txt+++ b/Tests/LibWebView/CMakeLists.txt@@ -145,6 +145,7 @@ if (NOT WIN32)TestRequestServerDiskCaches.cppTestShadowTreeTooltipsAndLinks.cppTestSharedWorkerHost.cpp+ TestTitleElementChanges.cppTestUserActivation.cppTestViewSource.cppTestWebCompatReport.cppdiff --git a/Tests/LibWebView/TestTitleElementChanges.cpp b/Tests/LibWebView/TestTitleElementChanges.cppnew file mode 100644index 00000000000..1d141feda0b--- /dev/null+++ b/Tests/LibWebView/TestTitleElementChanges.cpp@@ -0,0 +1,56 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include "HeadlessTest.h"+#include <AK/LexicalPath.h>+#include <AK/Utf16String.h>+#include <LibCore/EventLoop.h>+#include <LibGfx/SystemTheme.h>+#include <LibTest/TestCase.h>+#include <LibWebView/HeadlessWebView.h>+#include <LibWebView/Utilities.h>++// The view shows the document's title after a title element is replaced or removed, not the title from before.+TEST_CASE(title_element_replacement_and_removal)+{+ Test::HeadlessTestEnvironment environment { "TestTitleElementChanges"sv };++ auto app = TRY_OR_FAIL(environment.create_application());++ auto theme_path = LexicalPath::join(WebView::s_ladybird_resource_root, "themes"sv, "Default.ini"sv);+ auto theme = TRY_OR_FAIL(Gfx::load_system_theme(theme_path.string()));++ auto view = WebView::HeadlessWebView::create(move(theme), { 800, 600 });++ size_t loads_finished = 0;+ view->on_load_finish = [&](auto const&) { ++loads_finished; };+ Core::EventLoop::current().spin_until([&]() { return loads_finished >= 1; });++ Utf16String title;+ view->on_title_change = [&](Utf16String const& new_title) { title = new_title; };++ view->load_html(R"html(<!DOCTYPE html><title>TITLE_A</title>)html"sv);+ Core::EventLoop::current().spin_until([&]() { return title == "TITLE_A"sv; });++ view->run_javascript(R"js(+ const replacement = document.createElement("title");+ replacement.textContent = "TITLE_B";+ document.querySelector("title").replaceWith(replacement);+ )js"_string);+ Core::EventLoop::current().spin_until([&]() { return title == "TITLE_B"sv; });++ // A document without a title element has an empty title, which the view shows as the page's URL.+ view->run_javascript(R"js(document.querySelector("title").remove();)js"_string);+ auto url = Utf16String::from_utf8(view->url().serialize());+ Core::EventLoop::current().spin_until([&]() { return title == url; });++ view->run_javascript(R"js(+ const title = document.createElement("title");+ title.textContent = "TITLE_C";+ document.head.append(title);+ )js"_string);+ Core::EventLoop::current().spin_until([&]() { return title == "TITLE_C"sv; });+}