summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_nsITableEditor_getSelectedCellsType.html
blob: 33884b8e20a22d960bede972add77a43eac51177 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html>
<head>
  <title>Test for nsITableEditor.getSelectedCellsType()</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.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">
"use strict";

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

  const kTableSelectionMode_None = 0;
  const kTableSelectionMode_Cell = 1;
  const kTableSelectionMode_Row = 2;
  const kTableSelectionMode_Column = 3;

  (function test_without_selection_range() {
    selection.removeAllRanges();
    try {
      getTableEditor().getSelectedCellsType(null);
      ok(false, "nsITableEditor.getSelectedCellsType(null) should cause throwing an exception when editor does not have Selection");
    } catch (e) {
      ok(true, "nsITableEditor.getSelectedCellsType(null) should cause throwing an exception when editor does not have Selection");
    }
  })();

  (function test_without_table() {
    editor.innerHTML = "<p>Here is a paragraph</p>";
    selection.collapse(editor.querySelector("p").firstChild, 0);
    try {
      getTableEditor().getSelectedCellsType(null);
      ok(false, "nsITableEditor.getSelectedCellsType(null) should cause throwing an exception when editor does not have a <table>");
    } catch (e) {
      ok(true, "nsITableEditor.getSelectedCellsType(null) should cause throwing an exception when editor does not have a <table>");
    }
  })();

  (function test_with_selection_outside_table() {
    editor.innerHTML = "<p>Here is a paragraph before the table</p>" +
      "<table><tr><td>cell</td></tr></table>";
    selection.collapse(editor.querySelector("p").firstChild, 0);
    try {
      getTableEditor().getSelectedCellsType(null);
      ok(false, "nsITableEditor.getSelectedCellsType(null) should cause throwing an exception when selection is outside the <table>");
    } catch (e) {
      ok(true, "nsITableEditor.getSelectedCellsType(null) should cause throwing an exception when selection is outside the <table>");
    }
  })();

  (function test_with_selection_in_text_in_cell() {
    editor.innerHTML = "<table><tr><td>cell</td></tr></table>";
    selection.collapse(editor.querySelector("td").firstChild, 0);
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_None,
      "nsITableEditor.getSelectedCellsType(null) should return None when selection is collapsed in a text node in a cell");
  })();

  (function test_with_selecting_a_cell_which_is_only_one_cell_in_table() {
    editor.innerHTML = "<table><tr><td>cell</td></tr></table>";
    selection.setBaseAndExtent(editor.querySelector("tr"), 0, editor.querySelector("tr"), 1);
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_Row,
      "nsITableEditor.getSelectedCellsType(null) should return Row when selection selects the cell which is only one in the table");
  })();

  (function test_with_selecting_a_cell_whose_table_has_one_row() {
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr></table>";
    selection.setBaseAndExtent(editor.querySelector("tr"), 0, editor.querySelector("tr"), 1);
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_Column,
      "nsITableEditor.getSelectedCellsType(null) should return Column when selection selects a cell in the first row which is only one in the table");
  })();

  (function test_with_selecting_a_cell_whose_table_has_one_column() {
    editor.innerHTML = "<table><tr><td>cell1</td></tr><tr><td>cell2</td></tr></table>";
    selection.setBaseAndExtent(editor.querySelector("tr"), 0, editor.querySelector("tr"), 1);
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_Row,
      "nsITableEditor.getSelectedCellsType(null) should return Row when selection selects a cell in the first row which is only one in the row");
  })();

  (function test_with_selecting_a_cell_at_1_1_whose_table_has_2x2_cells() {
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    selection.setBaseAndExtent(editor.querySelector("tr"), 0, editor.querySelector("tr"), 1);
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_Cell,
      "nsITableEditor.getSelectedCellsType(null) should return Cell when selection selects a cell at first row and first column");
  })();

  (function test_with_selecting_a_cell_at_1_2_whose_table_has_2x2_cells() {
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    selection.setBaseAndExtent(editor.querySelector("tr"), 1, editor.querySelector("tr"), 2);
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_Cell,
      "nsITableEditor.getSelectedCellsType(null) should return Cell when selection selects a cell at first row and second column");
  })();

  (function test_with_selecting_a_cell_at_2_1_whose_table_has_2x2_cells() {
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    selection.setBaseAndExtent(editor.querySelector("tr + tr"), 0, editor.querySelector("tr + tr"), 1);
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_Cell,
      "nsITableEditor.getSelectedCellsType(null) should return Cell when selection selects a cell at second row and first column");
  })();

  (function test_with_selecting_a_cell_at_2_2_whose_table_has_2x2_cells() {
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    selection.setBaseAndExtent(editor.querySelector("tr + tr"), 1, editor.querySelector("tr + tr"), 2);
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_Cell,
      "nsITableEditor.getSelectedCellsType(null) should return Cell when selection selects a cell at second row and second column");
  })();

  (function test_with_selecting_a_cell_at_1_1_whose_colspan_2_and_whose_table_has_2x2_cells() {
    editor.innerHTML = "<table><tr><td colspan=\"2\">cell1</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    selection.setBaseAndExtent(editor.querySelector("tr"), 0, editor.querySelector("tr"), 1);
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_Row,
      "nsITableEditor.getSelectedCellsType(null) should return Row when selection selects a cell whose colspan is 2 at first row");
  })();

  (function test_with_selecting_a_cell_at_1_1_whose_rowspan_2_and_whose_table_has_2x2_cells() {
    editor.innerHTML = "<table><tr><td rowspan=\"2\">cell1</td><td>cell2</td></tr><tr><td>cell4</td></tr></table>";
    selection.setBaseAndExtent(editor.querySelector("tr"), 0, editor.querySelector("tr"), 1);
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_Column,
      "nsITableEditor.getSelectedCellsType(null) should return Column when selection selects a cell whose row is 2 at first column");
  })();

  (function test_with_selecting_all_cells_in_first_row_whose_table_has_2x2_cells() {
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    synthesizeMouseAtCenter(editor.querySelector("td"), {accelKey: true});
    synthesizeMouseAtCenter(editor.querySelector("td + td"), {accelKey: true});
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_Row,
      "nsITableEditor.getSelectedCellsType(null) should return Row when selection selects all cells in the first row whose table has 2 rows and 2 columns");
  })();

  (function test_with_selecting_all_cells_in_first_column_whose_table_has_2x2_cells() {
    selection.removeAllRanges();
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    synthesizeMouseAtCenter(editor.querySelector("td"), {accelKey: true});
    synthesizeMouseAtCenter(editor.querySelector("tr + tr > td"), {accelKey: true});
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_Column,
      "nsITableEditor.getSelectedCellsType(null) should return Row when selection selects all cells in the first column whose table has 2 rows and 2 columns");
  })();

  (function test_with_selecting_all_cells_whose_table_has_2x2_cells() {
    selection.removeAllRanges();
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    synthesizeMouseAtCenter(editor.querySelector("td"), {accelKey: true});
    synthesizeMouseAtCenter(editor.querySelector("td + td"), {accelKey: true});
    synthesizeMouseAtCenter(editor.querySelector("tr + tr > td"), {accelKey: true});
    synthesizeMouseAtCenter(editor.querySelector("tr + tr > td + td"), {accelKey: true});
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_Row,
      "nsITableEditor.getSelectedCellsType(null) should return Row when selection selects all cells whose table has 2 rows and 2 columns");
  })();

  (function test_with_selecting_a_raw() {
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    selection.setBaseAndExtent(editor.querySelector("tbody"), 0, editor.querySelector("tbody"), 1);
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_None,
      "nsITableEditor.getSelectedCellsType(null) should return None when selection selects a row");
  })();

  (function test_with_selecting_a_tbody() {
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    selection.setBaseAndExtent(editor.querySelector("table"), 0, editor.querySelector("table"), 1);
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_None,
      "nsITableEditor.getSelectedCellsType(null) should return None when selection selects a tbody");
  })();

  (function test_with_selecting_a_table() {
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    selection.setBaseAndExtent(editor, 0, editor, 1);
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_None,
      "nsITableEditor.getSelectedCellsType(null) should return None when selection selects a table");
  })();

  (function test_with_selecting_cells_in_different_table() {
    selection.removeAllRanges();
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>" +
      "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    let range = document.createRange();
    range.selectNode(editor.querySelector("td"));
    selection.addRange(range);
    range = document.createRange();
    range.selectNode(editor.querySelector("table + table td"));
    selection.addRange(range);
    is(getTableEditor().getSelectedCellsType(null), kTableSelectionMode_Cell,
      "nsITableEditor.getSelectedCellsType(null) should return Cell when selection selects 2 cells in different tables");
  })();

  (function test_with_selecting_a_cell_in_the_table() {
    selection.removeAllRanges();
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>" +
      "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    let range = document.createRange();
    range.selectNode(editor.querySelector("td"));
    selection.addRange(range);
    is(getTableEditor().getSelectedCellsType(editor.querySelector("table")), kTableSelectionMode_Cell,
      "nsITableEditor.getSelectedCellsType(editor.querySelector(\"table\")) should return Cell when selection selects a cell in the table");
  })();

  (function test_with_selecting_a_cell_in_the_table_specifying_different_cell() {
    selection.removeAllRanges();
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>" +
      "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    let range = document.createRange();
    range.selectNode(editor.querySelector("tr + tr > td"));
    selection.addRange(range);
    is(getTableEditor().getSelectedCellsType(editor.querySelector("td")), kTableSelectionMode_Cell,
      "nsITableEditor.getSelectedCellsType(editor.querySelector(\"td\")) should return Cell when selection selects a cell in the table");
  })();

  (function test_with_selecting_a_cell_in_the_other_table() {
    selection.removeAllRanges();
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>" +
      "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    let range = document.createRange();
    range.selectNode(editor.querySelector("td"));
    selection.addRange(range);
    todo_is(getTableEditor().getSelectedCellsType(editor.querySelector("table + table")), kTableSelectionMode_None,
      "nsITableEditor.getSelectedCellsType(editor.querySelector(\"table + table\")) should return None when selection selects a cell in the other table");
  })();

  (function test_with_selecting_a_cell_in_the_other_table_specifying_a_cell_in_the_other_one() {
    selection.removeAllRanges();
    editor.innerHTML = "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>" +
      "<table><tr><td>cell1</td><td>cell2</td></tr><tr><td>cell3</td><td>cell4</td></tr></table>";
    let range = document.createRange();
    range.selectNode(editor.querySelector("td"));
    selection.addRange(range);
    todo_is(getTableEditor().getSelectedCellsType(editor.querySelector("table + table td")), kTableSelectionMode_None,
      "nsITableEditor.getSelectedCellsType(editor.querySelector(\"table + table td\")) should return None when selection selects a cell in the other table");
  })();

  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>