Skip to content

Truly independent web browser

LibWeb: Insert plain text for insertHTML in plaintext-only hosts

Andreas Kling20a09f078481
diff --git a/Libraries/LibWeb/Editing/Commands.cpp b/Libraries/LibWeb/Editing/Commands.cpp
index 07a203a7fab..f09b23e083e 100644
--- a/Libraries/LibWeb/Editing/Commands.cpp
+++ b/Libraries/LibWeb/Editing/Commands.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <LibGC/ConservativeVector.h>
#include <LibGC/RootVector.h>
#include <LibUnicode/CharacterTypes.h>
#include <LibUnicode/Segmenter.h>
@@ -1274,6 +1275,96 @@ bool command_insert_horizontal_rule_action(DOM::Document& document, Utf16View)
return true;
}
+enum class InsertTextContext {
+ Typing,
+ TextReplacement,
+ OtherReplacement,
+};
+
+static bool insert_text(DOM::Document&, Utf16View, InsertTextContext);
+
+// INTEROP: In a plaintext-only editing host, browsers insert the text that HTML renders as instead of its nodes. This
+// returns that text for a parsed fragment. Content that is not rendered by default, such as scripts and the
+// head, is left out. Block-level elements and br elements separate lines, and white-space collapses outside
+// of preformatted elements.
+static Utf16String plain_text_of_fragment(DOM::DocumentFragment& fragment)
+{
+ Utf16StringBuilder builder;
+ bool at_line_start = true;
+ bool pending_space = false;
+ bool pending_line_break = false;
+
+ auto flush_line_break = [&] {
+ if (pending_line_break && !builder.is_empty()) {
+ builder.append_code_unit(u'\n');
+ at_line_start = true;
+ pending_space = false;
+ }
+ pending_line_break = false;
+ };
+
+ // NB: This walks the tree with a stack instead of recursion, since script can insert deeply nested HTML.
+ struct PendingNode {
+ GC::Ref<DOM::Node> node;
+ bool preserves_white_space;
+ bool is_block_end;
+ };
+ GC::ConservativeVector<PendingNode> pending_nodes;
+ pending_nodes.append({ fragment, false, false });
+ while (!pending_nodes.is_empty()) {
+ auto [node, preserves_white_space, is_block_end] = pending_nodes.take_last();
+ if (is_block_end) {
+ pending_line_break = true;
+ continue;
+ }
+
+ if (auto* text = as_if<DOM::Text>(*node)) {
+ for (auto code_unit : text->data().utf16_view()) {
+ bool is_white_space = code_unit == ' ' || code_unit == '\t' || code_unit == '\n' || code_unit == '\r' || code_unit == '\f';
+ if (!preserves_white_space && is_white_space) {
+ pending_space = !at_line_start;
+ continue;
+ }
+ flush_line_break();
+ if (pending_space) {
+ builder.append_code_unit(u' ');
+ pending_space = false;
+ }
+ builder.append_code_unit(code_unit);
+ at_line_start = code_unit == u'\n';
+ }
+ continue;
+ }
+
+ auto* element = as_if<DOM::Element>(*node);
+ if (element) {
+ if (element->local_name().is_one_of(HTML::TagNames::head, HTML::TagNames::title, HTML::TagNames::script,
+ HTML::TagNames::style, HTML::TagNames::noscript, HTML::TagNames::noframes, HTML::TagNames::template_,
+ HTML::TagNames::meta, HTML::TagNames::link, HTML::TagNames::base))
+ continue;
+ if (is<HTML::HTMLBRElement>(*element)) {
+ flush_line_break();
+ builder.append_code_unit(u'\n');
+ at_line_start = true;
+ pending_space = false;
+ continue;
+ }
+ preserves_white_space = preserves_white_space
+ || element->local_name().is_one_of(HTML::TagNames::pre, HTML::TagNames::listing, HTML::TagNames::xmp,
+ HTML::TagNames::plaintext, HTML::TagNames::textarea);
+ }
+
+ bool is_block = element && is_prohibited_paragraph_child_name(element->local_name());
+ if (is_block) {
+ pending_line_break = true;
+ pending_nodes.append({ node, preserves_white_space, true });
+ }
+ for (auto* child = node->last_child(); child; child = child->previous_sibling())
+ pending_nodes.append({ *child, preserves_white_space, false });
+ }
+ return builder.to_string();
+}
+
// https://w3c.github.io/editing/docs/execCommand/#the-inserthtml-command
bool command_insert_html_action(DOM::Document& document, Utf16View value)
{
@@ -1295,6 +1386,28 @@ bool command_insert_html_action(DOM::Document& document, Utf16View value)
if (!range->start_container()->is_editable_or_editing_host())
return true;
+ // NB: See the INTEROP note at plain_text_of_fragment(). Insert the lines as text, separated by line breaks.
+ if (auto editing_host = range->start_container()->editing_host(); is<HTML::HTMLElement>(editing_host.ptr())
+ && as<HTML::HTMLElement>(*editing_host).content_editable_state() == HTML::ContentEditableState::PlaintextOnly) {
+ GC::Ptr<DOM::DocumentFragment> fragment;
+ {
+ EditingHistory::ProxyMutationScope mutation_scope { *range->start_container() };
+ auto result = range->create_contextual_fragment(resulting_value);
+ if (result.is_exception())
+ return false;
+ fragment = result.release_value();
+ }
+ auto text = plain_text_of_fragment(*fragment);
+ auto lines = text.utf16_view().split_view(u'\n', SplitBehavior::KeepEmpty);
+ for (size_t i = 0; i < lines.size(); ++i) {
+ if (i > 0)
+ command_insert_linebreak_action(document, {});
+ if (!lines[i].is_empty())
+ insert_text(document, lines[i], InsertTextContext::Typing);
+ }
+ return true;
+ }
+
// INTEROP: Blink and WebKit route both inline and block rich paste through their selection-replacement command.
// Keeping fragment parsing, inserted-range tracking, and completion in that command prevents the two
// structural cases from acquiring different whitespace and caret behavior.
@@ -1988,12 +2101,6 @@ bool command_insert_paragraph_action(DOM::Document& document, Utf16View)
return true;
}
-enum class InsertTextContext {
- Typing,
- TextReplacement,
- OtherReplacement,
-};
-
// https://w3c.github.io/editing/docs/execCommand/#the-inserttext-command
static bool insert_text(DOM::Document& document, Utf16View value, InsertTextContext context)
{
diff --git a/Tests/LibWeb/Text/expected/Editing/execCommand-insertHTML-plaintext-xhtml-range.txt b/Tests/LibWeb/Text/expected/Editing/execCommand-insertHTML-plaintext-xhtml-range.txt
new file mode 100644
index 00000000000..5cc5bee4117
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Editing/execCommand-insertHTML-plaintext-xhtml-range.txt
@@ -0,0 +1,9 @@
+XHTML front door: InvalidStateError
+Foreign range: true
+Command enabled: true
+Unclosed tag: false
+Unbound prefix: false
+XHTML text: abcd
+HTML text: HTML
+Following command: true
+Following text: HTML!
diff --git a/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=normal.txt b/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=normal.txt
index 5b83f22f9d3..84d94e7a0a6 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=normal.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=normal.txt
@@ -1,28 +1,28 @@
-Harness status: Error
+Harness status: OK
Found 20 tests
-2 Pass
-18 Fail
+16 Pass
+4 Fail
Pass execCommand("insertHTML", false, "plaintext") when ""
-Fail execCommand("insertHTML", false, "1st line
+Pass execCommand("insertHTML", false, "1st line
2nd line") when ""
Fail execCommand("insertHTML", false, "<pre>1st line
2nd line</pre>") when ""
-Fail execCommand("insertHTML", false, "plaintext") when "<b>{}</b>"
-Fail execCommand("insertHTML", false, "plaintext") when "<b>{}<br></b>"
-Fail execCommand("insertHTML", false, "plaintext") when "<b>A[]B</b>"
-Fail execCommand("insertHTML", false, "plaintext") when "<span>A[]B</span>"
-Fail execCommand("insertHTML", false, "<div>abc</div>") when ""
+Pass execCommand("insertHTML", false, "plaintext") when "<b>{}</b>"
+Pass execCommand("insertHTML", false, "plaintext") when "<b>{}<br></b>"
+Pass execCommand("insertHTML", false, "plaintext") when "<b>A[]B</b>"
+Pass execCommand("insertHTML", false, "plaintext") when "<span>A[]B</span>"
+Pass execCommand("insertHTML", false, "<div>abc</div>") when ""
Fail execCommand("insertHTML", false, "<div>abc<br>def</div>") when ""
Fail execCommand("insertHTML", false, "<div>abc</div><div>def</div>") when ""
Fail execCommand("insertHTML", false, "<div>abc<br>def</div><div>ghi<br>jkl</div>") when ""
-Fail execCommand("insertHTML", false, "<noscript>no script</noscript>") when ""
-Fail execCommand("insertHTML", false, "<noframes>no frames</noframes>") when ""
-Fail execCommand("insertHTML", false, "<script>script</script>") when ""
-Fail execCommand("insertHTML", false, "<style>style</style>") when ""
-Fail execCommand("insertHTML", false, "<html><head><title>title</title></head><body>body</body></html>") when ""
-Fail execCommand("insertHTML", false, "plain text") when ""
-Fail execCommand("insertHTML", false, "<span>plain text</span>") when ""
-Fail execCommand("insertHTML", false, "<pre>plain text</pre>") when ""
+Pass execCommand("insertHTML", false, "<noscript>no script</noscript>") when ""
+Pass execCommand("insertHTML", false, "<noframes>no frames</noframes>") when ""
+Pass execCommand("insertHTML", false, "<script>script</script>") when ""
+Pass execCommand("insertHTML", false, "<style>style</style>") when ""
+Pass execCommand("insertHTML", false, "<html><head><title>title</title></head><body>body</body></html>") when ""
+Pass execCommand("insertHTML", false, "plain text") when ""
+Pass execCommand("insertHTML", false, "<span>plain text</span>") when ""
+Pass execCommand("insertHTML", false, "<pre>plain text</pre>") when ""
Pass execCommand("insertHTML", false, "") when "A[B]C"
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=pre-line.txt b/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=pre-line.txt
index 5b83f22f9d3..84d94e7a0a6 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=pre-line.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=pre-line.txt
@@ -1,28 +1,28 @@
-Harness status: Error
+Harness status: OK
Found 20 tests
-2 Pass
-18 Fail
+16 Pass
+4 Fail
Pass execCommand("insertHTML", false, "plaintext") when ""
-Fail execCommand("insertHTML", false, "1st line
+Pass execCommand("insertHTML", false, "1st line
2nd line") when ""
Fail execCommand("insertHTML", false, "<pre>1st line
2nd line</pre>") when ""
-Fail execCommand("insertHTML", false, "plaintext") when "<b>{}</b>"
-Fail execCommand("insertHTML", false, "plaintext") when "<b>{}<br></b>"
-Fail execCommand("insertHTML", false, "plaintext") when "<b>A[]B</b>"
-Fail execCommand("insertHTML", false, "plaintext") when "<span>A[]B</span>"
-Fail execCommand("insertHTML", false, "<div>abc</div>") when ""
+Pass execCommand("insertHTML", false, "plaintext") when "<b>{}</b>"
+Pass execCommand("insertHTML", false, "plaintext") when "<b>{}<br></b>"
+Pass execCommand("insertHTML", false, "plaintext") when "<b>A[]B</b>"
+Pass execCommand("insertHTML", false, "plaintext") when "<span>A[]B</span>"
+Pass execCommand("insertHTML", false, "<div>abc</div>") when ""
Fail execCommand("insertHTML", false, "<div>abc<br>def</div>") when ""
Fail execCommand("insertHTML", false, "<div>abc</div><div>def</div>") when ""
Fail execCommand("insertHTML", false, "<div>abc<br>def</div><div>ghi<br>jkl</div>") when ""
-Fail execCommand("insertHTML", false, "<noscript>no script</noscript>") when ""
-Fail execCommand("insertHTML", false, "<noframes>no frames</noframes>") when ""
-Fail execCommand("insertHTML", false, "<script>script</script>") when ""
-Fail execCommand("insertHTML", false, "<style>style</style>") when ""
-Fail execCommand("insertHTML", false, "<html><head><title>title</title></head><body>body</body></html>") when ""
-Fail execCommand("insertHTML", false, "plain text") when ""
-Fail execCommand("insertHTML", false, "<span>plain text</span>") when ""
-Fail execCommand("insertHTML", false, "<pre>plain text</pre>") when ""
+Pass execCommand("insertHTML", false, "<noscript>no script</noscript>") when ""
+Pass execCommand("insertHTML", false, "<noframes>no frames</noframes>") when ""
+Pass execCommand("insertHTML", false, "<script>script</script>") when ""
+Pass execCommand("insertHTML", false, "<style>style</style>") when ""
+Pass execCommand("insertHTML", false, "<html><head><title>title</title></head><body>body</body></html>") when ""
+Pass execCommand("insertHTML", false, "plain text") when ""
+Pass execCommand("insertHTML", false, "<span>plain text</span>") when ""
+Pass execCommand("insertHTML", false, "<pre>plain text</pre>") when ""
Pass execCommand("insertHTML", false, "") when "A[B]C"
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=pre-wrap.txt b/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=pre-wrap.txt
index 5b83f22f9d3..84d94e7a0a6 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=pre-wrap.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=pre-wrap.txt
@@ -1,28 +1,28 @@
-Harness status: Error
+Harness status: OK
Found 20 tests
-2 Pass
-18 Fail
+16 Pass
+4 Fail
Pass execCommand("insertHTML", false, "plaintext") when ""
-Fail execCommand("insertHTML", false, "1st line
+Pass execCommand("insertHTML", false, "1st line
2nd line") when ""
Fail execCommand("insertHTML", false, "<pre>1st line
2nd line</pre>") when ""
-Fail execCommand("insertHTML", false, "plaintext") when "<b>{}</b>"
-Fail execCommand("insertHTML", false, "plaintext") when "<b>{}<br></b>"
-Fail execCommand("insertHTML", false, "plaintext") when "<b>A[]B</b>"
-Fail execCommand("insertHTML", false, "plaintext") when "<span>A[]B</span>"
-Fail execCommand("insertHTML", false, "<div>abc</div>") when ""
+Pass execCommand("insertHTML", false, "plaintext") when "<b>{}</b>"
+Pass execCommand("insertHTML", false, "plaintext") when "<b>{}<br></b>"
+Pass execCommand("insertHTML", false, "plaintext") when "<b>A[]B</b>"
+Pass execCommand("insertHTML", false, "plaintext") when "<span>A[]B</span>"
+Pass execCommand("insertHTML", false, "<div>abc</div>") when ""
Fail execCommand("insertHTML", false, "<div>abc<br>def</div>") when ""
Fail execCommand("insertHTML", false, "<div>abc</div><div>def</div>") when ""
Fail execCommand("insertHTML", false, "<div>abc<br>def</div><div>ghi<br>jkl</div>") when ""
-Fail execCommand("insertHTML", false, "<noscript>no script</noscript>") when ""
-Fail execCommand("insertHTML", false, "<noframes>no frames</noframes>") when ""
-Fail execCommand("insertHTML", false, "<script>script</script>") when ""
-Fail execCommand("insertHTML", false, "<style>style</style>") when ""
-Fail execCommand("insertHTML", false, "<html><head><title>title</title></head><body>body</body></html>") when ""
-Fail execCommand("insertHTML", false, "plain text") when ""
-Fail execCommand("insertHTML", false, "<span>plain text</span>") when ""
-Fail execCommand("insertHTML", false, "<pre>plain text</pre>") when ""
+Pass execCommand("insertHTML", false, "<noscript>no script</noscript>") when ""
+Pass execCommand("insertHTML", false, "<noframes>no frames</noframes>") when ""
+Pass execCommand("insertHTML", false, "<script>script</script>") when ""
+Pass execCommand("insertHTML", false, "<style>style</style>") when ""
+Pass execCommand("insertHTML", false, "<html><head><title>title</title></head><body>body</body></html>") when ""
+Pass execCommand("insertHTML", false, "plain text") when ""
+Pass execCommand("insertHTML", false, "<span>plain text</span>") when ""
+Pass execCommand("insertHTML", false, "<pre>plain text</pre>") when ""
Pass execCommand("insertHTML", false, "") when "A[B]C"
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=pre.txt b/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=pre.txt
index 5b83f22f9d3..84d94e7a0a6 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=pre.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/editing/plaintext-only/insertHTML@white-space=pre.txt
@@ -1,28 +1,28 @@
-Harness status: Error
+Harness status: OK
Found 20 tests
-2 Pass
-18 Fail
+16 Pass
+4 Fail
Pass execCommand("insertHTML", false, "plaintext") when ""
-Fail execCommand("insertHTML", false, "1st line
+Pass execCommand("insertHTML", false, "1st line
2nd line") when ""
Fail execCommand("insertHTML", false, "<pre>1st line
2nd line</pre>") when ""
-Fail execCommand("insertHTML", false, "plaintext") when "<b>{}</b>"
-Fail execCommand("insertHTML", false, "plaintext") when "<b>{}<br></b>"
-Fail execCommand("insertHTML", false, "plaintext") when "<b>A[]B</b>"
-Fail execCommand("insertHTML", false, "plaintext") when "<span>A[]B</span>"
-Fail execCommand("insertHTML", false, "<div>abc</div>") when ""
+Pass execCommand("insertHTML", false, "plaintext") when "<b>{}</b>"
+Pass execCommand("insertHTML", false, "plaintext") when "<b>{}<br></b>"
+Pass execCommand("insertHTML", false, "plaintext") when "<b>A[]B</b>"
+Pass execCommand("insertHTML", false, "plaintext") when "<span>A[]B</span>"
+Pass execCommand("insertHTML", false, "<div>abc</div>") when ""
Fail execCommand("insertHTML", false, "<div>abc<br>def</div>") when ""
Fail execCommand("insertHTML", false, "<div>abc</div><div>def</div>") when ""
Fail execCommand("insertHTML", false, "<div>abc<br>def</div><div>ghi<br>jkl</div>") when ""
-Fail execCommand("insertHTML", false, "<noscript>no script</noscript>") when ""
-Fail execCommand("insertHTML", false, "<noframes>no frames</noframes>") when ""
-Fail execCommand("insertHTML", false, "<script>script</script>") when ""
-Fail execCommand("insertHTML", false, "<style>style</style>") when ""
-Fail execCommand("insertHTML", false, "<html><head><title>title</title></head><body>body</body></html>") when ""
-Fail execCommand("insertHTML", false, "plain text") when ""
-Fail execCommand("insertHTML", false, "<span>plain text</span>") when ""
-Fail execCommand("insertHTML", false, "<pre>plain text</pre>") when ""
+Pass execCommand("insertHTML", false, "<noscript>no script</noscript>") when ""
+Pass execCommand("insertHTML", false, "<noframes>no frames</noframes>") when ""
+Pass execCommand("insertHTML", false, "<script>script</script>") when ""
+Pass execCommand("insertHTML", false, "<style>style</style>") when ""
+Pass execCommand("insertHTML", false, "<html><head><title>title</title></head><body>body</body></html>") when ""
+Pass execCommand("insertHTML", false, "plain text") when ""
+Pass execCommand("insertHTML", false, "<span>plain text</span>") when ""
+Pass execCommand("insertHTML", false, "<pre>plain text</pre>") when ""
Pass execCommand("insertHTML", false, "") when "A[B]C"
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/Editing/execCommand-insertHTML-plaintext-xhtml-range.html b/Tests/LibWeb/Text/input/Editing/execCommand-insertHTML-plaintext-xhtml-range.html
new file mode 100644
index 00000000000..2206743189b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Editing/execCommand-insertHTML-plaintext-xhtml-range.html
@@ -0,0 +1,43 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const xhtml = new DOMParser().parseFromString(
+ '<html xmlns="http://www.w3.org/1999/xhtml"><body contenteditable="plaintext-only">abcd</body></html>',
+ "application/xhtml+xml"
+ );
+ try {
+ xhtml.execCommand("insertHTML", false, "<b>");
+ println("XHTML front door: unexpectedly succeeded");
+ } catch (error) {
+ println(`XHTML front door: ${error.name}`);
+ }
+
+ const host = document.createElement("div");
+ host.contentEditable = "plaintext-only";
+ host.textContent = "HTML";
+ document.body.append(host);
+ const selection = getSelection();
+ selection.removeAllRanges();
+ const range = document.createRange();
+ range.setStart(host.firstChild, 0);
+ range.collapse(true);
+ selection.addRange(range);
+
+ // Mutating an already-associated live range can move its boundaries to a different document.
+ // The HTML caller still has an active range, whose plaintext host needs the XML fragment parser.
+ const xhtmlHost = xhtml.documentElement.firstElementChild;
+ range.setStart(xhtmlHost.firstChild, 2);
+ println(`Foreign range: ${selection.anchorNode === xhtmlHost.firstChild}`);
+ println(`Command enabled: ${document.queryCommandEnabled("insertHTML")}`);
+ println(`Unclosed tag: ${document.execCommand("insertHTML", false, "<b>")}`);
+ println(`Unbound prefix: ${document.execCommand("insertHTML", false, "<unbound:span/>")}`);
+ println(`XHTML text: ${xhtmlHost.textContent}`);
+ println(`HTML text: ${host.textContent}`);
+
+ // A failed parse must also release the editing-command recursion and history guards.
+ selection.collapse(host.firstChild, host.firstChild.length);
+ println(`Following command: ${document.execCommand("insertText", false, "!")}`);
+ println(`Following text: ${host.textContent}`);
+ });
+</script>