From 21bd3a21bdf0a32014b4ecacfc61d7803b1df7fa Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 21 Feb 2022 16:34:54 -0500 Subject: [PATCH] LibWeb: Append only one line feed character in Document.writeln There were a couple issues here: 1. The line feed should only be appended once, rather than one per string. 2. The new_strings list of strings was unused (we were creating the new list, then passing the old list to Document.write). --- Userland/Libraries/LibWeb/DOM/Document.cpp | 36 ++++++++++++---------- Userland/Libraries/LibWeb/DOM/Document.h | 2 ++ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 999a8fcd28b..911049badb8 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -150,6 +150,25 @@ void Document::removed_last_ref() // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-write ExceptionOr Document::write(Vector const& strings) +{ + StringBuilder builder; + builder.join(""sv, strings); + + return run_the_document_write_steps(builder.build()); +} + +// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-writeln +ExceptionOr Document::writeln(Vector const& strings) +{ + StringBuilder builder; + builder.join(""sv, strings); + builder.append("\n"sv); + + return run_the_document_write_steps(builder.build()); +} + +// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#document-write-steps +ExceptionOr Document::run_the_document_write_steps(String input) { // 1. If document is an XML document, then throw an "InvalidStateError" DOMException. if (doctype() && doctype()->name() == "xml") @@ -174,9 +193,7 @@ ExceptionOr Document::write(Vector const& strings) } // 5. Insert input into the input stream just before the insertion point. - StringBuilder builder; - builder.join("", strings); - m_parser->tokenizer().insert_input_at_insertion_point(builder.build()); + m_parser->tokenizer().insert_input_at_insertion_point(input); // 6. If there is no pending parsing-blocking script, have the HTML parser process input, one code point at a time, processing resulting tokens as they are emitted, and stopping when the tokenizer reaches the insertion point or when the processing of the tokenizer is aborted by the tree construction stage (this can happen if a script end tag token is emitted by the tokenizer). if (!pending_parsing_blocking_script()) @@ -185,19 +202,6 @@ ExceptionOr Document::write(Vector const& strings) return {}; } -// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-writeln -ExceptionOr Document::writeln(Vector const& strings) -{ - - // FIXME: No need to allocate a new vector - Vector new_strings; - for (auto const& element : strings) { - new_strings.append(String::formatted("{}\n", element)); - } - - return write(strings); -} - // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-open ExceptionOr Document::open(String const&, String const&) { diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index c856387c742..f24e540fa73 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -324,6 +324,8 @@ private: void tear_down_layout_tree(); + ExceptionOr run_the_document_write_steps(String); + void increment_referencing_node_count() { VERIFY(!m_deletion_has_begun);