Bug 1671556 - part 3: Make `HTMLEditor::SetHTMLBackgroundColorWithTransaction()` stop using `HTMLEditor::Get(First|Next)SelectedTableCellElement()` r=m_kato

Differential Revision: https://phabricator.services.mozilla.com/D94229
This commit is contained in:
Masayuki Nakano 2020-10-24 04:12:51 +00:00
Родитель d57d81d139
Коммит 8165ffcad7
3 изменённых файлов: 207 добавлений и 11 удалений

Просмотреть файл

@ -3032,34 +3032,42 @@ nsresult HTMLEditor::SetHTMLBackgroundColorWithTransaction(
// odd, though.
if (isCellSelected || rootElementOfBackgroundColor->IsAnyOfHTMLElements(
nsGkAtoms::table, nsGkAtoms::tr)) {
IgnoredErrorResult ignoredError;
RefPtr<Element> cellElement =
GetFirstSelectedTableCellElement(ignoredError);
if (cellElement) {
SelectedTableCellScanner scanner(*SelectionRefPtr());
if (scanner.IsInTableCellSelectionMode()) {
if (setColor) {
while (cellElement) {
for (const OwningNonNull<Element>& cellElement :
scanner.ElementsRef()) {
// `MOZ_KnownLive(cellElement)` is safe because of `scanner`
// is stack only class and keeps grabbing it until it's destroyed.
nsresult rv = SetAttributeWithTransaction(
*cellElement, *nsGkAtoms::bgcolor, aColor);
MOZ_KnownLive(cellElement), *nsGkAtoms::bgcolor, aColor);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (NS_FAILED(rv)) {
NS_WARNING(
"EditorBase::::SetAttributeWithTransaction(nsGkAtoms::"
"bgcolor) failed");
return rv;
}
cellElement = GetNextSelectedTableCellElement(ignoredError);
}
return NS_OK;
}
while (cellElement) {
nsresult rv =
RemoveAttributeWithTransaction(*cellElement, *nsGkAtoms::bgcolor);
for (const OwningNonNull<Element>& cellElement :
scanner.ElementsRef()) {
// `MOZ_KnownLive(cellElement)` is safe because of `scanner`
// is stack only class and keeps grabbing it until it's destroyed.
nsresult rv = RemoveAttributeWithTransaction(
MOZ_KnownLive(cellElement), *nsGkAtoms::bgcolor);
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (NS_FAILED(rv)) {
NS_WARNING(
"EditorBase::RemoveAttributeWithTransaction(nsGkAtoms::bgcolor)"
" failed");
return rv;
}
cellElement = GetNextSelectedTableCellElement(ignoredError);
}
return NS_OK;
}

Просмотреть файл

@ -270,6 +270,7 @@ skip-if = headless
[test_nsIHTMLEditor_getSelectedElement.html]
[test_nsIHTMLEditor_removeInlineProperty.html]
[test_nsIHTMLEditor_selectElement.html]
[test_nsIHTMLEditor_setBackgroundColor.html]
[test_nsIHTMLEditor_setCaretAfterElement.html]
[test_nsIHTMLObjectResizer_hideResizers.html]
[test_nsITableEditor_deleteTableCell.html]

Просмотреть файл

@ -0,0 +1,187 @@
<!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>