editor/libeditor/tests/test_nsITableEditor_getFirstRow.html
author Mark Banner <standard8@mozilla.com>
Thu, 13 Sep 2018 07:58:19 +0000
changeset 436109 de36f39840c64a50ccc45f8c0593d8dcd651dfc2
parent 432703 af33be3aee822ddf186798da08ca3d9863557b30
child 436110 0d8eb8a0c2b4ad081ffad8ecf172b82d16c38d01
permissions -rw-r--r--
Bug 1489980 - Enable ESLint for editor/ - automatic fixes r=masayuki These are all automatically generated by ESLint with the --fix option. Depends on D5584 Differential Revision: https://phabricator.services.mozilla.com/D5585

<!DOCTYPE>
<html>
<head>
  <title>Test for nsITableEditor.getFirstRow()</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<div id="display">
</div>
<div id="content" contenteditable></div>
<pre id="test">
</pre>

<script class="testbody" type="application/javascript">

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(function() {
  let editor = document.getElementById("content");

  try {
    let ret = SpecialPowers.unwrap(getTableEditor().getFirstRow(undefined));
    ok(false, "nsITableEditor.getFirstRow(undefined) should cause throwing an exception");
  } catch (e) {
    ok(true, "nsITableEditor.getFirstRow(undefined) should cause throwing an exception");
  }

  try {
    let ret = SpecialPowers.unwrap(getTableEditor().getFirstRow(null));
    ok(false, "nsITableEditor.getFirstRow(null) should cause throwing an exception");
  } catch (e) {
    ok(true, "nsITableEditor.getFirstRow(null) should cause throwing an exception");
  }

  try {
    let ret = SpecialPowers.unwrap(getTableEditor().getFirstRow(editor));
    ok(false, "nsITableEditor.getFirstRow() should cause throwing an exception if given node is not in <table>");
  } catch (e) {
    ok(true, "nsITableEditor.getFirstRow() should cause throwing an exception if given node is not in <table>");
  }

  // Set id to "test" for the argument for getFirstRow().
  // Set id to "expected" for the expected <tr> element result (if there is).
  // Set class of <table> to "hasAnonymousRow" if it does not has <tr> but will be anonymous <tr> element is created.
  kTests = [
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<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>',
    '<table id="test"></table>',
    '<table><caption id="test">table-caption</caption></table>',
    '<table><caption>table-caption</caption><tr id="expected"><td id="test">cell</td></tr></table>',
    '<table class="hasAnonymousRow"><td id="test">cell</td></table>',
    '<table class="hasAnonymousRow"><td>cell-1</td><td id="test">cell-2</td></table>',
    '<table><tr><td><table id="test"></table></td></tr></table>',
    '<table><tr><td><table><caption id="test">table-caption</caption></table></td></tr></table>',
    '<table><tr><td><table class="hasAnonymousRow"><td id="test">cell</td></table></td></tr></table>',
    '<table><tr><td><table class="hasAnonymousRow"><td>cell-1</td><td id="test">cell-2</td></table></td></tr></table>',
    '<table><tr id="expected"><td><p id="test">paragraph</p></td></tr></table>',
  ];

  for (const kTest of kTests) {
    editor.innerHTML = kTest;
    let firstRow = SpecialPowers.unwrap(getTableEditor().getFirstRow(document.getElementById("test")));
    if (document.getElementById("expected")) {
      is(firstRow.tagName, "TR", `Result should be a <tr>: ${kTest}`);
      is(firstRow.getAttribute("id"), "expected", `Result should be the first <tr> element in the <table>: ${kTest}`);
    } else if (document.querySelector(".hasAnonymousRow")) {
      is(firstRow.tagName, "TR", `Result should be a <tr>: ${kTest}`);
      is(firstRow, document.querySelector(".hasAnonymousRow tr"), `Result should be the anonymous <tr> element in the <table>: ${kTest}`);
    } else {
      is(firstRow, null, `Result should be null if there is no <tr> element in the <table>: ${kTest}`);
    }
  }

  SimpleTest.finish();
});

function getTableEditor() {
  var Ci = SpecialPowers.Ci;
  var editingSession = SpecialPowers.wrap(window).docShell.editingSession;
  return editingSession.getEditorForWindow(window).QueryInterface(Ci.nsITableEditor);
}

</script>
</body>

</html>