File: insertLineBreak.html

package info (click to toggle)
firefox 147.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 4,683,324 kB
  • sloc: cpp: 7,607,156; javascript: 6,532,492; ansic: 3,775,158; python: 1,415,368; xml: 634,556; asm: 438,949; java: 186,241; sh: 62,751; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (198 lines) | stat: -rw-r--r-- 7,330 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="timeout" content="long">
<meta name="variant" content="?white-space=normal">
<meta name="variant" content="?white-space=pre">
<meta name="variant" content="?white-space=pre-line">
<meta name="variant" content="?white-space=pre-wrap">
<title>Inserting a line break in plaintext-only</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="../include/editor-test-utils.js"></script>
<script>
"use strict";

const searchParams = new URLSearchParams(document.location.search);
const whiteSpace = searchParams.get("white-space");
const useBR = whiteSpace == "normal";
const collapseWhiteSpaces = whiteSpace == "normal" || whiteSpace == "pre-line";
const isSafari = navigator.platform.includes("Mac") &&
    navigator.userAgent.includes("Safari") &&
    !navigator.userAgent.includes("Chrome");

addEventListener("load", () => {
  const editingHost = document.createElement("div");
  editingHost.style.whiteSpace = whiteSpace;
  editingHost.setAttribute("contenteditable", "plaintext-only");
  document.body.appendChild(editingHost);
  editingHost.focus();
  editingHost.getBoundingClientRect();
  const utils = new EditorTestUtils(editingHost);

  let lastBeforeInputEvent;
  editingHost.addEventListener("beforeinput", event => lastBeforeInputEvent = event);

  // When a preformatted linefeed is preferred, it should be used instead of <br> for making the
  // content can be retrieved with `.textContent`.  However, padding line breaks which is required
  // for empty block or empty last line should be <br> because it won't appear in `.textContent`.
  // So, it helps to make the `.textContent` result better.
  for (const data of [
    {
      initialInnerHTML: "A[]B",
      expected: useBR ? "A<br>B" : "A\nB",
    },
    {
      initialInnerHTML: "<p>{}<br></p>",
      expected: useBR ? "<p><br><br></p>" : "<p>\n<br></p>",
    },
    {
      initialInnerHTML: `<p style="white-space:normal">A[]B</p>`,
      expected: `<p style="white-space:normal">A<br>B</p>`,
    },
    {
      initialInnerHTML: `<p style="white-space:pre">A[]B</p>`,
      expected: [`<p style="white-space:pre">A<br>B</p>`, `<p style="white-space:pre">A\nB</p>`],
    },
    {
      initialInnerHTML: `<p style="white-space:pre-line">A[]B</p>`,
      expected: [`<p style="white-space:pre-line">A<br>B</p>`, `<p style="white-space:pre-line">A\nB</p>`],
    },
    {
      initialInnerHTML: `<p style="white-space:pre-wrap">A[]B</p>`,
      expected: [`<p style="white-space:pre-wrap">A<br>B</p>`, `<p style="white-space:pre-wrap">A\nB</p>`],
    },
    {
      initialInnerHTML: "<ul><li>[]AB</li></ul>",
      expected: useBR ? "<ul><li><br>AB</li></ul>" : "<ul><li>\nAB</li></ul>",
    },
    {
      initialInnerHTML: "<ul><li>A[]B</li></ul>",
      expected: useBR ? "<ul><li>A<br>B</li></ul>" : "<ul><li>A\nB</li></ul>",
    },
    {
      initialInnerHTML: "<ul><li>AB[]</li></ul>",
      expected: useBR
        ? "<ul><li>AB<br><br></li></ul>"
        : "<ul><li>AB\n<br></li></ul>",
    },
    {
      initialInnerHTML: "<dl><dt>[]AB</dt></dl>",
      expected: useBR ? "<dl><dt><br>AB</dt></dl>" : "<dl><dt>\nAB</dt></dl>",
    },
    {
      initialInnerHTML: "<dl><dt>A[]B</dt></dl>",
      expected: useBR ? "<dl><dt>A<br>B</dt></dl>" : "<dl><dt>A\nB</dt></dl>",
    },
    {
      initialInnerHTML: "<dl><dt>AB[]</dt></dl>",
      expected: useBR
        ? "<dl><dt>AB<br><br></dt></dl>"
        : "<dl><dt>AB\n<br></dt></dl>",
    },
    {
      initialInnerHTML: "<dl><dd>[]AB</dd></dl>",
      expected: useBR ? "<dl><dd><br>AB</dd></dl>" : "<dl><dd>\nAB</dd></dl>",
    },
    {
      initialInnerHTML: "<dl><dd>A[]B</dd></dl>",
      expected: useBR ? "<dl><dd>A<br>B</dd></dl>" : "<dl><dd>A\nB</dd></dl>",
    },
    {
      initialInnerHTML: "<dl><dd>AB[]</dd></dl>",
      expected: useBR
        ? "<dl><dd>AB<br><br></dd></dl>"
        : "<dl><dd>AB\n<br></dd></dl>",
    },
    {
      initialInnerHTML: "<table><tbody><tr><td>[]AB</td></tr></tbody></table>",
      expected: useBR
        ? "<table><tbody><tr><td><br>AB</td></tr></tbody></table>"
        : "<table><tbody><tr><td>\nAB</td></tr></tbody></table>",
    },
    {
      initialInnerHTML: "<table><tbody><tr><td>A[]B</td></tr></tbody></table>",
      expected: useBR
        ? "<table><tbody><tr><td>A<br>B</td></tr></tbody></table>"
        : "<table><tbody><tr><td>A\nB</td></tr></tbody></table>",
    },
    {
      initialInnerHTML: "<table><tbody><tr><td>AB[]</td></tr></tbody></table>",
      expected: useBR
        ? "<table><tbody><tr><td>AB<br><br></td></tr></tbody></table>"
        : "<table><tbody><tr><td>AB\n<br></td></tr></tbody></table>",
    },
    {
      initialInnerHTML: "<h1>[]AB</h1>",
      expected: useBR ? "<h1><br>AB</h1>" : "<h1>\nAB</h1>",
    },
    {
      initialInnerHTML: "<h1>A[]B</h1>",
      expected: useBR ? "<h1>A<br>B</h1>" : "<h1>A\nB</h1>",
    },
    {
      initialInnerHTML: "<h1>AB[]</h1>",
      expected: useBR ? "<h1>AB<br><br></h1>" : "<h1>AB\n<br></h1>",
    },
  ]) {
    test(() => {
      utils.setupEditingHost(data.initialInnerHTML);
      document.execCommand("insertLineBreak");
      if (Array.isArray(data.expected)) {
        assert_in_array(editingHost.innerHTML, data.expected);
      } else {
        assert_equals(editingHost.innerHTML, data.expected);
      }
    }, `execCommand("insertLineBreak") when ${data.initialInnerHTML}`);
    promise_test(async t => {
      utils.setupEditingHost(data.initialInnerHTML);
      lastBeforeInputEvent = undefined;
      await utils.sendEnterKey();
      test(() => {
        assert_equals(
          lastBeforeInputEvent?.inputType,
          "insertLineBreak",
          `inputType should be "insertLineBreak"`
        );
      }, `${t.name}: beforeinput`);
      test(() => {
        if (Array.isArray(data.expected)) {
          assert_in_array(editingHost.innerHTML, data.expected);
        } else {
          assert_equals(editingHost.innerHTML, data.expected);
        }
      }, `${t.name}: editingHost.innerHTML`);
    }, `Pressing Enter when ${
      data.initialInnerHTML
    } should cause "insertLineBreak" and shouldn't insert new paragraph`);
    promise_test(async t => {
      utils.setupEditingHost(data.initialInnerHTML);
      lastBeforeInputEvent = undefined;
      await utils.sendEnterKey(isSafari ? utils.kControl : utils.kShift);
      test(() => {
        assert_equals(
          lastBeforeInputEvent?.inputType,
          "insertLineBreak",
          `inputType should be "insertLineBreak"`
        );
      }, `${t.name}: beforeinput`);
      test(() => {
        if (Array.isArray(data.expected)) {
          assert_in_array(editingHost.innerHTML, data.expected);
        } else {
          assert_equals(editingHost.innerHTML, data.expected);
        }
      }, `${t.name}: editingHost.innerHTML`);
    }, `Pressing ${isSafari ? "Ctrl" : "Shift"}+Enter when ${
      data.initialInnerHTML
    } should cause "insertLineBreak" and shouldn't insert new paragraph`);
  }
}, {once: true});
</script>
</head>
<body></body>
</html>