summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_nsITableEditor_getFirstRow.html
blob: 923453873359844eb7c58cf37e03e0c6f3f0eee1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!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 {
    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 {
    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 {
    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.
  const 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>