test_nsITableEditor_getFirstRow.html (6571B)
1 <!DOCTYPE> 2 <html> 3 <head> 4 <title>Test for nsITableEditor.getFirstRow()</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 21 try { 22 SpecialPowers.unwrap(getTableEditor().getFirstRow(undefined)); 23 ok(false, "nsITableEditor.getFirstRow(undefined) should cause throwing an exception"); 24 } catch (e) { 25 ok(true, "nsITableEditor.getFirstRow(undefined) should cause throwing an exception"); 26 } 27 28 try { 29 SpecialPowers.unwrap(getTableEditor().getFirstRow(null)); 30 ok(false, "nsITableEditor.getFirstRow(null) should cause throwing an exception"); 31 } catch (e) { 32 ok(true, "nsITableEditor.getFirstRow(null) should cause throwing an exception"); 33 } 34 35 try { 36 SpecialPowers.unwrap(getTableEditor().getFirstRow(editor)); 37 ok(false, "nsITableEditor.getFirstRow() should cause throwing an exception if given node is not in <table>"); 38 } catch (e) { 39 ok(true, "nsITableEditor.getFirstRow() should cause throwing an exception if given node is not in <table>"); 40 } 41 42 // Set id to "test" for the argument for getFirstRow(). 43 // Set id to "expected" for the expected <tr> element result (if there is). 44 // Set class of <table> to "hasAnonymousRow" if it does not has <tr> but will be anonymous <tr> element is created. 45 const kTests = [ 46 '<table id="test"><tr id="expected"><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr></table>', 47 '<table><tr id="expected"><td id="test">cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr></table>', 48 '<table><tr id="expected"><td id="test">cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr></table>', 49 '<table><tr id="expected"><td>cell1-1</td><td id="test">cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr></table>', 50 '<table><tr id="expected"><td>cell1-1</td><td>cell1-2</td></tr><tr id="test"><td>cell2-1</td><td>cell2-2</td></tr></table>', 51 '<table><tr id="expected"><td>cell1-1</td><td>cell1-2</td></tr><tr><td id="test">cell2-1</td><td>cell2-2</td></tr></table>', 52 '<table><tr id="expected"><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td id="test">cell2-2</td></tr></table>', 53 '<table><tbody id="test"><tr id="expected"><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr></tbody></table>', 54 '<table><tbody><tr id="expected"><td id="test">cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr></tbody></table>', 55 '<table><thead id="test"><tr id="expected"><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr></thead></table>', 56 '<table><thead><tr id="expected"><td id="test">cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr></thead></table>', 57 '<table><tfoot id="test"><tr id="expected"><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr></tfoot></table>', 58 '<table><tfoot><tr id="expected"><td id="test">cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr></tfoot></table>', 59 '<table><thead id="test"><tr id="expected"><td>cell1-1</td><td>cell1-2</td></tr></thead><tbody><tr><td>cell2-1</td><td>cell2-2</td></tr></tbody></table>', 60 '<table><thead><tr id="expected"><td id="test">cell1-1</td><td>cell1-2</td></tr></thead><tbody><tr><td>cell2-1</td><td>cell2-2</td></tr></tbody></table>', 61 '<table><thead><tr id="expected"><td>cell1-1</td><td>cell1-2</td></tr></thead><tbody><tr><td id="test">cell2-1</td><td>cell2-2</td></tr></tbody></table>', 62 '<table><tfoot id="test"><tr id="expected"><td>cell1-1</td><td>cell1-2</td></tr></tfoot><tbody><tr><td>cell2-1</td><td>cell2-2</td></tr></tbody></table>', 63 '<table><tfoot><tr id="expected"><td id="test">cell1-1</td><td>cell1-2</td></tr></tfoot><tbody><tr><td>cell2-1</td><td>cell2-2</td></tr></tbody></table>', 64 '<table><tfoot><tr id="expected"><td>cell1-1</td><td>cell1-2</td></tr></tfoot><tbody><tr><td id="test">cell2-1</td><td>cell2-2</td></tr></tbody></table>', 65 '<table><tr><td><table id="test"><tr id="expected"><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr></table></td></tr></table>', 66 '<table><tr><td><table><tr id="expected"><td>cell1-1</td><td>cell1-2</td></tr><tr><td id="test">cell2-1</td><td>cell2-2</td></tr></table></td></tr></table>', 67 '<table id="test"></table>', 68 '<table><caption id="test">table-caption</caption></table>', 69 '<table><caption>table-caption</caption><tr id="expected"><td id="test">cell</td></tr></table>', 70 '<table class="hasAnonymousRow"><td id="test">cell</td></table>', 71 '<table class="hasAnonymousRow"><td>cell-1</td><td id="test">cell-2</td></table>', 72 '<table><tr><td><table id="test"></table></td></tr></table>', 73 '<table><tr><td><table><caption id="test">table-caption</caption></table></td></tr></table>', 74 '<table><tr><td><table class="hasAnonymousRow"><td id="test">cell</td></table></td></tr></table>', 75 '<table><tr><td><table class="hasAnonymousRow"><td>cell-1</td><td id="test">cell-2</td></table></td></tr></table>', 76 '<table><tr id="expected"><td><p id="test">paragraph</p></td></tr></table>', 77 ]; 78 79 for (const kTest of kTests) { 80 editor.innerHTML = kTest; 81 let firstRow = SpecialPowers.unwrap(getTableEditor().getFirstRow(document.getElementById("test"))); 82 if (document.getElementById("expected")) { 83 is(firstRow.tagName, "TR", `Result should be a <tr>: ${kTest}`); 84 is(firstRow.getAttribute("id"), "expected", `Result should be the first <tr> element in the <table>: ${kTest}`); 85 } else if (document.querySelector(".hasAnonymousRow")) { 86 is(firstRow.tagName, "TR", `Result should be a <tr>: ${kTest}`); 87 is(firstRow, document.querySelector(".hasAnonymousRow tr"), `Result should be the anonymous <tr> element in the <table>: ${kTest}`); 88 } else { 89 is(firstRow, null, `Result should be null if there is no <tr> element in the <table>: ${kTest}`); 90 } 91 } 92 93 SimpleTest.finish(); 94 }); 95 96 function getTableEditor() { 97 var Ci = SpecialPowers.Ci; 98 var editingSession = SpecialPowers.wrap(window).docShell.editingSession; 99 return editingSession.getEditorForWindow(window).QueryInterface(Ci.nsITableEditor); 100 } 101 102 </script> 103 </body> 104 105 </html>