summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_nsITableEditor_getCellAt.html
blob: f2544a2b006f23b1ae9c87a06e09f1c9db0886b3 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE>
<html>
<head>
  <title>Test for nsITableEditor.getCellAt()</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");
  let selection = document.getSelection();

  try {
    SpecialPowers.unwrap(getTableEditor().getCellAt(undefined, 0, 0));
    ok(false, "nsITableEditor.getCellAt(undefined) should cause throwing an exception when editor does not have Selection");
  } catch (e) {
    ok(true, "nsITableEditor.getCellAt(undefined) should cause throwing an exception when editor does not have Selection");
  }

  try {
    SpecialPowers.unwrap(getTableEditor().getTableSize(null, 0, 0));
    ok(false, "nsITableEditor.getCellAt(null) should cause throwing an exception when editor does not have Selection");
  } catch (e) {
    ok(true, "nsITableEditor.getCellAt(null) should cause throwing an exception when editor does not have Selection");
  }

  // XXX This is inconsistent behavior with other APIs.
  try {
    let cell = SpecialPowers.unwrap(getTableEditor().getCellAt(editor, 0, 0));
    ok(true, "nsITableEditor.getCellAt() should not cause throwing exception even if given node is not a <table>");
    is(cell, null, "nsITableEditor.getCellAt() should return null if given node is not a <table>");
  } catch (e) {
    ok(false, "nsITableEditor.getCellAt() should not cause throwing exception even if given node is not a <table>");
  }

  editor.innerHTML =
    '<table id="table">' +
      '<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>' +
      '<tr><td id="c2-1" rowspan="2">cell2-1</td><td id="c2-2">cell2-2<td id="c2-3">cell2-3</td></tr>' +
      '<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>' +
      '<tr><td id="c4-1" rowspan="4">cell4-1</td><td id="c4-2">' +
        '<table id="inner-table"><tr><td id="c2-1-1">cell2-1-1</td><td id="c2-1-2">cell2-1-2</td></tr>' +
          '<tr><td id="c2-2-1">cell2-2-1</td><td id="c2-2-2">cell2-2-2</td></table>' +
        '</td><td id="c4-3">cell4-3</td><td id="c4-4">cell4-4</td><td id="c4-5">cell4-5</td></tr>' +
      '<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>' +
      '<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>' +
      '<tr><td id="c7-2" colspan="4">cell7-2</td></tr>' +
    "</table>";

  const kTestsInParent = [
    { row: 0, column: 0, expected: "c1-1" },
    { row: 0, column: 3, expected: "c1-4" },
    { row: 0, column: 4, expected: "c1-4" },
    { row: 1, column: 3, expected: "c1-4" },
    { row: 1, column: 4, expected: "c1-4" },
    { row: 1, column: 0, expected: "c2-1" },
    { row: 2, column: 0, expected: "c2-1" },
    { row: 3, column: 0, expected: "c4-1" },
    { row: 4, column: 0, expected: "c4-1" },
    { row: 5, column: 0, expected: "c4-1" },
    { row: 6, column: 0, expected: "c4-1" },
    { row: 4, column: 2, expected: "c5-3" },
    { row: 4, column: 3, expected: "c5-3" },
    { row: 4, column: 4, expected: "c5-5" },
    { row: 6, column: 1, expected: "c7-2" },
    { row: 6, column: 2, expected: "c7-2" },
    { row: 6, column: 3, expected: "c7-2" },
    { row: 6, column: 4, expected: "c7-2" },
    { row: 6, column: 5, expected: null },
  ];

  let table = document.getElementById("table");
  for (const kTest of kTestsInParent) {
    let cell = SpecialPowers.unwrap(getTableEditor().getCellAt(table, kTest.row, kTest.column));
    if (kTest.expected === null) {
      is(cell, null,
         `Specified the parent <table> element directly (${kTest.row} - ${kTest.column})`);
    } else {
      is(cell.getAttribute("id"), kTest.expected,
         `Specified the parent <table> element directly (${kTest.row} - ${kTest.column})`);
    }
    if (cell && cell.firstChild && cell.firstChild.nodeType == Node.TEXT_NODE) {
      selection.collapse(cell.firstChild, 0);
      cell = getTableEditor().getCellAt(null, kTest.row, kTest.column);
      is(cell.getAttribute("id"), kTest.expected,
         `Selection is collapsed in a cell element in the parent <table> (${kTest.row} - ${kTest.column})`);
    }
  }

  const kTestsInChild = [
    { row: 0, column: 0, expected: "c2-1-1" },
    { row: 0, column: 1, expected: "c2-1-2" },
    { row: 0, column: 2, expected: null },
    { row: 1, column: 0, expected: "c2-2-1" },
    { row: 1, column: 1, expected: "c2-2-2" },
    { row: 2, column: 0, expected: null },
  ];

  let innerTable = document.getElementById("inner-table");
  for (const kTest of kTestsInChild) {
    let cell = SpecialPowers.unwrap(getTableEditor().getCellAt(innerTable, kTest.row, kTest.column));
    if (kTest.expected === null) {
      is(cell, null,
         `Specified the inner <table> element directly (${kTest.row} - ${kTest.column})`);
    } else {
      is(cell.getAttribute("id"), kTest.expected,
         `Specified the inner <table> element directly (${kTest.row} - ${kTest.column})`);
    }
    if (cell && cell.firstChild && cell.firstChild.nodeType == Node.TEXT_NODE) {
      selection.collapse(cell.firstChild, 0);
      cell = getTableEditor().getCellAt(null, kTest.row, kTest.column);
      is(cell.getAttribute("id"), kTest.expected,
         `Selection is collapsed in a cell element in the inner <table> (${kTest.row} - ${kTest.column})`);
    }
  }

  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>