commit 21953f6384e82c6ad50f6eafd9adeecb47ef65af
parent e4338fcdcb24e288142eab0703bed60243d9ed1a
Author: Edgar Chen <echen@mozilla.com>
Date: Tue, 4 Nov 2025 20:44:19 +0000
Bug 1650720 - Part 1: Add plaintext serializer tests for linebreak; r=masayuki
Differential Revision: https://phabricator.services.mozilla.com/D270276
Diffstat:
2 files changed, 132 insertions(+), 0 deletions(-)
diff --git a/dom/serializers/tests/mochitest/mochitest.toml b/dom/serializers/tests/mochitest/mochitest.toml
@@ -123,3 +123,5 @@ skip-if = [
["test_htmlcopyencoder.html"]
["test_htmlcopyencoder.xhtml"]
+
+["test_plaintext_linebreak.html"]
diff --git a/dom/serializers/tests/mochitest/test_plaintext_linebreak.html b/dom/serializers/tests/mochitest/test_plaintext_linebreak.html
@@ -0,0 +1,130 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+<title>Test line breaks for plaintext serializer</title>
+<script src="/tests/SimpleTest/SimpleTest.js"></script>
+<script src="/tests/SimpleTest/EventUtils.js"></script>
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1650720">Mozilla Bug 1650720</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+</div>
+<pre id="preformatted"></pre>
+<div id="container"></div>
+<script>
+
+const de = SpecialPowers.Ci.nsIDocumentEncoder;
+const platformLineBreak = navigator.platform.indexOf("Win") == 0 ? "\r\n" : "\n";
+const CASES = [
+ "\r",
+ "\n",
+ "\r\r",
+ "\r\n",
+ "\n\r",
+ "\n\n",
+ "\r\r\r",
+ "\r\r\n",
+ "\r\n\r",
+ "\r\n\n",
+ "\n\r\r",
+ "\n\r\n",
+ "\n\n\r",
+ "\n\n\n",
+ "\r \n",
+ "\n \r",
+];
+
+function convertLineBreaksForTestResult(aText) {
+ return aText.replace(/\r?\n|\r(?!\n)/g, platformLineBreak);
+}
+
+function selectAndEncode(aElement) {
+ // Select the element.
+ const selection = window.getSelection();
+ selection.removeAllRanges();
+ selection.selectAllChildren(aElement);
+
+ const encoder = SpecialPowers.Cu.createHTMLCopyEncoder();
+ encoder.init(document, "text/plain", de.OutputSelectionOnly);
+ encoder.setSelection(selection);
+ return encoder.encodeToString();
+}
+
+CASES.forEach((lineBreaks) => {
+ const text = `${lineBreaks}First${lineBreaks}Second${lineBreaks}`;
+
+ add_task(async function test_pre_text() {
+ info(`Testing line breaks: ${JSON.stringify(lineBreaks)}`);
+
+ const pre = document.getElementById("preformatted");
+ pre.textContent = text;
+
+ is(selectAndEncode(pre), text.replace(/\r|\n/g, platformLineBreak),
+ `Encoded data for line breaks: ${JSON.stringify(lineBreaks)}`);
+ });
+
+ add_task(async function test_pre_img_alt() {
+ info(`Testing line breaks: ${JSON.stringify(lineBreaks)}`);
+
+ const pre = document.getElementById("preformatted");
+ const img = document.createElement("img");
+ img.alt = text;
+ pre.replaceChildren(img);
+
+ is(selectAndEncode(pre), convertLineBreaksForTestResult(text),
+ `Encoded data for line breaks: ${JSON.stringify(lineBreaks)}`);
+ });
+
+ add_task(async function test_pre_img_title() {
+ info(`Testing line breaks: ${JSON.stringify(lineBreaks)}`);
+
+ const pre = document.getElementById("preformatted");
+ const img = document.createElement("img");
+ img.title = text;
+ pre.replaceChildren(img);
+
+ is(selectAndEncode(pre), ` [${convertLineBreaksForTestResult(text)}] `,
+ `Encoded data for line breaks: ${JSON.stringify(lineBreaks)}`);
+ });
+
+ add_task(async function test_div_text() {
+ info(`Testing line breaks: ${JSON.stringify(lineBreaks)}`);
+
+ const div = document.getElementById("container");
+ div.textContent = text;
+
+ is(selectAndEncode(div), ` First Second `,
+ `Encoded data for line breaks: ${JSON.stringify(lineBreaks)}`);
+ });
+
+ add_task(async function test_div_img_alt() {
+ info(`Testing line breaks: ${JSON.stringify(lineBreaks)}`);
+
+ const div = document.getElementById("container");
+ const img = document.createElement("img");
+ img.alt = text;
+ div.replaceChildren(img);
+
+ // Our plain text serializer keep the first CR/LF.
+ is(selectAndEncode(div), `${lineBreaks[0]}First Second `,
+ `Encoded data for line breaks: ${JSON.stringify(lineBreaks)}`);
+ });
+
+ add_task(async function test_div_img_title() {
+ info(`Testing line breaks: ${JSON.stringify(lineBreaks)}`);
+
+ const div = document.getElementById("container");
+ const img = document.createElement("img");
+ img.title = text;
+ div.replaceChildren(img);
+
+ is(selectAndEncode(div), ` [ First Second ] `,
+ `Encoded data for line breaks: ${JSON.stringify(lineBreaks)}`);
+ });
+});
+
+</script>
+</body>
+</html>