summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/editing/other/white-spaces-after-execCommand-insertlinebreak.tentative.html
blob: a961ee77bc33cd5107b579db19915e613259de15 (plain)
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
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>Testing normalizing white-space sequence after execCommand("insertlinebreak", false, "foo")</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
"use strict";

setup({explicit_done: true});

function runTests() {
  // README:
  // These tests based on the behavior of Chrome 83.  This test does NOT define
  // nor suggest any standard behavior (actually, some expected results might
  // look odd), but this test must help you to understand how other browsers
  // use different logic to normalize white-space sequence.

  document.body.innerHTML = "<div contenteditable></div>";
  let editor = document.querySelector("div[contenteditable]");
  editor.focus();
  let selection = document.getSelection();

  function escape(str) {
    return typeof(str) === "string" ? str.replace(/\u00A0/ig, "&nbsp;") : "";
  }

  function generateWhiteSpaces(num, lastIsAlwaysNBSP) {
    let str = "";
    for (let i = 0; i < num - 1; i++) {
      str += i % 2 ? " " : "\u00A0";
    }
    str += lastIsAlwaysNBSP || num % 2 ? "\u00A0" : " ";
    return escape(str);
  }
  function getDescriptionForTextNode(textNode) {
    return selection.focusNode === textNode ?
      `${escape(textNode.data.slice(0, selection.focusOffset))}[]${escape(textNode.data.slice(selection.focusOffset))}` :
      escape(textNode);
  }

  editor.innerHTML = "a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b";
  selection.collapse(editor.firstChild, 0);
  test(function () {
    document.execCommand("insertlinebreak", false, "");
    assert_equals(editor.innerHTML,
                  `<br>a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b`,
                  "Modified text is wrong");
  }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`);

  editor.innerHTML = "a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b";
  selection.collapse(editor.firstChild, 1);
  test(function () {
    document.execCommand("insertlinebreak", false, "");
    assert_equals(editor.innerHTML,
                  `a<br>${escape(generateWhiteSpaces(9, false))}b`,
                  "Modified text is wrong");
  }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`);

  editor.innerHTML = "a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b";
  selection.collapse(editor.firstChild, 2);
  test(function () {
    document.execCommand("insertlinebreak", false, "");
    assert_equals(editor.innerHTML,
                  `a&nbsp;<br>${escape(generateWhiteSpaces(8, false))}b`,
                  "Modified text is wrong");
  }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`);

  editor.innerHTML = "a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b";
  selection.collapse(editor.firstChild, 3);
  test(function () {
    document.execCommand("insertlinebreak", false, "");
    assert_equals(editor.innerHTML,
                  `a&nbsp;&nbsp;<br>${escape(generateWhiteSpaces(7, false))}b`,
                  "Modified text is wrong");
  }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`);

  editor.innerHTML = "a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b";
  selection.collapse(editor.firstChild, 4);
  test(function () {
    document.execCommand("insertlinebreak", false, "");
    assert_equals(editor.innerHTML,
                  `a&nbsp;&nbsp;&nbsp;<br>${escape(generateWhiteSpaces(6, false))}b`,
                  "Modified text is wrong");
  }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`);

  editor.innerHTML = "a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b";
  selection.collapse(editor.firstChild, 5);
  test(function () {
    document.execCommand("insertlinebreak", false, "");
    assert_equals(editor.innerHTML,
                  `a&nbsp;&nbsp;&nbsp;&nbsp;<br>${escape(generateWhiteSpaces(5, false))}b`,
                  "Modified text is wrong");
  }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`);

  editor.innerHTML = "a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b";
  selection.collapse(editor.firstChild, 6);
  test(function () {
    document.execCommand("insertlinebreak", false, "");
    assert_equals(editor.innerHTML,
                  `a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>${escape(generateWhiteSpaces(4, false))}b`,
                  "Modified text is wrong");
  }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`);

  editor.innerHTML = "a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b";
  selection.collapse(editor.firstChild, 7);
  test(function () {
    document.execCommand("insertlinebreak", false, "");
    assert_equals(editor.innerHTML,
                  `a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>${escape(generateWhiteSpaces(3, false))}b`,
                  "Modified text is wrong");
  }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`);

  editor.innerHTML = "a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b";
  selection.collapse(editor.firstChild, 8);
  test(function () {
    document.execCommand("insertlinebreak", false, "");
    assert_equals(editor.innerHTML,
                  `a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>${escape(generateWhiteSpaces(2, false))}b`,
                  "Modified text is wrong");
  }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`);

  editor.innerHTML = "a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b";
  selection.collapse(editor.firstChild, 9);
  test(function () {
    document.execCommand("insertlinebreak", false, "");
    assert_equals(editor.innerHTML,
                  `a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>${escape(generateWhiteSpaces(1, false))}b`,
                  "Modified text is wrong");
  }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`);

  editor.innerHTML = "a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b";
  selection.collapse(editor.firstChild, 10);
  test(function () {
    document.execCommand("insertlinebreak", false, "");
    assert_equals(editor.innerHTML,
                  `a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>b`,
                  "Modified text is wrong");
  }, `execCommand("insertlinebreak", false, "") at "${getDescriptionForTextNode(editor.firstChild)}"`);

  done();
}

window.addEventListener("load", runTests, {once: true});
</script>
</body>
</html>