Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!DOCTYPE>
<html>
<head>
<title>Test for nsITableEditor.getTableSize()</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();
let rowCount = {}, columnCount = {};
try {
getTableEditor().getTableSize(undefined, rowCount, columnCount);
ok(false, "nsITableEditor.getTableSize(undefined) should cause throwing an exception");
} catch (e) {
ok(true, "nsITableEditor.getTableSize(undefined) should cause throwing an exception");
}
try {
getTableEditor().getTableSize(null, rowCount, columnCount);
ok(false, "nsITableEditor.getTableSize(null) should cause throwing an exception");
} catch (e) {
ok(true, "nsITableEditor.getTableSize(null) should cause throwing an exception");
}
try {
getTableEditor().getTableSize(editor, rowCount, columnCount);
ok(false, "nsITableEditor.getTableSize() should cause throwing an exception if given node is not in a <table>");
} catch (e) {
ok(true, "nsITableEditor.getTableSize() should cause throwing an exception if given node is not in a <table>");
}
// Set id to "test" for the argument for getTableSize().
// Set data-rows and data-cols to expected count of them.
const kTests = [
'<table><tr><td id="test" data-rows="2" data-cols="3">cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>cell2-1</td><td>cell2-2</td><td>cell2-3</td></tr></table>',
'<table><tr id="test" data-rows="2" data-cols="3"><td>cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>cell2-1</td><td>cell2-2</td><td>cell2-3</td></tr></table>',
'<table id="test" data-rows="2" data-cols="3"><tr><td>cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>cell2-1</td><td>cell2-2</td><td>cell2-3</td></tr></table>',
'<table><tr><td>cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>cell2-1</td><td>cell2-2</td><td><p id="test" data-rows="2" data-cols="3">cell2-3</p></td></tr></table>',
'<table><caption id="test" data-rows="2" data-cols="3">caption</caption><tr><td>cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>cell2-1</td><td>cell2-2</td><td>cell2-3</td></tr></table>',
'<table id="test" data-rows="0" data-cols="0"></table>',
'<table id="test" data-rows="0" data-cols="0"><caption>caption</caption></table>',
'<table id="test" data-rows="1" data-cols="1"><td>cell1-1</td></table>',
// rowspan does not affect, but colspan affects...
'<table id="test" data-rows="1" data-cols="12"><tr><td rowspan="8" colspan="12">cell1-1</td></tr></table>',
'<table id="test" data-rows="1" data-cols="1"><tr><td><table><tr><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr><tr><td>cell3-1</td><td>cell3-2</td></tr></table></td></tr></table>',
'<table><tr><td id="test" data-rows="1" data-cols="1"><table><tr><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr><tr><td>cell3-1</td><td>cell3-2</td></tr></table></td></tr></table>',
'<table><tr><td><table id="test" data-rows="3" data-cols="2"><tr><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr><tr><td>cell3-1</td><td>cell3-2</td></tr></table></td></tr></table>',
'<table><tr><td><table><tr><td id="test" data-rows="3" data-cols="2">cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr><tr><td>cell3-1</td><td>cell3-2</td></tr></table></td></tr></table>',
'<table><tr><td><table><tr><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td><p id="test" data-rows="3" data-cols="2">cell2-2</p></td></tr><tr><td>cell3-1</td><td>cell3-2</td></tr></table></td></tr></table>',
];
for (const kTest of kTests) {
editor.innerHTML = kTest;
let element = document.getElementById("test");
getTableEditor().getTableSize(element, rowCount, columnCount);
is(rowCount.value.toString(10), element.getAttribute("data-rows"),
`Specified an element in a <table> directly, its parent table row count should be retrieved: ${kTest}`);
is(columnCount.value.toString(10), element.getAttribute("data-cols"),
`Specified an element in a <table> directly, its parent table column count should be retrieved: ${kTest}`);
if (element.firstChild && element.firstChild.nodeType == Node.TEXT_NODE) {
selection.collapse(element.firstChild, 0);
getTableEditor().getTableSize(null, rowCount, columnCount);
is(rowCount.value.toString(10), element.getAttribute("data-rows"),
`Selection is collapsed in a cell element, its parent table row count should be retrieved: ${kTest}`);
is(columnCount.value.toString(10), element.getAttribute("data-cols"),
`Selection is collapsed in a cell element, its parent table column count should be retrieved: ${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>