Bug 1772588 [wpt PR 34302] - [wpt] Add test for block-in-inline offsetParent., a...
[gecko.git] / editor / libeditor / tests / test_nsITableEditor_getFirstSelectedCellInTable.html
blobf85c338f9907eb5d984f94a1edd9764e08584366
1 <!DOCTYPE>
2 <html>
3 <head>
4 <title>Test for nsITableEditor.getFirstSelectedCellInTable()</title>
5 <script src="/tests/SimpleTest/SimpleTest.js"></script>
6 <link rel="stylesheet" href="/tests/SimpleTest/test.css">
7 </head>
8 <body>
9 <div id="display">
10 </div>
11 <div id="content" contenteditable></div>
12 <pre id="test">
13 </pre>
15 <script class="testbody" type="application/javascript">
17 SimpleTest.waitForExplicitFinish();
18 SimpleTest.waitForFocus(function() {
19 let editor = document.getElementById("content");
20 let selection = document.getSelection();
22 selection.collapse(editor, 0);
23 let rowWrapper = {};
24 let colWrapper = {};
25 let cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCellInTable(rowWrapper, colWrapper));
26 is(cell, null,
27 "nsITableEditor.getFirstSelectedCellInTable() should return null if Selection does not select cells");
28 is(rowWrapper.value, 0,
29 "nsITableEditor.getFirstSelectedCellInTable() should return 0 to row number if Selection does not select cells");
30 is(colWrapper.value, 0,
31 "nsITableEditor.getFirstSelectedCellInTable() should return 0 to row number if Selection does not select cells");
33 editor.innerHTML =
34 '<table id="table">' +
35 '<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>' +
36 '<tr id="r2"><th id="c2-1" rowspan="2">cell2-1</th><td id="c2-2">cell2-2<td id="c2-3">cell2-3</td></tr>' +
37 '<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>' +
38 '<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>' +
39 '<tr id="r5"><th id="c5-2">cell5-2</th><th id="c5-3" colspan="2">cell5-3</th><td id="c5-5">cell5-5</td></tr>' +
40 '<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>' +
41 '<tr id="r7"><td id="c7-2" colspan="4">cell7-2</td></tr>' +
42 "</table>";
44 let tr = document.getElementById("r1");
45 selection.setBaseAndExtent(tr, 0, tr, 1);
46 cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCellInTable(rowWrapper, colWrapper));
47 is(cell, document.getElementById("c1-1"),
48 "#1-1 nsITableEditor.getFirstSelectedCellInTable() should return the first cell element in the first row");
49 is(rowWrapper.value, 0,
50 "#1-1 nsITableEditor.getFirstSelectedCellInTable() should return 0 to row number for the first row");
51 is(colWrapper.value, 0,
52 "#1-1 nsITableEditor.getFirstSelectedCellInTable() should return 0 to column number for the first column");
54 tr = document.getElementById("r1");
55 selection.setBaseAndExtent(tr, 3, tr, 4);
56 cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCellInTable(rowWrapper, colWrapper));
57 is(cell, document.getElementById("c1-4"),
58 "#1-4 nsITableEditor.getFirstSelectedCellInTable() should return the last cell element whose colspan and rowspan are 2 in the first row");
59 is(rowWrapper.value, 0,
60 "#1-4 nsITableEditor.getFirstSelectedCellInTable() should return 0 to row number for the first row");
61 is(colWrapper.value, 3,
62 "#1-4 nsITableEditor.getFirstSelectedCellInTable() should return 3 to column number for the forth column");
64 tr = document.getElementById("r2");
65 selection.setBaseAndExtent(tr, 0, tr, 1);
66 cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCellInTable(rowWrapper, colWrapper));
67 is(cell, document.getElementById("c2-1"),
68 "#2-1 nsITableEditor.getFirstSelectedCellInTable() should return the first cell element in the second row");
69 is(rowWrapper.value, 1,
70 "#2-1 nsITableEditor.getFirstSelectedCellInTable() should return 1 to row number for the second row");
71 is(colWrapper.value, 0,
72 "#2-1 nsITableEditor.getFirstSelectedCellInTable() should return 0 to column number for the first column");
74 tr = document.getElementById("r7");
75 selection.setBaseAndExtent(tr, 0, tr, 1);
76 cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCellInTable(rowWrapper, colWrapper));
77 is(cell, document.getElementById("c7-2"),
78 "#7-2 nsITableEditor.getFirstSelectedCellInTable() should return the second cell element in the last row");
79 is(rowWrapper.value, 6,
80 "#7-2 nsITableEditor.getFirstSelectedCellInTable() should return 6 to row number for the seventh row");
81 is(colWrapper.value, 1,
82 "#7-2 nsITableEditor.getFirstSelectedCellInTable() should return 1 to column number for the second column");
84 selection.removeAllRanges();
85 let range = document.createRange();
86 range.selectNode(document.getElementById("c2-2"));
87 selection.addRange(range);
88 range = document.createRange();
89 range.selectNode(document.getElementById("c2-3"));
90 selection.addRange(range);
91 cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCellInTable(rowWrapper, colWrapper));
92 is(cell, document.getElementById("c2-2"),
93 "#2-2 nsITableEditor.getFirstSelectedCellInTable() should return the second cell element in the second row");
94 is(rowWrapper.value, 1,
95 "#2-2 nsITableEditor.getFirstSelectedCellInTable() should return 1 to row number for the second row");
96 is(colWrapper.value, 1,
97 "#2-2 nsITableEditor.getFirstSelectedCellInTable() should return 1 to column number for the second column");
99 selection.removeAllRanges();
100 range = document.createRange();
101 range.selectNode(document.getElementById("c3-4"));
102 selection.addRange(range);
103 range = document.createRange();
104 range.selectNode(document.getElementById("c5-2"));
105 selection.addRange(range);
106 cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCellInTable(rowWrapper, colWrapper));
107 is(cell, document.getElementById("c3-4"),
108 "#3-4 nsITableEditor.getFirstSelectedCellInTable() should return the last cell element in the third row");
109 is(rowWrapper.value, 2,
110 "#3-4 nsITableEditor.getFirstSelectedCellInTable() should return 2 to row number for the third row");
111 is(colWrapper.value, 3,
112 "#3-4 nsITableEditor.getFirstSelectedCellInTable() should return 3 to column number for the forth column");
114 cell = document.getElementById("c6-4");
115 selection.selectAllChildren(cell);
116 cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCellInTable(rowWrapper, colWrapper));
117 is(cell, null,
118 "nsITableEditor.getFirstSelectedCellInTable() should return null if neither <td> nor <th> element node is selected");
119 is(rowWrapper.value, 0,
120 "#3-4 nsITableEditor.getFirstSelectedCellInTable() should return 0 to row number if neither <td> nor <th> element node is selected");
121 is(colWrapper.value, 0,
122 "#3-4 nsITableEditor.getFirstSelectedCellInTable() should return 0 to column number if neither <td> nor <th> element node is selected");
124 cell = document.getElementById("c6-5");
125 selection.setBaseAndExtent(cell.firstChild, 0, cell.firstChild, 0);
126 cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCellInTable(rowWrapper, colWrapper));
127 is(cell, null,
128 "nsITableEditor.getFirstSelectedCellInTable() should return null if a text node is selected");
129 is(rowWrapper.value, 0,
130 "#3-4 nsITableEditor.getFirstSelectedCellInTable() should return 0 to row number if a text node is selected");
131 is(colWrapper.value, 0,
132 "#3-4 nsITableEditor.getFirstSelectedCellInTable() should return 0 to column number if a text node is selected");
134 // XXX If cell is not selected, nsITableEditor.getFirstSelectedCellInTable()
135 // returns null without throwing exception, however, if there is no
136 // selection ranges, throwing an exception. This inconsistency is odd.
137 selection.removeAllRanges();
138 try {
139 cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCellInTable(rowWrapper, colWrapper));
140 ok(false, "nsITableEditor.getFirstSelectedCellInTable() should throw an exception if there is no selection ranges");
141 } catch (e) {
142 ok(true, "nsITableEditor.getFirstSelectedCellInTable() should throw an exception if there is no selection ranges");
145 tr = document.getElementById("r6");
146 selection.setBaseAndExtent(tr, 0, tr, 1);
147 try {
148 cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCellInTable());
149 ok(false, "nsITableEditor.getFirstSelectedCellInTable() should throw an exception if it does not have argument");
150 } catch (e) {
151 ok(true, "nsITableEditor.getFirstSelectedCellInTable() should throw an exception if it does not have argument");
154 tr = document.getElementById("r6");
155 selection.setBaseAndExtent(tr, 0, tr, 1);
156 try {
157 cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCellInTable(null));
158 ok(false, "nsITableEditor.getFirstSelectedCellInTable() should throw an exception if its argument is only one null");
159 } catch (e) {
160 ok(true, "nsITableEditor.getFirstSelectedCellInTable() should throw an exception if its argument is only one null");
163 tr = document.getElementById("r6");
164 selection.setBaseAndExtent(tr, 0, tr, 1);
165 try {
166 cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCellInTable(null, null));
167 ok(false, "nsITableEditor.getFirstSelectedCellInTable() should throw an exception if its arguments are all null");
168 } catch (e) {
169 ok(true, "nsITableEditor.getFirstSelectedCellInTable() should throw an exception if its arguments are all null");
172 tr = document.getElementById("r6");
173 selection.setBaseAndExtent(tr, 0, tr, 1);
174 try {
175 cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCellInTable(rowWrapper, null));
176 ok(false, "nsITableEditor.getFirstSelectedCellInTable() should throw an exception if its column argument is null");
177 } catch (e) {
178 ok(true, "nsITableEditor.getFirstSelectedCellInTable() should throw an exception if its column argument is null");
181 tr = document.getElementById("r6");
182 selection.setBaseAndExtent(tr, 0, tr, 1);
183 try {
184 cell = SpecialPowers.unwrap(getTableEditor().getFirstSelectedCellInTable(null, colWrapper));
185 ok(false, "nsITableEditor.getFirstSelectedCellInTable() should throw an exception if its row argument is null");
186 } catch (e) {
187 ok(true, "nsITableEditor.getFirstSelectedCellInTable() should throw an exception if its row argument is null");
190 SimpleTest.finish();
193 function getTableEditor() {
194 let editingSession = SpecialPowers.wrap(window).docShell.editingSession;
195 return editingSession.getEditorForWindow(window).QueryInterface(SpecialPowers.Ci.nsITableEditor);
198 </script>
199 </body>
201 </html>