Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!DOCTYPE>
<html>
<head>
<title>Test for nsIHTMLEditor.setBackgroundColor()</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_selecting_table_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_without_CSS() {
(function test_with_selecting_1_1() {
let cell = document.getElementById("c1-1");
let range = document.createRange();
range.selectNode(cell);
selection.removeAllRanges();
selection.addRange(range);
getHTMLEditor().setBackgroundColor("#ff0000");
is(cell.getAttribute("bgcolor"), "#ff0000",
'#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the first cell element in the first row');
ok(!cell.nextSibling.hasAttribute("bgcolor"),
'#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the other cells');
ok(!cell.parentNode.hasAttribute("bgcolor"),
'#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the parent row');
ok(!cell.parentNode.parentNode.hasAttribute("bgcolor"),
'#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the parent tbody');
ok(!cell.parentNode.parentNode.parentNode.hasAttribute("bgcolor"),
'#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the parent table');
ok(!editor.hasAttribute("bgcolor"),
'#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the editing host');
ok(!document.body.hasAttribute("bgcolor"),
'#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the body');
ok(!document.documentElement.hasAttribute("bgcolor"),
'#1-1 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the html');
getHTMLEditor().setBackgroundColor("");
ok(!cell.hasAttribute("bgcolor"),
'#1-1 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the first cell element in the first row');
})();
(function test_with_selecting_2_2_and_2_3() {
let cell2_2 = editor.querySelector("#c2-2");
let cell2_3 = editor.querySelector("#c2-3");
selection.removeAllRanges();
let range = document.createRange();
range.selectNode(cell2_2);
selection.addRange(range);
range = document.createRange();
range.selectNode(cell2_3);
selection.addRange(range);
getHTMLEditor().setBackgroundColor("#ff0000");
is(cell2_2.getAttribute("bgcolor"), "#ff0000",
'#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the second cell element in the second row');
is(cell2_3.getAttribute("bgcolor"), "#ff0000",
'#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the third cell element in the second row');
ok(!cell2_2.parentNode.hasAttribute("bgcolor"),
'#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the parent row');
ok(!cell2_2.parentNode.parentNode.hasAttribute("bgcolor"),
'#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the parent tbody');
ok(!cell2_2.parentNode.parentNode.parentNode.hasAttribute("bgcolor"),
'#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the parent table');
ok(!editor.hasAttribute("bgcolor"),
'#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the editing host');
ok(!document.body.hasAttribute("bgcolor"),
'#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the body');
ok(!document.documentElement.hasAttribute("bgcolor"),
'#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the html');
getHTMLEditor().setBackgroundColor("");
ok(!cell2_2.hasAttribute("bgcolor"),
'#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the second cell element in the second row');
ok(!cell2_3.hasAttribute("bgcolor"),
'#2-2, #2-3 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the third cell element in the second row');
})();
(function test_with_selecting_6_3_and_paragraph_in_6_4_and_6_5() {
selection.removeAllRanges();
let cell6_3 = editor.querySelector("#c6-3");
let cell6_4 = editor.querySelector("#c6-4");
let cell6_5 = editor.querySelector("#c6-5");
let range = document.createRange();
range.selectNode(cell6_3);
selection.addRange(range);
range = document.createRange();
range.selectNode(cell6_4.firstChild);
selection.addRange(range);
range = document.createRange();
range.selectNode(cell6_5);
selection.addRange(range);
getHTMLEditor().setBackgroundColor("#ff0000");
is(cell6_3.getAttribute("bgcolor"), "#ff0000",
'#6-3, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the second cell element in the sixth row');
ok(!cell6_4.hasAttribute("bgcolor"),
'#6-3, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the third cell element in the sixth row');
ok(!cell6_4.firstChild.hasAttribute("bgcolor"),
'#6-3, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the paragraph in the third cell element in the sixth row');
is(cell6_5.getAttribute("bgcolor"), "#ff0000",
'#6-3, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the forth cell element in the sixth row');
getHTMLEditor().setBackgroundColor("");
ok(!cell6_3.hasAttribute("bgcolor"),
'#6-3, #6-5 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the second cell element in the sixth row');
ok(!cell6_5.hasAttribute("bgcolor"),
'#6-3, #6-5 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the third cell element in the sixth row');
})();
(function test_with_selecting_3_2_and_2_1_1_and_7_2() {
let cell3_2 = editor.querySelector("#c3-2");
let cell2_1_1 = editor.querySelector("#c2-1-1");
let cell6_5 = editor.querySelector("#c6-5");
selection.removeAllRanges();
let range = document.createRange();
range.selectNode(cell3_2);
selection.addRange(range);
range = document.createRange();
range.selectNode(cell2_1_1);
selection.addRange(range);
range = document.createRange();
range.selectNode(cell6_5);
selection.addRange(range);
getHTMLEditor().setBackgroundColor("#ff0000");
is(cell3_2.getAttribute("bgcolor"), "#ff0000",
'#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the first cell element in the third row');
is(cell2_1_1.getAttribute("bgcolor"), "#ff0000",
'#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the cell element in the child table');
ok(!cell2_1_1.parentNode.hasAttribute("bgcolor"),
'#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the row in the child table');
ok(!cell2_1_1.parentNode.parentNode.hasAttribute("bgcolor"),
'#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the tbody in the child table');
ok(!cell2_1_1.parentNode.parentNode.parentNode.hasAttribute("bgcolor"),
'#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the table in the child table');
ok(!cell2_1_1.parentNode.parentNode.parentNode.parentNode.hasAttribute("bgcolor"),
'#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should not set "bgcolor" attribute of the cell having the child table');
is(cell6_5.getAttribute("bgcolor"), "#ff0000",
'#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("#ff0000") should set "bgcolor" attribute of the forth cell element in the sixth row');
getHTMLEditor().setBackgroundColor("");
ok(!cell3_2.hasAttribute("bgcolor"),
'#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the first cell element in the third row');
ok(!cell2_1_1.hasAttribute("bgcolor"),
'#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the cell element in the child table');
ok(!cell6_5.hasAttribute("bgcolor"),
'#3-2, #2-1-1, #6-5 nsIHTMLEditor.setBackgroundColor("") should remove "bgcolor" attribute of the third cell element in the sixth row');
})();
})();
})();
SimpleTest.finish();
});
function getHTMLEditor() {
var Ci = SpecialPowers.Ci;
var editingSession = SpecialPowers.wrap(window).docShell.editingSession;
return editingSession.getEditorForWindow(window).QueryInterface(Ci.nsIHTMLEditor);
}
</script>
</body>
</html>