Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!DOCTYPE>
<html>
<head>
<title>Test for nsITableEditor.getSelectedCells()</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">
"use strict";
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(() => {
let editor = document.getElementById("content");
let selection = document.getSelection();
(function test_with_collapsed_selection() {
selection.collapse(editor, 0);
let cells = getTableEditor().getSelectedCells();
is(cells.length, 0,
"nsITableEditor.getSelectedCells() should return empty array if Selection does not select cells");
})();
editor.innerHTML =
'<table id="table">' +
'<tr id="r1"><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 id="r2"><th id="c2-1" rowspan="2">cell2-1</th><td id="c2-2">cell2-2</td><td id="c2-3">cell2-3</td></tr>' +
'<tr id="r3"><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 id="r4"><td id="c4-1" rowspan="4">cell4-1</td><td id="c4-2">cell4-2</td><td id="c4-3">cell4-3</td><th id="c4-4">cell4-4</th><td id="c4-5">cell4-5</td></tr>' +
'<tr id="r5"><th id="c5-2">cell5-2</th><th id="c5-3" colspan="2">' +
'<table><tr id="r2-1"><td id="c2-1-1">cell2-1-1</td></tr></table>' +
'</th><td id="c5-5">cell5-5</td></tr>' +
'<tr id="r6"><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 id="r7"><td id="c7-2" colspan="4">cell7-2</td></tr>' +
"</table>";
(function test_with_selecting_1_1() {
let tr = document.getElementById("r1");
selection.setBaseAndExtent(tr, 0, tr, 1);
let cells = getTableEditor().getSelectedCells();
is(cells.length, 1,
"#1-1 nsITableEditor.getSelectedCells() should return an array whose length is 1");
is(SpecialPowers.unwrap(cells[0]), document.getElementById("c1-1"),
"#1-1 nsITableEditor.getSelectedCells() should set the first item of the result to the first cell element in the first row");
})();
(function test_with_selecting_1_4() {
let tr = document.getElementById("r1");
selection.setBaseAndExtent(tr, 3, tr, 4);
let cells = getTableEditor().getSelectedCells();
is(cells.length, 1,
"#1-4 nsITableEditor.getSelectedCells() should return an array whose length is 1");
is(SpecialPowers.unwrap(cells[0]), document.getElementById("c1-4"),
"#1-4 nsITableEditor.getSelectedCells() should set the first item of the result to the last cell element whose colspan and rowspan are 2 in the first row");
})();
(function test_with_selecting_2_1() {
let tr = document.getElementById("r2");
selection.setBaseAndExtent(tr, 0, tr, 1);
let cells = getTableEditor().getSelectedCells();
is(cells.length, 1,
"#2-1 nsITableEditor.getSelectedCells() should return an array whose length is 1");
is(SpecialPowers.unwrap(cells[0]), document.getElementById("c2-1"),
"#2-1 nsITableEditor.getSelectedCells() should set the first item of the result to the first cell element in the second row");
})();
(function test_with_selecting_7_2() {
let tr = document.getElementById("r7");
selection.setBaseAndExtent(tr, 0, tr, 1);
let cells = getTableEditor().getSelectedCells();
is(cells.length, 1,
"#7-2 nsITableEditor.getSelectedCells() should return an array whose length is 1");
is(SpecialPowers.unwrap(cells[0]), document.getElementById("c7-2"),
"#7-2 nsITableEditor.getSelectedCells() should set the first item of the result to the second cell element in the last row");
})();
(function test_with_selecting_2_2_and_2_3() {
let tr = document.getElementById("r2");
selection.removeAllRanges();
let range = document.createRange();
range.setStart(tr, 1);
range.setEnd(tr, 2);
selection.addRange(range);
range = document.createRange();
range.setStart(tr, 2);
range.setEnd(tr, 3);
selection.addRange(range);
let cells = getTableEditor().getSelectedCells();
is(cells.length, 2,
"#2-2 nsITableEditor.getSelectedCells() should return an array whose length is 2");
is(SpecialPowers.unwrap(cells[0]), document.getElementById("c2-2"),
"#2-2 nsITableEditor.getSelectedCells() should set the first item of the result to the second cell element in the second row");
})();
(function test_with_selecting_3_4_and_5_2() {
let tr = document.getElementById("r3");
selection.removeAllRanges();
let range = document.createRange();
range.setStart(tr, 2);
range.setEnd(tr, 3);
selection.addRange(range);
range = document.createRange();
range.setStart(document.getElementById("r5"), 0);
range.setEnd(document.getElementById("r5"), 1);
selection.addRange(range);
let cells = getTableEditor().getSelectedCells();
is(cells.length, 2,
"#3-4, #5-2 nsITableEditor.getSelectedCells() should return an array whose length is 2");
is(SpecialPowers.unwrap(cells[0]), document.getElementById("c3-4"),
"#3-4, #5-2 nsITableEditor.getSelectedCells() should set the first item of the result to the last cell element in the third row");
is(SpecialPowers.unwrap(cells[1]), document.getElementById("c5-2"),
"#3-4, #5-2 nsITableEditor.getSelectedCells() should set the second item of the result to the first cell element in the fifth row");
})();
(function test_with_selecting_1_2_and_1_3_and_1_4_and_2_1_and_2_2() {
selection.removeAllRanges();
let tr = document.getElementById("r1");
let range = document.createRange();
range.setStart(tr, 1);
range.setEnd(tr, 2);
selection.addRange(range);
range = document.createRange();
range.setStart(tr, 2);
range.setEnd(tr, 3);
selection.addRange(range);
range = document.createRange();
range.setStart(tr, 3);
range.setEnd(tr, 4);
selection.addRange(range);
tr = document.getElementById("r2");
range = document.createRange();
range.setStart(tr, 0);
range.setEnd(tr, 1);
selection.addRange(range);
range = document.createRange();
range.setStart(tr, 1);
range.setEnd(tr, 2);
selection.addRange(range);
let cells = getTableEditor().getSelectedCells();
is(cells.length, 5,
"#1-2, #1-3, #1-4, #2-1, #2-2 nsITableEditor.getSelectedCells() should return an array whose length is 5");
is(SpecialPowers.unwrap(cells[0]), document.getElementById("c1-2"),
"#1-2, #1-3, #1-4, #2-1, #2-2 nsITableEditor.getSelectedCells() should set the first item of the result to the second cell element in the first row");
is(SpecialPowers.unwrap(cells[1]), document.getElementById("c1-3"),
"#1-2, #1-3, #1-4, #2-1, #2-2 nsITableEditor.getSelectedCells() should set the second item of the result to the third cell element in the first row");
is(SpecialPowers.unwrap(cells[2]), document.getElementById("c1-4"),
"#1-2, #1-3, #1-4, #2-1, #2-2 nsITableEditor.getSelectedCells() should set the third item of the result to the forth cell element in the first row");
is(SpecialPowers.unwrap(cells[3]), document.getElementById("c2-1"),
"#1-2, #1-3, #1-4, #2-1, #2-2 nsITableEditor.getSelectedCells() should set the forth item of the result to the first cell element in the second row");
is(SpecialPowers.unwrap(cells[4]), document.getElementById("c2-2"),
"#1-2, #1-3, #1-4, #2-1, #2-2 nsITableEditor.getSelectedCells() should set the forth item of the result to the second cell element in the second row");
})();
(function test_with_selecting_6_3_and_paragraph_in_6_4_and_6_5() {
selection.removeAllRanges();
let tr = document.getElementById("r6");
let range = document.createRange();
range.setStart(tr, 1);
range.setEnd(tr, 2);
selection.addRange(range);
range = document.createRange();
range.setStart(document.getElementById("c6-4").firstChild, 0);
range.setEnd(document.getElementById("c6-4").firstChild, 1);
selection.addRange(range);
range = document.createRange();
range.setStart(tr, 3);
range.setEnd(tr, 4);
selection.addRange(range);
let cells = getTableEditor().getSelectedCells();
is(cells.length, 2,
"#6-3, #6-5 nsITableEditor.getSelectedCells() should return an array whose length is 2");
is(SpecialPowers.unwrap(cells[0]), document.getElementById("c6-3"),
"#6-3, #6-5 nsITableEditor.getSelectedCells() should set the first item of the result to the second cell element in the sixth row");
// The <p> element in c6-4 is selected, this does not select the cell
// element so that it should be ignored.
is(SpecialPowers.unwrap(cells[1]), document.getElementById("c6-5"),
"#6-3, #6-5 nsITableEditor.getSelectedCells() should set the first item of the result to the forth cell element in the sixth row");
})();
(function test_with_selecting_2_3_and_text_in_4_1_and_7_2() {
selection.removeAllRanges();
let tr = document.getElementById("r2");
let range = document.createRange();
range.setStart(tr, 2);
range.setEnd(tr, 3);
selection.addRange(range);
range = document.createRange();
range.setStart(document.getElementById("c4-1").firstChild, 0);
range.setEnd(document.getElementById("c4-1").firstChild, 7);
selection.addRange(range);
tr = document.getElementById("r7");
range = document.createRange();
range.setStart(tr, 0);
range.setEnd(tr, 1);
selection.addRange(range);
let cells = getTableEditor().getSelectedCells();
is(cells.length, 2,
"#2-3, #7-2 nsITableEditor.getSelectedCells() should return an array whose length is 2");
is(SpecialPowers.unwrap(cells[0]), document.getElementById("c2-3"),
"#2-3, #7-2 nsITableEditor.getSelectedCells() should set the first item of the result to the third cell element in the second row");
// Text in c4-1 is selected, this does not select the cell element so that
// it should be ignored. Note that we've ignored the following selected
// cell elements in old API, but it causes inconsistent behavior with the
// previous test case. Therefore, we take this behavior.
is(SpecialPowers.unwrap(cells[1]), document.getElementById("c7-2"),
"#2-3, #7-2 nsITableEditor.getSelectedCells() should set the second item of the result to the cell element in the seventh row");
})();
(function test_with_selecting_3_2_and_2_1_1_and_7_2() {
selection.removeAllRanges();
let tr = document.getElementById("r3");
let range = document.createRange();
range.setStart(tr, 0);
range.setEnd(tr, 1);
selection.addRange(range);
tr = document.getElementById("r2-1");
range = document.createRange();
range.setStart(tr, 0);
range.setEnd(tr, 1);
selection.addRange(range);
tr = document.getElementById("r7");
range = document.createRange();
range.setStart(tr, 0);
range.setEnd(tr, 1);
selection.addRange(range);
let cells = getTableEditor().getSelectedCells();
is(cells.length, 3,
"#3-2, #2-1-1, #7-2 nsITableEditor.getSelectedCells() should return an array whose length is 3");
is(SpecialPowers.unwrap(cells[0]), document.getElementById("c3-2"),
"#3-2, #2-1-1, #7-2 nsITableEditor.getSelectedCells() should set the first item of the result to the first cell element in the third row");
// c2-1-1 is in another <table>, however, getSelectedCells() returns it
// since it works only with ranges of Selection.
is(SpecialPowers.unwrap(cells[1]), document.getElementById("c2-1-1"),
"#3-2, #2-1-1, #7-2 nsITableEditor.getSelectedCells() should set the second item of the result to the cell element in the child <table> element");
is(SpecialPowers.unwrap(cells[2]), document.getElementById("c7-2"),
"#3-2, #2-1-1, #7-2 nsITableEditor.getSelectedCells() should set the third item of the result to the cell element in the seventh row");
})();
(function test_with_selecting_all_children_of_cell() {
selection.selectAllChildren(document.getElementById("c6-4"));
let cells = getTableEditor().getSelectedCells();
is(cells.length, 0,
"nsITableEditor.getSelectedCells() should return an empty array when no cell element is selected");
})();
(function test_with_selecting_text_in_cell() {
let cell = document.getElementById("c6-5");
selection.collapse(cell.firstChild, 0);
let cells = getTableEditor().getSelectedCells();
is(cells.length, 0,
"nsITableEditor.getSelectedCells() should return an empty array when selecting text in a cell element");
})();
(function test_with_selecting_text_in_1_1_and_1_2() {
let cell = document.getElementById("c1-1");
selection.setBaseAndExtent(cell.firstChild, 0, cell.firstChild, 3);
let range = document.createRange();
range.setStart(cell.parentNode, 1);
range.setEnd(cell.parentNode, 2);
selection.addRange(range);
let cells = getTableEditor().getSelectedCells();
is(cells.length, 0,
"nsITableEditor.getSelectedCells() should return an empty array when the first range does not select a cell element");
})();
(function test_without_selection_ranges() {
selection.removeAllRanges();
let cells = getTableEditor().getSelectedCells();
is(cells.length, 0,
"nsITableEditor.getSelectedCells() should return an empty array even when there is no selection range");
})();
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>