summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_nsIHTMLEditor_setBackgroundColor.html
blob: 1d67e7b688384fad4fc713e3256ee37f7d4a0973 (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
<!DOCTYPE>
<html>
<head>
  <title>Test for nsIHTMLEditor.setBackgroundColor()</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<div id="display">
</div>
<div id="content" contenteditable></div>
<pre id="test">
</pre>

<script class="testbody" type="application/javascript">
"use strict";

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(() => {
  let editor = document.getElementById("content");
  let selection = document.getSelection();

  (function test_with_selecting_table_cells() {
    editor.innerHTML =
      '<table id="table">' +
        '<tr id="r1"><td id="c1-1">cell1-1</td><td id="c1-2">cell1-2</td><td id="c1-3">cell1-3</td><td id="c1-4" colspan="2" rowspan="2">cell1-4</td></tr>' +
        '<tr id="r2"><th id="c2-1" rowspan="2">cell2-1</th><td id="c2-2">cell2-2</td><td id="c2-3">cell2-3</td></tr>' +
        '<tr id="r3"><td id="c3-2">cell3-2</td><td id="c3-3">cell3-3</td><td id="c3-4" colspan="2">cell3-4</td></tr>' +
        '<tr id="r4"><td id="c4-1" rowspan="4">cell4-1</td><td id="c4-2">cell4-2</td><td id="c4-3">cell4-3</td><th id="c4-4">cell4-4</th><td id="c4-5">cell4-5</td></tr>' +
        '<tr id="r5"><th id="c5-2">cell5-2</th><th id="c5-3" colspan="2">' +
          '<table><tr id="r2-1"><td id="c2-1-1">cell2-1-1</td></tr></table>' +
          '</th><td id="c5-5">cell5-5</td></tr>' +
        '<tr id="r6"><td id="c6-2">cell6-2</td><td id="c6-3">cell6-3</td><td id="c6-4"><p>cell6-4</p></td><td id="c6-5">cell6-5</td></tr>' +
        '<tr id="r7"><td id="c7-2" colspan="4">cell7-2</td></tr>' +
      "</table>";

    (function test_without_CSS() {
      (function test_with_selecting_1_1() {
        let cell = document.getElementById("c1-1");
        let range = document.createRange();
        range.selectNode(cell);
        selection.removeAllRanges();
        selection.addRange(range);
        getHTMLEditor().setBackgroundColor("#ff0000");
        is(cell.getAttribute("bgcolor"), "#ff0000",
          '#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the first cell element in the first row');
        ok(!cell.nextSibling.hasAttribute("bgcolor"),
          '#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the other cells');
        ok(!cell.parentNode.hasAttribute("bgcolor"),
          '#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the parent row');
        ok(!cell.parentNode.parentNode.hasAttribute("bgcolor"),
          '#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the parent tbody');
        ok(!cell.parentNode.parentNode.parentNode.hasAttribute("bgcolor"),
          '#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the parent table');
        ok(!editor.hasAttribute("bgcolor"),
          '#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the editing host');
        ok(!document.body.hasAttribute("bgcolor"),
          '#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the body');
        ok(!document.documentElement.hasAttribute("bgcolor"),
          '#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the html');

        getHTMLEditor().setBackgroundColor("");
        ok(!cell.hasAttribute("bgcolor"),
          '#1-1 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the first cell element in the first row');
      })();

      (function test_with_selecting_2_2_and_2_3() {
        let cell2_2 = editor.querySelector("#c2-2");
        let cell2_3 = editor.querySelector("#c2-3");
        selection.removeAllRanges();
        let range = document.createRange();
        range.selectNode(cell2_2);
        selection.addRange(range);
        range = document.createRange();
        range.selectNode(cell2_3);
        selection.addRange(range);
        getHTMLEditor().setBackgroundColor("#ff0000");
        is(cell2_2.getAttribute("bgcolor"), "#ff0000",
          '#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the second cell element in the second row');
        is(cell2_3.getAttribute("bgcolor"), "#ff0000",
          '#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the third cell element in the second row');
        ok(!cell2_2.parentNode.hasAttribute("bgcolor"),
          '#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the parent row');
        ok(!cell2_2.parentNode.parentNode.hasAttribute("bgcolor"),
          '#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the parent tbody');
        ok(!cell2_2.parentNode.parentNode.parentNode.hasAttribute("bgcolor"),
          '#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the parent table');
        ok(!editor.hasAttribute("bgcolor"),
          '#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the editing host');
        ok(!document.body.hasAttribute("bgcolor"),
          '#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the body');
        ok(!document.documentElement.hasAttribute("bgcolor"),
          '#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the html');

        getHTMLEditor().setBackgroundColor("");
        ok(!cell2_2.hasAttribute("bgcolor"),
          '#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the second cell element in the second row');
        ok(!cell2_3.hasAttribute("bgcolor"),
          '#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the third cell element in the second row');
      })();

      (function test_with_selecting_6_3_and_paragraph_in_6_4_and_6_5() {
        selection.removeAllRanges();
        let cell6_3 = editor.querySelector("#c6-3");
        let cell6_4 = editor.querySelector("#c6-4");
        let cell6_5 = editor.querySelector("#c6-5");
        let range = document.createRange();
        range.selectNode(cell6_3);
        selection.addRange(range);
        range = document.createRange();
        range.selectNode(cell6_4.firstChild);
        selection.addRange(range);
        range = document.createRange();
        range.selectNode(cell6_5);
        selection.addRange(range);
        getHTMLEditor().setBackgroundColor("#ff0000");
        is(cell6_3.getAttribute("bgcolor"), "#ff0000",
          '#6-3, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the second cell element in the sixth row');
        ok(!cell6_4.hasAttribute("bgcolor"),
          '#6-3, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the third cell element in the sixth row');
        ok(!cell6_4.firstChild.hasAttribute("bgcolor"),
          '#6-3, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the paragraph in the third cell element in the sixth row');
        is(cell6_5.getAttribute("bgcolor"), "#ff0000",
          '#6-3, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the forth cell element in the sixth row');

        getHTMLEditor().setBackgroundColor("");
        ok(!cell6_3.hasAttribute("bgcolor"),
          '#6-3, #6-5 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the second cell element in the sixth row');
        ok(!cell6_5.hasAttribute("bgcolor"),
          '#6-3, #6-5 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the third cell element in the sixth row');
      })();

      (function test_with_selecting_3_2_and_2_1_1_and_7_2() {
        let cell3_2 = editor.querySelector("#c3-2");
        let cell2_1_1 = editor.querySelector("#c2-1-1");
        let cell6_5 = editor.querySelector("#c6-5");
        selection.removeAllRanges();
        let range = document.createRange();
        range.selectNode(cell3_2);
        selection.addRange(range);
        range = document.createRange();
        range.selectNode(cell2_1_1);
        selection.addRange(range);
        range = document.createRange();
        range.selectNode(cell6_5);
        selection.addRange(range);

        getHTMLEditor().setBackgroundColor("#ff0000");
        is(cell3_2.getAttribute("bgcolor"), "#ff0000",
          '#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the first cell element in the third row');
        is(cell2_1_1.getAttribute("bgcolor"), "#ff0000",
          '#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the cell element in the child table');
        ok(!cell2_1_1.parentNode.hasAttribute("bgcolor"),
          '#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the row in the child table');
        ok(!cell2_1_1.parentNode.parentNode.hasAttribute("bgcolor"),
          '#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the tbody in the child table');
        ok(!cell2_1_1.parentNode.parentNode.parentNode.hasAttribute("bgcolor"),
          '#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the table in the child table');
        ok(!cell2_1_1.parentNode.parentNode.parentNode.parentNode.hasAttribute("bgcolor"),
          '#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the cell having the child table');
        is(cell6_5.getAttribute("bgcolor"), "#ff0000",
          '#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the forth cell element in the sixth row');

        getHTMLEditor().setBackgroundColor("");
        ok(!cell3_2.hasAttribute("bgcolor"),
          '#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the first cell element in the third row');
        ok(!cell2_1_1.hasAttribute("bgcolor"),
          '#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the cell element in the child table');
        ok(!cell6_5.hasAttribute("bgcolor"),
          '#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the third cell element in the sixth row');
      })();
    })();
  })();

  SimpleTest.finish();
});

function getHTMLEditor() {
  var Ci = SpecialPowers.Ci;
  var editingSession = SpecialPowers.wrap(window).docShell.editingSession;
  return editingSession.getEditorForWindow(window).QueryInterface(Ci.nsIHTMLEditor);
}

</script>
</body>

</html>