tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_nsITableEditor_getCellAt.html (6004B)


      1 <!DOCTYPE>
      2 <html>
      3 <head>
      4  <title>Test for nsITableEditor.getCellAt()</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  try {
     23    SpecialPowers.unwrap(getTableEditor().getCellAt(undefined, 0, 0));
     24    ok(false, "nsITableEditor.getCellAt(undefined) should cause throwing an exception when editor does not have Selection");
     25  } catch (e) {
     26    ok(true, "nsITableEditor.getCellAt(undefined) should cause throwing an exception when editor does not have Selection");
     27  }
     28 
     29  try {
     30    SpecialPowers.unwrap(getTableEditor().getTableSize(null, 0, 0));
     31    ok(false, "nsITableEditor.getCellAt(null) should cause throwing an exception when editor does not have Selection");
     32  } catch (e) {
     33    ok(true, "nsITableEditor.getCellAt(null) should cause throwing an exception when editor does not have Selection");
     34  }
     35 
     36  // XXX This is inconsistent behavior with other APIs.
     37  try {
     38    let cell = SpecialPowers.unwrap(getTableEditor().getCellAt(editor, 0, 0));
     39    ok(true, "nsITableEditor.getCellAt() should not cause throwing exception even if given node is not a <table>");
     40    is(cell, null, "nsITableEditor.getCellAt() should return null if given node is not a <table>");
     41  } catch (e) {
     42    ok(false, "nsITableEditor.getCellAt() should not cause throwing exception even if given node is not a <table>");
     43  }
     44 
     45  editor.innerHTML =
     46    '<table id="table">' +
     47      '<tr><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>' +
     48      '<tr><td id="c2-1" rowspan="2">cell2-1</td><td id="c2-2">cell2-2<td id="c2-3">cell2-3</td></tr>' +
     49      '<tr><td id="c3-2">cell3-2</td><td id="c3-3">cell3-3</td><td id="c3-4" colspan="2">cell3-4</td></tr>' +
     50      '<tr><td id="c4-1" rowspan="4">cell4-1</td><td id="c4-2">' +
     51        '<table id="inner-table"><tr><td id="c2-1-1">cell2-1-1</td><td id="c2-1-2">cell2-1-2</td></tr>' +
     52          '<tr><td id="c2-2-1">cell2-2-1</td><td id="c2-2-2">cell2-2-2</td></table>' +
     53        '</td><td id="c4-3">cell4-3</td><td id="c4-4">cell4-4</td><td id="c4-5">cell4-5</td></tr>' +
     54      '<tr><td id="c5-2">cell5-2</td><td id="c5-3" colspan="2">cell5-3</td><td id="c5-5">cell5-5</td></tr>' +
     55      '<tr><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>' +
     56      '<tr><td id="c7-2" colspan="4">cell7-2</td></tr>' +
     57    "</table>";
     58 
     59  const kTestsInParent = [
     60    { row: 0, column: 0, expected: "c1-1" },
     61    { row: 0, column: 3, expected: "c1-4" },
     62    { row: 0, column: 4, expected: "c1-4" },
     63    { row: 1, column: 3, expected: "c1-4" },
     64    { row: 1, column: 4, expected: "c1-4" },
     65    { row: 1, column: 0, expected: "c2-1" },
     66    { row: 2, column: 0, expected: "c2-1" },
     67    { row: 3, column: 0, expected: "c4-1" },
     68    { row: 4, column: 0, expected: "c4-1" },
     69    { row: 5, column: 0, expected: "c4-1" },
     70    { row: 6, column: 0, expected: "c4-1" },
     71    { row: 4, column: 2, expected: "c5-3" },
     72    { row: 4, column: 3, expected: "c5-3" },
     73    { row: 4, column: 4, expected: "c5-5" },
     74    { row: 6, column: 1, expected: "c7-2" },
     75    { row: 6, column: 2, expected: "c7-2" },
     76    { row: 6, column: 3, expected: "c7-2" },
     77    { row: 6, column: 4, expected: "c7-2" },
     78    { row: 6, column: 5, expected: null },
     79  ];
     80 
     81  let table = document.getElementById("table");
     82  for (const kTest of kTestsInParent) {
     83    let cell = SpecialPowers.unwrap(getTableEditor().getCellAt(table, kTest.row, kTest.column));
     84    if (kTest.expected === null) {
     85      is(cell, null,
     86         `Specified the parent <table> element directly (${kTest.row} - ${kTest.column})`);
     87    } else {
     88      is(cell.getAttribute("id"), kTest.expected,
     89         `Specified the parent <table> element directly (${kTest.row} - ${kTest.column})`);
     90    }
     91    if (cell && cell.firstChild && cell.firstChild.nodeType == Node.TEXT_NODE) {
     92      selection.collapse(cell.firstChild, 0);
     93      cell = getTableEditor().getCellAt(null, kTest.row, kTest.column);
     94      is(cell.getAttribute("id"), kTest.expected,
     95         `Selection is collapsed in a cell element in the parent <table> (${kTest.row} - ${kTest.column})`);
     96    }
     97  }
     98 
     99  const kTestsInChild = [
    100    { row: 0, column: 0, expected: "c2-1-1" },
    101    { row: 0, column: 1, expected: "c2-1-2" },
    102    { row: 0, column: 2, expected: null },
    103    { row: 1, column: 0, expected: "c2-2-1" },
    104    { row: 1, column: 1, expected: "c2-2-2" },
    105    { row: 2, column: 0, expected: null },
    106  ];
    107 
    108  let innerTable = document.getElementById("inner-table");
    109  for (const kTest of kTestsInChild) {
    110    let cell = SpecialPowers.unwrap(getTableEditor().getCellAt(innerTable, kTest.row, kTest.column));
    111    if (kTest.expected === null) {
    112      is(cell, null,
    113         `Specified the inner <table> element directly (${kTest.row} - ${kTest.column})`);
    114    } else {
    115      is(cell.getAttribute("id"), kTest.expected,
    116         `Specified the inner <table> element directly (${kTest.row} - ${kTest.column})`);
    117    }
    118    if (cell && cell.firstChild && cell.firstChild.nodeType == Node.TEXT_NODE) {
    119      selection.collapse(cell.firstChild, 0);
    120      cell = getTableEditor().getCellAt(null, kTest.row, kTest.column);
    121      is(cell.getAttribute("id"), kTest.expected,
    122         `Selection is collapsed in a cell element in the inner <table> (${kTest.row} - ${kTest.column})`);
    123    }
    124  }
    125 
    126  SimpleTest.finish();
    127 });
    128 
    129 function getTableEditor() {
    130  var Ci = SpecialPowers.Ci;
    131  var editingSession = SpecialPowers.wrap(window).docShell.editingSession;
    132  return editingSession.getEditorForWindow(window).QueryInterface(Ci.nsITableEditor);
    133 }
    134 
    135 </script>
    136 </body>
    137 
    138 </html>