From a1a740bb3eeec55785754b22081a2cbfb04c0b04 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Mon, 11 Nov 2024 05:32:49 +1300 Subject: [PATCH] LibWeb: Make rendered_text_fragment return a DocumentFragment This closer matches the spec and is needed in the implementation of the innerText setter. --- Libraries/LibWeb/HTML/HTMLElement.cpp | 19 ++++++++++++------- Libraries/LibWeb/HTML/HTMLElement.h | 2 +- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Libraries/LibWeb/HTML/HTMLElement.cpp b/Libraries/LibWeb/HTML/HTMLElement.cpp index 271d53a7102..9800437210c 100644 --- a/Libraries/LibWeb/HTML/HTMLElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLElement.cpp @@ -145,9 +145,10 @@ WebIDL::ExceptionOr HTMLElement::set_content_editable(StringView content_e void HTMLElement::set_inner_text(StringView text) { // 1. Let fragment be the rendered text fragment for value given element's node document. + auto fragment = rendered_text_fragment(text); + // 2. Replace all with fragment within element. - remove_all_children(); - append_rendered_text_fragment(text); + replace_all(fragment); set_needs_style_update(true); } @@ -160,10 +161,11 @@ WebIDL::ExceptionOr HTMLElement::set_outer_text(String) } // https://html.spec.whatwg.org/multipage/dom.html#rendered-text-fragment -void HTMLElement::append_rendered_text_fragment(StringView input) +JS::NonnullGCPtr HTMLElement::rendered_text_fragment(StringView input) { - // FIXME: 1. Let fragment be a new DocumentFragment whose node document is document. - // Instead of creating a DocumentFragment the nodes are appended directly. + // 1. Let fragment be a new DocumentFragment whose node document is document. + // Instead of creating a DocumentFragment the nodes are appended directly. + auto fragment = heap().allocate(realm(), document()); // 2. Let position be a position variable for input, initially pointing at the start of input. // 3. Let text be the empty string. @@ -177,7 +179,7 @@ void HTMLElement::append_rendered_text_fragment(StringView input) // 2. If text is not the empty string, then append a new Text node whose data is text and node document is document to fragment. if (!text.is_empty()) { - MUST(append_child(document().create_text_node(MUST(String::from_utf8(text))))); + MUST(fragment->append_child(document().create_text_node(MUST(String::from_utf8(text))))); } // 3. While position is not past the end of input, and the code point at position is either U+000A LF or U+000D CR: @@ -193,9 +195,12 @@ void HTMLElement::append_rendered_text_fragment(StringView input) // 3. Append the result of creating an element given document, br, and the HTML namespace to fragment. auto br_element = DOM::create_element(document(), HTML::TagNames::br, Namespace::HTML).release_value(); - MUST(append_child(br_element)); + MUST(fragment->append_child(br_element)); } } + + // 5. Return fragment. + return fragment; } struct RequiredLineBreakCount { diff --git a/Libraries/LibWeb/HTML/HTMLElement.h b/Libraries/LibWeb/HTML/HTMLElement.h index 92f7f9eae0c..feb47862570 100644 --- a/Libraries/LibWeb/HTML/HTMLElement.h +++ b/Libraries/LibWeb/HTML/HTMLElement.h @@ -97,7 +97,7 @@ private: virtual void did_lose_focus() override; [[nodiscard]] String get_the_text_steps(); - void append_rendered_text_fragment(StringView input); + JS::NonnullGCPtr rendered_text_fragment(StringView input); JS::GCPtr m_labels;