Skip to content

Commit f95ed9f

Browse files
trflynn89awesomekling
authored andcommitted
LibWeb: Implement document.title for SVG documents
1 parent e2e4e6d commit f95ed9f

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

Userland/Libraries/LibWeb/DOM/Document.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
#include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
8181
#include <LibWeb/Platform/Timer.h>
8282
#include <LibWeb/SVG/SVGElement.h>
83+
#include <LibWeb/SVG/SVGTitleElement.h>
8384
#include <LibWeb/SVG/TagNames.h>
8485
#include <LibWeb/Selection/Selection.h>
8586
#include <LibWeb/UIEvents/EventNames.h>
@@ -688,8 +689,9 @@ DeprecatedString Document::title() const
688689

689690
// 1. If the document element is an SVG svg element, then let value be the child text content of the first SVG title
690691
// element that is a child of the document element.
691-
if (auto const* document_element = this->document_element(); document_element && is<SVG::SVGElement>(document_element)) {
692-
// FIXME: Implement the SVG title element and get its child text content.
692+
if (auto const* document_element = this->document_element(); is<SVG::SVGElement>(document_element)) {
693+
if (auto const* title_element = document_element->first_child_of_type<SVG::SVGTitleElement>())
694+
value = title_element->child_text_content();
693695
}
694696

695697
// 2. Otherwise, let value be the child text content of the title element, or the empty string if the title element
@@ -712,13 +714,25 @@ WebIDL::ExceptionOr<void> Document::set_title(DeprecatedString const& title)
712714

713715
// -> If the document element is an SVG svg element
714716
if (is<SVG::SVGElement>(document_element)) {
715-
// FIXME: 1. If there is an SVG title element that is a child of the document element, let element be the first such
716-
// element.
717-
// FIXME: 2. Otherwise:
718-
// 1. Let element be the result of creating an element given the document element's node document, title,
719-
// and the SVG namespace.
720-
// 2. Insert element as the first child of the document element.
721-
// FIXME: 3. String replace all with the given value within element.
717+
JS::GCPtr<Element> element;
718+
719+
// 1. If there is an SVG title element that is a child of the document element, let element be the first such
720+
// element.
721+
if (auto* title_element = document_element->first_child_of_type<SVG::SVGTitleElement>()) {
722+
element = title_element;
723+
}
724+
// 2. Otherwise:
725+
else {
726+
// 1. Let element be the result of creating an element given the document element's node document, title,
727+
// and the SVG namespace.
728+
element = TRY(DOM::create_element(*this, HTML::TagNames::title, Namespace::SVG));
729+
730+
// 2. Insert element as the first child of the document element.
731+
document_element->insert_before(*element, nullptr);
732+
}
733+
734+
// 3. String replace all with the given value within element.
735+
element->string_replace_all(title);
722736
}
723737

724738
// -> If the document element is in the HTML namespace

0 commit comments

Comments
 (0)