test_nsITableEditor_getSelectedOrParentTableElement.html (15927B)
1 <!DOCTYPE> 2 <html> 3 <head> 4 <title>Test for nsITableEditor.getSelectedOrParentTableElement()</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> 14 15 <script class="testbody" type="application/javascript"> 16 17 SimpleTest.waitForExplicitFinish(); 18 SimpleTest.waitForFocus(function() { 19 let editor = document.getElementById("content"); 20 let selection = document.getSelection(); 21 22 selection.collapse(editor, 0); 23 let tagNameWrapper = {}; 24 let countWrapper = {}; 25 let cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, countWrapper)); 26 is(cell, null, 27 "nsITableEditor.getSelectedOrParentTableElement() should return null if Selection does not select cells nor in <table>"); 28 is(tagNameWrapper.value, "", 29 "nsITableEditor.getSelectedOrParentTableElement() should return empty string to tag name if Selection does not select cells nor in <table>"); 30 is(countWrapper.value, 0, 31 "nsITableEditor.getSelectedOrParentTableElement() should return 0 to count if Selection does not select cells nor in <table>"); 32 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><th id="c2-2">cell2-2</th><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">' + 39 '<table id="table2">' + 40 '<tr id="r2-1"><th id="c2-1-1">cell2-1-1-</td><td id="c2-1-2">cell2-1-2</td></tr>' + 41 '<tr id="r2-2"><td id="c2-2-1">cell2-2-1-</td><th id="c2-2-2">cell2-2-2</th></tr>' + 42 "</table>" + 43 '</td><th id="c4-4">cell4-4</th><td id="c4-5">cell4-5</td></tr>' + 44 '<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>' + 45 '<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>' + 46 '<tr id="r7"><td id="c7-2" colspan="4">cell7-2</td></tr>' + 47 "</table>"; 48 49 let tr = document.getElementById("r1"); 50 selection.setBaseAndExtent(tr, 0, tr, 1); 51 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, countWrapper)); 52 is(cell, document.getElementById("c1-1"), 53 "#1-1 nsITableEditor.getSelectedOrParentTableElement() should return the first cell element in the first row"); 54 is(tagNameWrapper.value, "td", 55 "#1-1 nsITableEditor.getSelectedOrParentTableElement() should return 'td' to tag name when a cell is selected"); 56 is(countWrapper.value, 1, 57 "#1-1 nsITableEditor.getSelectedOrParentTableElement() should return 1 to count when a cell is selected"); 58 59 tr = document.getElementById("r1"); 60 selection.setBaseAndExtent(tr, 3, tr, 4); 61 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, countWrapper)); 62 is(cell, document.getElementById("c1-4"), 63 "#1-4 nsITableEditor.getSelectedOrParentTableElement() should return the last cell element whose colspan and rowspan are 2 in the first row"); 64 is(tagNameWrapper.value, "td", 65 "#1-4 nsITableEditor.getSelectedOrParentTableElement() should return 'td' to tag name when a cell is selected"); 66 is(countWrapper.value, 1, 67 "#1-4 nsITableEditor.getSelectedOrParentTableElement() should return 1 to count when a cell is selected"); 68 69 tr = document.getElementById("r2"); 70 selection.setBaseAndExtent(tr, 0, tr, 1); 71 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, countWrapper)); 72 is(cell, document.getElementById("c2-1"), 73 "#2-1 nsITableEditor.getSelectedOrParentTableElement() should return the first cell element in the second row"); 74 is(tagNameWrapper.value, "td", 75 "#2-1 nsITableEditor.getSelectedOrParentTableElement() should return 'td' to tag name when a cell is selected but even if the cell is <th>"); 76 is(countWrapper.value, 1, 77 "#2-1 nsITableEditor.getSelectedOrParentTableElement() should return 1 to count when a cell is selected"); 78 79 tr = document.getElementById("r7"); 80 selection.setBaseAndExtent(tr, 0, tr, 1); 81 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, countWrapper)); 82 is(cell, document.getElementById("c7-2"), 83 "#7-2 nsITableEditor.getSelectedOrParentTableElement() should return the second cell element in the last row"); 84 is(tagNameWrapper.value, "td", 85 "#7-2 nsITableEditor.getSelectedOrParentTableElement() should return 'td' to tag name when a cell is selected"); 86 is(countWrapper.value, 1, 87 "#7-2 nsITableEditor.getSelectedOrParentTableElement() should return 1 to count when a cell is selected"); 88 89 selection.removeAllRanges(); 90 let range = document.createRange(); 91 range.selectNode(document.getElementById("c2-2")); 92 selection.addRange(range); 93 range = document.createRange(); 94 range.selectNode(document.getElementById("c2-3")); 95 selection.addRange(range); 96 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, countWrapper)); 97 is(cell, document.getElementById("c2-2"), 98 "#2-2 nsITableEditor.getSelectedOrParentTableElement() should return the second cell element in the second row"); 99 is(tagNameWrapper.value, "td", 100 "#2-2 nsITableEditor.getSelectedOrParentTableElement() should return 'td' to tag name when first range selects a cell"); 101 is(countWrapper.value, 2, 102 "#2-2 nsITableEditor.getSelectedOrParentTableElement() should return 2 to count when there are 2 selection ranges"); 103 104 selection.removeAllRanges(); 105 range = document.createRange(); 106 range.selectNode(document.getElementById("c3-4")); 107 selection.addRange(range); 108 range = document.createRange(); 109 range.selectNode(document.getElementById("c5-2")); 110 selection.addRange(range); 111 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, countWrapper)); 112 is(cell, document.getElementById("c3-4"), 113 "#3-4 nsITableEditor.getSelectedOrParentTableElement() should return the last cell element in the third row"); 114 is(tagNameWrapper.value, "td", 115 "#3-4 nsITableEditor.getSelectedOrParentTableElement() should return 'td' to tag name when first range selects a cell"); 116 is(countWrapper.value, 2, 117 "#3-4 nsITableEditor.getSelectedOrParentTableElement() should return 2 to count when there are 2 selection ranges"); 118 119 cell = document.getElementById("c2-2"); 120 selection.removeAllRanges(); 121 range = document.createRange(); 122 range.setStart(cell.firstChild, 0); 123 selection.addRange(range); 124 cell = document.getElementById("c2-1-1"); 125 range = document.createRange(); 126 range.setStart(cell.firstChild, 1); 127 range.setEnd(cell.firstChild, 2); 128 selection.addRange(range); 129 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, countWrapper)); 130 is(cell, document.getElementById("c2-1-1"), 131 "#2-1-1 nsITableEditor.getSelectedOrParentTableElement() should return the cell which contains the last selection range if first selection range does not select a cell"); 132 is(tagNameWrapper.value, "td", 133 "#2-1-1 nsITableEditor.getSelectedOrParentTableElement() should return 'td' to tag name when the first range does not select a cell and the last range is in a cell"); 134 is(countWrapper.value, 0, 135 "#2-1-1 nsITableEditor.getSelectedOrParentTableElement() should return 0 to count when the first range does not select a cell"); 136 137 tr = document.getElementById("r2-2"); 138 selection.setBaseAndExtent(tr, 0, tr, 1); 139 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, countWrapper)); 140 is(cell, document.getElementById("c2-2-1"), 141 "#2-2-1 nsITableEditor.getSelectedOrParentTableElement() should return the first cell element in the first row of nested <table>"); 142 is(tagNameWrapper.value, "td", 143 "#2-2-1 nsITableEditor.getSelectedOrParentTableElement() should return 'td' to tag name when a cell in nested <table> is selected"); 144 is(countWrapper.value, 1, 145 "#2-2-1 nsITableEditor.getSelectedOrParentTableElement() should return 1 to count when a cell in nested <table> is selected"); 146 147 cell = document.getElementById("c2-1-2"); 148 selection.setBaseAndExtent(cell.firstChild, 0, cell.firstChild, 0); 149 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, countWrapper)); 150 is(cell, document.getElementById("c2-1-2"), 151 "#2-1-2 nsITableEditor.getSelectedOrParentTableElement() should return the first cell element in the first row of nested <table>"); 152 is(tagNameWrapper.value, "td", 153 "#2-1-2 nsITableEditor.getSelectedOrParentTableElement() should return 'td' to tag name when a cell in nested <table> contains the first selection range"); 154 is(countWrapper.value, 0, 155 "#2-1-2 nsITableEditor.getSelectedOrParentTableElement() should return 0 to count when a cell in nested <table> contains the first selection range"); 156 157 let table = document.getElementById("table2"); 158 selection.removeAllRanges(); 159 range = document.createRange(); 160 range.selectNode(table); 161 selection.addRange(range); 162 table = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, countWrapper)); 163 is(table, document.getElementById("table2"), 164 "nsITableEditor.getSelectedOrParentTableElement() should return a <table> element which is selected"); 165 is(tagNameWrapper.value, "table", 166 "nsITableEditor.getSelectedOrParentTableElement() should return 'table' to tag name when a <table> is selected"); 167 is(countWrapper.value, 1, 168 "nsITableEditor.getSelectedOrParentTableElement() should return 1 to count when a <table> is selected"); 169 170 selection.removeAllRanges(); 171 range = document.createRange(); 172 range.selectNode(document.getElementById("r2-1")); 173 selection.addRange(range); 174 range = document.createRange(); 175 range.selectNode(document.getElementById("r2-2")); 176 selection.addRange(range); 177 table = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, countWrapper)); 178 is(table, document.getElementById("r2-2"), 179 "nsITableEditor.getSelectedOrParentTableElement() should return a <tr> element which is selected by the last selection range"); 180 is(tagNameWrapper.value, "tr", 181 "nsITableEditor.getSelectedOrParentTableElement() should return 'tr' to tag name when a <tr> is selected"); 182 is(countWrapper.value, 1, 183 "nsITableEditor.getSelectedOrParentTableElement() should return 1 to count when a <tr> is selected"); 184 185 selection.removeAllRanges(); 186 range = document.createRange(); 187 range.selectNode(document.getElementById("r1")); 188 selection.addRange(range); 189 range = document.createRange(); 190 range.selectNode(document.getElementById("c5-5")); 191 selection.addRange(range); 192 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, countWrapper)); 193 is(cell, document.getElementById("c5-5"), 194 "#5-5 nsITableEditor.getSelectedOrParentTableElement() should return the cell selected by the last range when first range selects <tr>"); 195 is(tagNameWrapper.value, "td", 196 "#5-5 nsITableEditor.getSelectedOrParentTableElement() should return 'td' to tag name if the last range selects the cell when first range selects <tr>"); 197 is(countWrapper.value, 2, 198 "#5-5 nsITableEditor.getSelectedOrParentTableElement() should return 2 to count if the last range selects <td> when first range selects <tr>"); 199 200 selection.removeAllRanges(); 201 range = document.createRange(); 202 range.selectNode(document.getElementById("r1")); 203 selection.addRange(range); 204 range = document.createRange(); 205 range.selectNode(document.getElementById("c5-2")); 206 selection.addRange(range); 207 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, countWrapper)); 208 is(cell, null, 209 "#5-5 nsITableEditor.getSelectedOrParentTableElement() should return null if the last range selects <th> when first range selects <tr>"); 210 is(tagNameWrapper.value, "", 211 "#5-5 nsITableEditor.getSelectedOrParentTableElement() should return empty string to tag name if the last range selects <th> when first range selects <tr>"); 212 is(countWrapper.value, 0, 213 "#5-5 nsITableEditor.getSelectedOrParentTableElement() should return 0 to count if the last range selects <th> when first range selects <tr>"); 214 215 // XXX If cell is not selected, nsITableEditor.getSelectedOrParentTableElement() 216 // returns null without throwing exception, however, if there is no 217 // selection ranges, throwing an exception. This inconsistency is odd. 218 selection.removeAllRanges(); 219 try { 220 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, countWrapper)); 221 ok(false, "nsITableEditor.getSelectedOrParentTableElement() should throw an exception if there is no selection ranges"); 222 } catch (e) { 223 ok(true, "nsITableEditor.getSelectedOrParentTableElement() should throw an exception if there is no selection ranges"); 224 } 225 226 tr = document.getElementById("r6"); 227 selection.setBaseAndExtent(tr, 0, tr, 1); 228 try { 229 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement()); 230 ok(false, "nsITableEditor.getSelectedOrParentTableElement() should throw an exception if it does not have argument"); 231 } catch (e) { 232 ok(true, "nsITableEditor.getSelectedOrParentTableElement() should throw an exception if it does not have argument"); 233 } 234 235 tr = document.getElementById("r6"); 236 selection.setBaseAndExtent(tr, 0, tr, 1); 237 try { 238 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(null)); 239 ok(false, "nsITableEditor.getSelectedOrParentTableElement() should throw an exception if its argument is only one null"); 240 } catch (e) { 241 ok(true, "nsITableEditor.getSelectedOrParentTableElement() should throw an exception if its argument is only one null"); 242 } 243 244 tr = document.getElementById("r6"); 245 selection.setBaseAndExtent(tr, 0, tr, 1); 246 try { 247 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(null, null)); 248 ok(false, "nsITableEditor.getSelectedOrParentTableElement() should throw an exception if its arguments are all null"); 249 } catch (e) { 250 ok(true, "nsITableEditor.getSelectedOrParentTableElement() should throw an exception if its arguments are all null"); 251 } 252 253 tr = document.getElementById("r6"); 254 selection.setBaseAndExtent(tr, 0, tr, 1); 255 try { 256 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(tagNameWrapper, null)); 257 ok(false, "nsITableEditor.getSelectedOrParentTableElement() should throw an exception if its count argument is null"); 258 } catch (e) { 259 ok(true, "nsITableEditor.getSelectedOrParentTableElement() should throw an exception if its count argument is null"); 260 } 261 262 tr = document.getElementById("r6"); 263 selection.setBaseAndExtent(tr, 0, tr, 1); 264 try { 265 cell = SpecialPowers.unwrap(getTableEditor().getSelectedOrParentTableElement(null, countWrapper)); 266 ok(false, "nsITableEditor.getSelectedOrParentTableElement() should throw an exception if its tag name argument is null"); 267 } catch (e) { 268 ok(true, "nsITableEditor.getSelectedOrParentTableElement() should throw an exception if its tag name argument is null"); 269 } 270 271 SimpleTest.finish(); 272 }); 273 274 function getTableEditor() { 275 var Ci = SpecialPowers.Ci; 276 var editingSession = SpecialPowers.wrap(window).docShell.editingSession; 277 return editingSession.getEditorForWindow(window).QueryInterface(Ci.nsITableEditor); 278 } 279 280 </script> 281 </body> 282 283 </html>