summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_cmd_backgroundColor.html
blob: 7e089ad9f0aafb8887c47b98c08b1a2d32e844a2 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Testing "cmd_backgroundColor" behavior</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<div contenteditable></div>
<script>
"use strict";

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(() => {
  document.execCommand("styleWithCSS", false, "true");
  const commandManager =
    SpecialPowers.wrap(window).
      docShell.
      QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor).
      getInterface(SpecialPowers.Ci.nsICommandManager);
  const editor = document.querySelector("div[contenteditable]");
  editor.style.backgroundColor = "#ff0000";

  editor.innerHTML = "<p>abc</p>";
  editor.getBoundingClientRect();
  editor.focus();
  getSelection().collapse(editor.querySelector("p").firstChild, 1);
  let params = SpecialPowers.Cu.createCommandParams();
  commandManager.getCommandState("cmd_backgroundColor", window, params);
  is(
    params.getCStringValue("state_attribute"),
    "rgb(255, 0, 0)",
    "cmd_backgroundColor should return the editing host's background color (should ignore the transparent paragraph)"
  );

  editor.innerHTML = "<p style=\"background-color: #00ff00\">abc</p>";
  editor.getBoundingClientRect();
  editor.focus();
  getSelection().collapse(editor.querySelector("p").firstChild, 1);
  params = SpecialPowers.Cu.createCommandParams();
  commandManager.getCommandState("cmd_backgroundColor", window, params);
  is(
    params.getCStringValue("state_attribute"),
    "rgb(0, 255, 0)",
    "cmd_backgroundColor should return the paragraph's background color"
  );

  editor.innerHTML = "<span style=\"background-color: #00ff00\">abc</span>";
  editor.getBoundingClientRect();
  editor.focus();
  getSelection().collapse(editor.querySelector("span").firstChild, 1);
  params = SpecialPowers.Cu.createCommandParams();
  commandManager.getCommandState("cmd_backgroundColor", window, params);
  is(
    params.getCStringValue("state_attribute"),
    "rgb(255, 0, 0)",
    "cmd_backgroundColor shouldn't return the span's background color due to inline element"
  );

  editor.innerHTML = "<p style=\"background-color: #00ff00\" contenteditable=\"false\">a<span style=\"background-color: #0000ff\" contenteditable=\"true\">b</span>c</p>";
  editor.getBoundingClientRect();
  editor.focus();
  getSelection().collapse(editor.querySelector("span[contenteditable=true]").firstChild, 1);
  params = SpecialPowers.Cu.createCommandParams();
  commandManager.getCommandState("cmd_backgroundColor", window, params);
  is(
    params.getCStringValue("state_attribute"),
    "rgb(0, 255, 0)",
    "cmd_backgroundColor should return non-editable block element's background color"
  );

  editor.innerHTML = "<p contenteditable=\"false\">a<span style=\"background-color: #0000ff\" contenteditable=\"true\">b</span>c</p>";
  editor.getBoundingClientRect();
  editor.focus();
  getSelection().collapse(editor.querySelector("span[contenteditable=true]").firstChild, 1);
  params = SpecialPowers.Cu.createCommandParams();
  commandManager.getCommandState("cmd_backgroundColor", window, params);
  is(
    params.getCStringValue("state_attribute"),
    "rgb(255, 0, 0)",
    "cmd_backgroundColor should return the parent editing host's background color (should ignore the transparent non-editable paragraph)"
  );

  editor.style.backgroundColor = "#ff0000";
  editor.innerHTML = "<div><p><span>abc</span></p></div>";
  editor.getBoundingClientRect();
  editor.focus();
  getSelection().collapse(editor.querySelector("span").firstChild, 1);
  SpecialPowers.doCommand(window, "cmd_backgroundColor", "#00ff00");
  is(
    editor.outerHTML,
    "<div contenteditable=\"\" style=\"background-color: rgb(255, 0, 0);\"><div><p style=\"background-color: rgb(0, 255, 0);\"><span>abc</span></p></div></div>",
    "cmd_backgroundColor should set background of the closest block element from the caret in a text node"
  );

  editor.style.backgroundColor = "#ff0000";
  editor.innerHTML = "abc";
  editor.getBoundingClientRect();
  editor.focus();
  getSelection().collapse(editor.firstChild, 1);
  SpecialPowers.doCommand(window, "cmd_backgroundColor", "#00ff00");
  is(
    editor.outerHTML,
    "<div contenteditable=\"\" style=\"background-color: rgb(0, 255, 0);\">abc</div>",
    "cmd_backgroundColor should set background of the editing host which is direct block parent from the caret in a text node"
  );

  editor.style.backgroundColor = "#ff0000";
  editor.innerHTML = "<div contenteditable=\"false\"><span contenteditable=\"true\">abc</span></div>";
  editor.getBoundingClientRect();
  editor.focus();
  getSelection().collapse(editor.firstChild, 1);
  SpecialPowers.doCommand(window, "cmd_backgroundColor", "#00ff00");
  is(
    editor.outerHTML,
    "<div contenteditable=\"\" style=\"background-color: rgb(255, 0, 0);\"><div contenteditable=\"false\"><span contenteditable=\"true\">abc</span></div></div>",
    "cmd_backgroundColor should not set background color of inline editing host nor its non-editable parent block"
  );

  editor.style.backgroundColor = "#ff0000";
  editor.innerHTML = "<div><p><span>ab<br>c</span></p></div>";
  editor.getBoundingClientRect();
  editor.focus();
  getSelection().setBaseAndExtent(editor.querySelector("span"), 1, editor.querySelector("span"), 2);
  SpecialPowers.doCommand(window, "cmd_backgroundColor", "#00ff00");
  is(
    editor.outerHTML,
    "<div contenteditable=\"\" style=\"background-color: rgb(255, 0, 0);\"><div><p style=\"background-color: rgb(0, 255, 0);\"><span>ab<br>c</span></p></div></div>",
    "cmd_backgroundColor should set background of the closest block element when selection a leaf element"
  );

  editor.style.backgroundColor = "#ff0000";
  editor.innerHTML = "<div><p><span>abc</span></p></div>";
  editor.getBoundingClientRect();
  editor.focus();
  getSelection().setBaseAndExtent(editor.firstChild, 0, editor.firstChild, 1);
  SpecialPowers.doCommand(window, "cmd_backgroundColor", "#00ff00");
  is(
    editor.outerHTML,
    "<div contenteditable=\"\" style=\"background-color: rgb(255, 0, 0);\"><div><p style=\"background-color: rgb(0, 255, 0);\"><span>abc</span></p></div></div>",
    "cmd_backgroundColor should set background of the selected block element"
  );

  editor.style.backgroundColor = "#ff0000";
  editor.innerHTML = "<div><p><span>abc</span></p><p><span>def</span></p><p><span>ghi</span></p></div>";
  editor.getBoundingClientRect();
  editor.focus();
  getSelection().setBaseAndExtent(editor.querySelector("span").firstChild, 1, editor.querySelector("p + p + p > span").firstChild, 1);
  SpecialPowers.doCommand(window, "cmd_backgroundColor", "#00ff00");
  is(
    editor.outerHTML,
    "<div contenteditable=\"\" style=\"background-color: rgb(255, 0, 0);\"><div><p style=\"background-color: rgb(0, 255, 0);\"><span>abc</span></p>" +
      "<p style=\"background-color: rgb(0, 255, 0);\"><span>def</span></p>" +
      "<p style=\"background-color: rgb(0, 255, 0);\"><span>ghi</span></p></div></div>",
    "cmd_backgroundColor should set background of the paragraph elements in the selection range"
  );

  editor.style.backgroundColor = "#ff0000";
  editor.innerHTML = "<div><p><span>abc</span></p><p><span contenteditable=\"false\">def</span></p><p><span>ghi</span></p></div>";
  editor.getBoundingClientRect();
  editor.focus();
  getSelection().setBaseAndExtent(editor.querySelector("span").firstChild, 1, editor.querySelector("p + p + p > span").firstChild, 1);
  SpecialPowers.doCommand(window, "cmd_backgroundColor", "#00ff00");
  is(
    editor.outerHTML,
    "<div contenteditable=\"\" style=\"background-color: rgb(255, 0, 0);\"><div><p style=\"background-color: rgb(0, 255, 0);\"><span>abc</span></p>" +
      "<p style=\"background-color: rgb(0, 255, 0);\"><span contenteditable=\"false\">def</span></p>" +
      "<p style=\"background-color: rgb(0, 255, 0);\"><span>ghi</span></p></div></div>",
    "cmd_backgroundColor should set background of the paragraph elements in the selection range even if a paragraph has only non-editable content"
  );

  editor.style.backgroundColor = "#ff0000";
  editor.innerHTML = "<div><p><span>abc</span></p><p contenteditable=\"false\"><span>def</span></p><p><span>ghi</span></p></div>";
  editor.getBoundingClientRect();
  editor.focus();
  getSelection().setBaseAndExtent(editor.querySelector("span").firstChild, 1, editor.querySelector("p + p + p > span").firstChild, 1);
  SpecialPowers.doCommand(window, "cmd_backgroundColor", "#00ff00");
  is(
    editor.outerHTML,
    "<div contenteditable=\"\" style=\"background-color: rgb(255, 0, 0);\"><div><p style=\"background-color: rgb(0, 255, 0);\"><span>abc</span></p>" +
      "<p contenteditable=\"false\"><span>def</span></p>" +
      "<p style=\"background-color: rgb(0, 255, 0);\"><span>ghi</span></p></div></div>",
    "cmd_backgroundColor should set background of the paragraph elements in the selection range except the non-editable paragraph"
  );

  editor.style.backgroundColor = "#ff0000";
  editor.innerHTML = "<div><p><span>abc</span></p><span>def</span><p><span>ghi</span></p></div>";
  editor.getBoundingClientRect();
  editor.focus();
  getSelection().setBaseAndExtent(editor.querySelector("span").firstChild, 1, editor.querySelector("p + span + p > span").firstChild, 1);
  SpecialPowers.doCommand(window, "cmd_backgroundColor", "#00ff00");
  is(
    editor.outerHTML,
    "<div contenteditable=\"\" style=\"background-color: rgb(255, 0, 0);\"><div style=\"background-color: rgb(0, 255, 0);\">" +
      "<p style=\"background-color: rgb(0, 255, 0);\"><span>abc</span></p>" +
      "<span>def</span>" +
      "<p style=\"background-color: rgb(0, 255, 0);\"><span>ghi</span></p></div></div>",
    "cmd_backgroundColor should set background of the paragraph elements in the selection range and the container <div> which has inline child"
  );

  editor.style.backgroundColor = "#ff0000";
  editor.innerHTML = "<p><span>abc</span></p><span>def</span><p><span>ghi</span></p>";
  editor.getBoundingClientRect();
  editor.focus();
  getSelection().setBaseAndExtent(editor.querySelector("span").firstChild, 1, editor.querySelector("p + span + p > span").firstChild, 1);
  SpecialPowers.doCommand(window, "cmd_backgroundColor", "#00ff00");
  is(
    editor.outerHTML,
    "<div contenteditable=\"\" style=\"background-color: rgb(0, 255, 0);\">" +
      "<p style=\"background-color: rgb(0, 255, 0);\"><span>abc</span></p>" +
      "<span>def</span>" +
      "<p style=\"background-color: rgb(0, 255, 0);\"><span>ghi</span></p></div>",
    "cmd_backgroundColor should set background of the paragraph elements in the selection range and the editing host which has inline child"
  );

  // TODO: Add testcase for HTML styling mode.

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