summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_join_split_node_direction_change_command.html
blob: 79faab48061b66fd77b8c6f0c9a3f72ab047b6e3 (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
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
<script>
"use strict";

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async () => {
  let iframe = document.querySelector("iframe");
  async function resetIframe() {
    iframe?.remove();
    iframe = document.createElement("iframe");
    document.body.appendChild(iframe);
    iframe.srcdoc = "<body></body>";
    if (iframe.contentDocument?.readyState != "complete") {
      await new Promise(resolve => iframe.addEventListener("load", resolve, {once: true}));
    }
    iframe.contentWindow.focus();
  }

  await resetIframe();
  await SpecialPowers.pushPrefEnv({
    set: [["editor.join_split_direction.compatible_with_the_other_browsers", false]],
  });
  (function test_command_when_legacy_behavior_is_enabled_by_default() {
    iframe.contentDocument.body.innerHTML = "<div contenteditable><br></div>";
    ok(
      iframe.contentDocument.queryCommandSupported("enableCompatibleJoinSplitDirection"),
      "test_command_when_legacy_behavior_is_enabled_by_default: command should be supported"
    );
    ok(
      iframe.contentDocument.queryCommandEnabled("enableCompatibleJoinSplitDirection"),
      "test_command_when_legacy_behavior_is_enabled_by_default: command should be enabled"
    );
    ok(
      !iframe.contentDocument.queryCommandState("enableCompatibleJoinSplitDirection"),
      "test_command_when_legacy_behavior_is_enabled_by_default: command state should be false"
    );
    is(
      iframe.contentDocument.queryCommandValue("enableCompatibleJoinSplitDirection"),
      "",
      "test_command_when_legacy_behavior_is_enabled_by_default: command value should be empty string"
    );
    ok(
      iframe.contentDocument.execCommand("enableCompatibleJoinSplitDirection", false, "true"),
      "test_command_when_legacy_behavior_is_enabled_by_default: command to enable it should return true"
    );
  })();

  (function test_command_when_enabling_new_behavior_when_legacy_one_is_enabled_by_default() {
    ok(
      iframe.contentDocument.queryCommandSupported("enableCompatibleJoinSplitDirection"),
      "test_command_when_enabling_new_behavior_when_legacy_one_is_enabled_by_default: command should be supported"
    );
    ok(
      iframe.contentDocument.queryCommandEnabled("enableCompatibleJoinSplitDirection"),
      "test_command_when_enabling_new_behavior_when_legacy_one_is_enabled_by_default: command should be enabled"
    );
    ok(
      iframe.contentDocument.queryCommandState("enableCompatibleJoinSplitDirection"),
      "test_command_when_enabling_new_behavior_when_legacy_one_is_enabled_by_default: command state should be true"
    );
    is(
      iframe.contentDocument.queryCommandValue("enableCompatibleJoinSplitDirection"),
      "",
      "test_command_when_enabling_new_behavior_when_legacy_one_is_enabled_by_default: command value should be empty string"
    );

    ok(
      iframe.contentDocument.execCommand("enableCompatibleJoinSplitDirection", false, "false"),
      "test_command_when_enabling_new_behavior_when_legacy_one_is_enabled_by_default: command to disable it should return true"
    );
  })();

  (function test_command_when_disabling_new_behavior_when_the_legacy_one_is_enabled_by_default() {
    ok(
      iframe.contentDocument.queryCommandSupported("enableCompatibleJoinSplitDirection"),
      "test_command_when_disabling_new_behavior_when_the_legacy_one_is_enabled_by_default: command should be supported"
    );
    ok(
      iframe.contentDocument.queryCommandEnabled("enableCompatibleJoinSplitDirection"),
      "test_command_when_disabling_new_behavior_when_the_legacy_one_is_enabled_by_default: command should be enabled"
    );
    ok(
      !iframe.contentDocument.queryCommandState("enableCompatibleJoinSplitDirection"),
      "test_command_when_disabling_new_behavior_when_the_legacy_one_is_enabled_by_default: command state should be false"
    );
    is(
      iframe.contentDocument.queryCommandValue("enableCompatibleJoinSplitDirection"),
      "",
      "test_command_when_disabling_new_behavior_when_the_legacy_one_is_enabled_by_default: command value should be empty string"
    );
  })();

  await resetIframe();
  await SpecialPowers.pushPrefEnv({
    set: [["editor.join_split_direction.compatible_with_the_other_browsers", true]],
  });
  (function test_command_when_new_behavior_is_enabled_by_default() {
    iframe.contentDocument.body.innerHTML = "<div contenteditable><br></div>";
    ok(
      iframe.contentDocument.queryCommandSupported("enableCompatibleJoinSplitDirection"),
      "test_command_when_new_behavior_is_enabled_by_default: command should be supported"
    );
    ok(
      iframe.contentDocument.queryCommandEnabled("enableCompatibleJoinSplitDirection"),
      "test_command_when_new_behavior_is_enabled_by_default: command should be enabled"
    );
    ok(
      iframe.contentDocument.queryCommandState("enableCompatibleJoinSplitDirection"),
      "test_command_when_new_behavior_is_enabled_by_default: command state should be true"
    );
    is(
      iframe.contentDocument.queryCommandValue("enableCompatibleJoinSplitDirection"),
      "",
      "test_command_when_new_behavior_is_enabled_by_default: command value should be empty string"
    );
    ok(
      !iframe.contentDocument.execCommand("enableCompatibleJoinSplitDirection", false, "false"),
      "test_command_when_new_behavior_is_enabled_by_default: command to disable it should return false"
    );
    ok(
      iframe.contentDocument.queryCommandState("enableCompatibleJoinSplitDirection"),
      "test_command_when_new_behavior_is_enabled_by_default: command state should be true even after executing the command to disable it"
    );
  })();

  await resetIframe();
  await SpecialPowers.pushPrefEnv({
    set: [["editor.join_split_direction.compatible_with_the_other_browsers", false]],
  });
  (function test_command_disabled_after_joining_nodes() {
    iframe.contentDocument.body.innerHTML = "<div contenteditable><p>abc</p><p>def</p></div>";
    iframe.contentWindow.getSelection().collapse(iframe.contentDocument.querySelector("p + p").firstChild, 0);
    iframe.contentDocument.execCommand("delete");
    ok(
      !iframe.contentDocument.execCommand("enableCompatibleJoinSplitDirection", false, "true"),
      "test_command_disabled_after_joining_nodes: command should return false"
    );
    ok(
      !iframe.contentDocument.queryCommandEnabled("enableCompatibleJoinSplitDirection"),
      "test_command_when_new_behavior_is_enabled_by_default: command should be disabled"
    );
    ok(
      !iframe.contentDocument.queryCommandState("enableCompatibleJoinSplitDirection"),
      "test_command_when_new_behavior_is_enabled_by_default: command state should be false"
    );
  })();

  await resetIframe();
  await SpecialPowers.pushPrefEnv({
    set: [["editor.join_split_direction.compatible_with_the_other_browsers", false]],
  });
  (function test_command_disabled_after_splitting_node() {
    iframe.contentDocument.body.innerHTML = "<div contenteditable><p>abcdef</p></div>";
    iframe.contentWindow.getSelection().collapse(iframe.contentDocument.querySelector("p").firstChild, "abc".length);
    iframe.contentDocument.execCommand("insertParagraph");
    ok(
      !iframe.contentDocument.execCommand("enableCompatibleJoinSplitDirection", false, "true"),
      "test_command_disabled_after_splitting_node: command should return false"
    );
    ok(
      !iframe.contentDocument.queryCommandEnabled("enableCompatibleJoinSplitDirection"),
      "test_command_disabled_after_splitting_node: command should be disabled"
    );
    ok(
      !iframe.contentDocument.queryCommandState("enableCompatibleJoinSplitDirection"),
      "test_command_disabled_after_splitting_node: command state should be false"
    );
  })();

  await resetIframe();
  await SpecialPowers.pushPrefEnv({
    set: [["editor.join_split_direction.compatible_with_the_other_browsers", false]],
  });
  (function test_split_direction_after_enabling_new_direction() {
    iframe.contentDocument.body.innerHTML = "<div contenteditable><p>abc</p><p>def</p></div>";
    const rightP = iframe.contentDocument.querySelector("p + p");
    iframe.contentWindow.getSelection().collapse(rightP.firstChild, 0);
    iframe.contentDocument.execCommand("delete");
    iframe.contentDocument.execCommand("enableCompatibleJoinSplitDirection", false, "true");
    is(
      iframe.contentDocument.querySelector("p"),
      rightP,
      "test_split_direction_after_enabling_new_direction: left paragraph should be deleted and right paragraph should be alive"
    );
  })();

  SimpleTest.finish();
});
</script>
</head>
<body></body>
</html>