4 <title>Test for nsITableEditor.getSelectedCells()
</title>
5 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
6 <link rel=
"stylesheet" href=
"/tests/SimpleTest/test.css">
11 <div id=
"content" contenteditable
></div>
15 <script class=
"testbody" type=
"application/javascript">
18 SimpleTest.waitForExplicitFinish();
19 SimpleTest.waitForFocus(() =
> {
20 let editor = document.getElementById(
"content");
21 let selection = document.getSelection();
23 (function test_with_collapsed_selection() {
24 selection.collapse(editor,
0);
25 let cells = getTableEditor().getSelectedCells();
27 "nsITableEditor.getSelectedCells() should return empty array if Selection does not select cells");
31 '
<table id=
"table">' +
32 '
<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>' +
33 '
<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>' +
34 '
<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>' +
35 '
<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>' +
36 '
<tr id=
"r5"><th id=
"c5-2">cell5-
2</th><th id=
"c5-3" colspan=
"2">' +
37 '
<table><tr id=
"r2-1"><td id=
"c2-1-1">cell2-
1-
1</td></tr></table>' +
38 '
</th><td id=
"c5-5">cell5-
5</td></tr>' +
39 '
<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>' +
40 '
<tr id=
"r7"><td id=
"c7-2" colspan=
"4">cell7-
2</td></tr>' +
43 (function test_with_selecting_1_1() {
44 let tr = document.getElementById(
"r1");
45 selection.setBaseAndExtent(tr,
0, tr,
1);
46 let cells = getTableEditor().getSelectedCells();
48 "#1-1 nsITableEditor.getSelectedCells() should return an array whose length is 1");
49 is(SpecialPowers.unwrap(cells[
0]), document.getElementById(
"c1-1"),
50 "#1-1 nsITableEditor.getSelectedCells() should set the first item of the result to the first cell element in the first row");
53 (function test_with_selecting_1_4() {
54 let tr = document.getElementById(
"r1");
55 selection.setBaseAndExtent(tr,
3, tr,
4);
56 let cells = getTableEditor().getSelectedCells();
58 "#1-4 nsITableEditor.getSelectedCells() should return an array whose length is 1");
59 is(SpecialPowers.unwrap(cells[
0]), document.getElementById(
"c1-4"),
60 "#1-4 nsITableEditor.getSelectedCells() should set the first item of the result to the last cell element whose colspan and rowspan are 2 in the first row");
63 (function test_with_selecting_2_1() {
64 let tr = document.getElementById(
"r2");
65 selection.setBaseAndExtent(tr,
0, tr,
1);
66 let cells = getTableEditor().getSelectedCells();
68 "#2-1 nsITableEditor.getSelectedCells() should return an array whose length is 1");
69 is(SpecialPowers.unwrap(cells[
0]), document.getElementById(
"c2-1"),
70 "#2-1 nsITableEditor.getSelectedCells() should set the first item of the result to the first cell element in the second row");
73 (function test_with_selecting_7_2() {
74 let tr = document.getElementById(
"r7");
75 selection.setBaseAndExtent(tr,
0, tr,
1);
76 let cells = getTableEditor().getSelectedCells();
78 "#7-2 nsITableEditor.getSelectedCells() should return an array whose length is 1");
79 is(SpecialPowers.unwrap(cells[
0]), document.getElementById(
"c7-2"),
80 "#7-2 nsITableEditor.getSelectedCells() should set the first item of the result to the second cell element in the last row");
83 (function test_with_selecting_2_2_and_2_3() {
84 let tr = document.getElementById(
"r2");
85 selection.removeAllRanges();
86 let range = document.createRange();
87 range.setStart(tr,
1);
89 selection.addRange(range);
90 range = document.createRange();
91 range.setStart(tr,
2);
93 selection.addRange(range);
94 let cells = getTableEditor().getSelectedCells();
96 "#2-2 nsITableEditor.getSelectedCells() should return an array whose length is 2");
97 is(SpecialPowers.unwrap(cells[
0]), document.getElementById(
"c2-2"),
98 "#2-2 nsITableEditor.getSelectedCells() should set the first item of the result to the second cell element in the second row");
101 (function test_with_selecting_3_4_and_5_2() {
102 let tr = document.getElementById(
"r3");
103 selection.removeAllRanges();
104 let range = document.createRange();
105 range.setStart(tr,
2);
107 selection.addRange(range);
108 range = document.createRange();
109 range.setStart(document.getElementById(
"r5"),
0);
110 range.setEnd(document.getElementById(
"r5"),
1);
111 selection.addRange(range);
112 let cells = getTableEditor().getSelectedCells();
114 "#3-4, #5-2 nsITableEditor.getSelectedCells() should return an array whose length is 2");
115 is(SpecialPowers.unwrap(cells[
0]), document.getElementById(
"c3-4"),
116 "#3-4, #5-2 nsITableEditor.getSelectedCells() should set the first item of the result to the last cell element in the third row");
117 is(SpecialPowers.unwrap(cells[
1]), document.getElementById(
"c5-2"),
118 "#3-4, #5-2 nsITableEditor.getSelectedCells() should set the second item of the result to the first cell element in the fifth row");
121 (function test_with_selecting_1_2_and_1_3_and_1_4_and_2_1_and_2_2() {
122 selection.removeAllRanges();
123 let tr = document.getElementById(
"r1");
124 let range = document.createRange();
125 range.setStart(tr,
1);
127 selection.addRange(range);
128 range = document.createRange();
129 range.setStart(tr,
2);
131 selection.addRange(range);
132 range = document.createRange();
133 range.setStart(tr,
3);
135 selection.addRange(range);
136 tr = document.getElementById(
"r2");
137 range = document.createRange();
138 range.setStart(tr,
0);
140 selection.addRange(range);
141 range = document.createRange();
142 range.setStart(tr,
1);
144 selection.addRange(range);
146 let cells = getTableEditor().getSelectedCells();
148 "#1-2, #1-3, #1-4, #2-1, #2-2 nsITableEditor.getSelectedCells() should return an array whose length is 5");
149 is(SpecialPowers.unwrap(cells[
0]), document.getElementById(
"c1-2"),
150 "#1-2, #1-3, #1-4, #2-1, #2-2 nsITableEditor.getSelectedCells() should set the first item of the result to the second cell element in the first row");
151 is(SpecialPowers.unwrap(cells[
1]), document.getElementById(
"c1-3"),
152 "#1-2, #1-3, #1-4, #2-1, #2-2 nsITableEditor.getSelectedCells() should set the second item of the result to the third cell element in the first row");
153 is(SpecialPowers.unwrap(cells[
2]), document.getElementById(
"c1-4"),
154 "#1-2, #1-3, #1-4, #2-1, #2-2 nsITableEditor.getSelectedCells() should set the third item of the result to the forth cell element in the first row");
155 is(SpecialPowers.unwrap(cells[
3]), document.getElementById(
"c2-1"),
156 "#1-2, #1-3, #1-4, #2-1, #2-2 nsITableEditor.getSelectedCells() should set the forth item of the result to the first cell element in the second row");
157 is(SpecialPowers.unwrap(cells[
4]), document.getElementById(
"c2-2"),
158 "#1-2, #1-3, #1-4, #2-1, #2-2 nsITableEditor.getSelectedCells() should set the forth item of the result to the second cell element in the second row");
161 (function test_with_selecting_6_3_and_paragraph_in_6_4_and_6_5() {
162 selection.removeAllRanges();
163 let tr = document.getElementById(
"r6");
164 let range = document.createRange();
165 range.setStart(tr,
1);
167 selection.addRange(range);
168 range = document.createRange();
169 range.setStart(document.getElementById(
"c6-4").firstChild,
0);
170 range.setEnd(document.getElementById(
"c6-4").firstChild,
1);
171 selection.addRange(range);
172 range = document.createRange();
173 range.setStart(tr,
3);
175 selection.addRange(range);
177 let cells = getTableEditor().getSelectedCells();
179 "#6-3, #6-5 nsITableEditor.getSelectedCells() should return an array whose length is 2");
180 is(SpecialPowers.unwrap(cells[
0]), document.getElementById(
"c6-3"),
181 "#6-3, #6-5 nsITableEditor.getSelectedCells() should set the first item of the result to the second cell element in the sixth row");
182 // The
<p> element in c6-
4 is selected, this does not select the cell
183 // element so that it should be ignored.
184 is(SpecialPowers.unwrap(cells[
1]), document.getElementById(
"c6-5"),
185 "#6-3, #6-5 nsITableEditor.getSelectedCells() should set the first item of the result to the forth cell element in the sixth row");
188 (function test_with_selecting_2_3_and_text_in_4_1_and_7_2() {
189 selection.removeAllRanges();
190 let tr = document.getElementById(
"r2");
191 let range = document.createRange();
192 range.setStart(tr,
2);
194 selection.addRange(range);
195 range = document.createRange();
196 range.setStart(document.getElementById(
"c4-1").firstChild,
0);
197 range.setEnd(document.getElementById(
"c4-1").firstChild,
7);
198 selection.addRange(range);
199 tr = document.getElementById(
"r7");
200 range = document.createRange();
201 range.setStart(tr,
0);
203 selection.addRange(range);
205 let cells = getTableEditor().getSelectedCells();
207 "#2-3, #7-2 nsITableEditor.getSelectedCells() should return an array whose length is 2");
208 is(SpecialPowers.unwrap(cells[
0]), document.getElementById(
"c2-3"),
209 "#2-3, #7-2 nsITableEditor.getSelectedCells() should set the first item of the result to the third cell element in the second row");
210 // Text in c4-
1 is selected, this does not select the cell element so that
211 // it should be ignored. Note that we've ignored the following selected
212 // cell elements in old API, but it causes inconsistent behavior with the
213 // previous test case. Therefore, we take this behavior.
214 is(SpecialPowers.unwrap(cells[
1]), document.getElementById(
"c7-2"),
215 "#2-3, #7-2 nsITableEditor.getSelectedCells() should set the second item of the result to the cell element in the seventh row");
218 (function test_with_selecting_3_2_and_2_1_1_and_7_2() {
219 selection.removeAllRanges();
220 let tr = document.getElementById(
"r3");
221 let range = document.createRange();
222 range.setStart(tr,
0);
224 selection.addRange(range);
225 tr = document.getElementById(
"r2-1");
226 range = document.createRange();
227 range.setStart(tr,
0);
229 selection.addRange(range);
230 tr = document.getElementById(
"r7");
231 range = document.createRange();
232 range.setStart(tr,
0);
234 selection.addRange(range);
236 let cells = getTableEditor().getSelectedCells();
238 "#3-2, #2-1-1, #7-2 nsITableEditor.getSelectedCells() should return an array whose length is 3");
239 is(SpecialPowers.unwrap(cells[
0]), document.getElementById(
"c3-2"),
240 "#3-2, #2-1-1, #7-2 nsITableEditor.getSelectedCells() should set the first item of the result to the first cell element in the third row");
241 // c2-
1-
1 is in another
<table>, however, getSelectedCells() returns it
242 // since it works only with ranges of Selection.
243 is(SpecialPowers.unwrap(cells[
1]), document.getElementById(
"c2-1-1"),
244 "#3-2, #2-1-1, #7-2 nsITableEditor.getSelectedCells() should set the second item of the result to the cell element in the child <table> element");
245 is(SpecialPowers.unwrap(cells[
2]), document.getElementById(
"c7-2"),
246 "#3-2, #2-1-1, #7-2 nsITableEditor.getSelectedCells() should set the third item of the result to the cell element in the seventh row");
249 (function test_with_selecting_all_children_of_cell() {
250 selection.selectAllChildren(document.getElementById(
"c6-4"));
251 let cells = getTableEditor().getSelectedCells();
253 "nsITableEditor.getSelectedCells() should return an empty array when no cell element is selected");
256 (function test_with_selecting_text_in_cell() {
257 let cell = document.getElementById(
"c6-5");
258 selection.collapse(cell.firstChild,
0);
259 let cells = getTableEditor().getSelectedCells();
261 "nsITableEditor.getSelectedCells() should return an empty array when selecting text in a cell element");
264 (function test_with_selecting_text_in_1_1_and_1_2() {
265 let cell = document.getElementById(
"c1-1");
266 selection.setBaseAndExtent(cell.firstChild,
0, cell.firstChild,
3);
267 let range = document.createRange();
268 range.setStart(cell.parentNode,
1);
269 range.setEnd(cell.parentNode,
2);
270 selection.addRange(range);
271 let cells = getTableEditor().getSelectedCells();
273 "nsITableEditor.getSelectedCells() should return an empty array when the first range does not select a cell element");
276 (function test_without_selection_ranges() {
277 selection.removeAllRanges();
278 let cells = getTableEditor().getSelectedCells();
280 "nsITableEditor.getSelectedCells() should return an empty array even when there is no selection range");
286 function getTableEditor() {
287 var Ci = SpecialPowers.Ci;
288 var editingSession = SpecialPowers.wrap(window).docShell.editingSession;
289 return editingSession.getEditorForWindow(window).QueryInterface(Ci.nsITableEditor);