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).
This commit is contained in:
Timothy Flynn 2022-02-21 16:34:54 -05:00 committed by Andreas Kling
parent d94db1900e
commit 21bd3a21bd
Notes: sideshowbarker 2024-07-17 18:19:19 +09:00
2 changed files with 22 additions and 16 deletions

View file

@ -150,6 +150,25 @@ void Document::removed_last_ref()
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-write
ExceptionOr<void> Document::write(Vector<String> 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<void> Document::writeln(Vector<String> 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<void> 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<void> Document::write(Vector<String> 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<void> Document::write(Vector<String> const& strings)
return {};
}
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-writeln
ExceptionOr<void> Document::writeln(Vector<String> const& strings)
{
// FIXME: No need to allocate a new vector
Vector<String> 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*> Document::open(String const&, String const&)
{

View file

@ -324,6 +324,8 @@ private:
void tear_down_layout_tree();
ExceptionOr<void> run_the_document_write_steps(String);
void increment_referencing_node_count()
{
VERIFY(!m_deletion_has_begun);