Bug 1764895 - part 2: Make comm-central stop using `nsIEditor.setShouldTxnSetSelection` r=mkmelin

It'll be removed and `nsIEditor.insertNode()` and `nsIEditor.deleteNode()`
have new optional parameter which if you set to true, selection won't be
modified by the editor (except `beforeinput` and `input` event listeners).

Differential Revision: https://phabricator.services.mozilla.com/D196006

--HG--
extra : amend_source : 4ac61cde6cdb49db4b77406c55442b762faee442
This commit is contained in:
Masayuki Nakano 2023-12-11 05:36:02 +00:00
Родитель 227e141c49
Коммит 61ef12c3aa
10 изменённых файлов: 70 добавлений и 55 удалений

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

@ -253,9 +253,7 @@ function onAccept(event) {
if (gImageMap && gInsertNewIMap) {
// Insert the ImageMap element at beginning of document
var body = editor.rootElement;
editor.setShouldTxnSetSelection(false);
editor.insertNode(gImageMap, body, 0);
editor.setShouldTxnSetSelection(true);
editor.insertNode(gImageMap, body, 0, true /* preserve selection */);
}
} catch (e) {
dump(e);

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

@ -302,9 +302,12 @@ function onAccept(event) {
// Insert the anchor into the document,
// but don't let the transaction change the selection
gActiveEditor.setShouldTxnSetSelection(false);
gActiveEditor.insertNode(anchorNode, gHNodeArray[href], 0);
gActiveEditor.setShouldTxnSetSelection(true);
gActiveEditor.insertNode(
anchorNode,
gHNodeArray[href],
0,
true /* preserve selection */
);
}
}
gActiveEditor.endTransaction();

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

@ -520,8 +520,9 @@ function PrependHeadElement(element) {
var editor = GetCurrentEditor();
try {
// Use editor's undoable transaction
// XXX Here tried to prevent updating Selection with unknown 4th argument,
// but nsIEditor.setShouldTxnSetSelection is not used for that.
// XXX Here tried to prevent updating Selection with unknown 4th argument
// before bug 1764895, but nsIEditor.setShouldTxnSetSelection was not
// used for that.
editor.insertNode(element, head, 0);
} catch (e) {}
}
@ -538,8 +539,9 @@ function AppendHeadElement(element) {
var editor = GetCurrentEditor();
try {
// Use editor's undoable transaction
// XXX Here tried to prevent updating Selection with unknown 4th argument,
// but nsIEditor.setShouldTxnSetSelection is not used for that.
// XXX Here tried to prevent updating Selection with unknown 4th argument
// before bug 1764895, but nsIEditor.setShouldTxnSetSelection was not
// used for that.
editor.insertNode(element, head, position);
} catch (e) {}
}
@ -768,26 +770,26 @@ function InsertElementAroundSelection(element) {
}
// Now insert the node
// XXX Here tried to prevent updating Selection with unknown 4th argument,
// but nsIEditor.setShouldTxnSetSelection is not used for that.
// XXX Here tried to prevent updating Selection with unknown 4th argument
// before bug 1764895, but nsIEditor.setShouldTxnSetSelection was not
// used for that.
editor.insertNode(element, range.commonAncestorContainer, offset);
offset = element.childNodes.length;
if (!editor.nodeIsBlock(element)) {
editor.setShouldTxnSetSelection(false);
}
// Move all the old child nodes to the element
const preserveSelection = !editor.nodeIsBlock(element);
var empty = true;
while (start != end) {
var next = start.nextSibling;
editor.deleteNode(start);
editor.insertNode(start, element, element.childNodes.length);
editor.deleteNode(start, preserveSelection);
editor.insertNode(start, element, element.childNodes.length, preserveSelection);
empty = false;
start = next;
}
if (!editor.nodeIsBlock(element)) {
editor.setShouldTxnSetSelection(true);
} else {
// FYI: nsIHTMLEditor.nodeIsBlock may return different value if it's moved
// or removed from the old position or the style has been changed.
// Therefore, the result may be different from `!preserveSelection`.
if (editor.nodeIsBlock(element)) {
// Also move a trailing <br>
if (start && start.localName == "br") {
editor.deleteNode(start);

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

@ -167,26 +167,31 @@ function onAccept() {
}
InsertElementAroundSelection(fieldsetElement);
} else if (gDialog.editText.checked) {
editor.setShouldTxnSetSelection(false);
if (gDialog.legendText.value) {
if (newLegend) {
editor.insertNode(legendElement, fieldsetElement, 0);
editor.insertNode(
legendElement,
fieldsetElement,
0,
true /* preserve selection */
);
} else {
while (legendElement.firstChild) {
editor.deleteNode(legendElement.lastChild);
editor.deleteNode(
legendElement.lastChild,
true /* preserve selection */
);
}
}
editor.insertNode(
editor.document.createTextNode(gDialog.legendText.value),
legendElement,
0
0,
true /* preserve selection */
);
} else if (!newLegend) {
editor.deleteNode(legendElement);
editor.deleteNode(legendElement, true /* preserve selection */);
}
editor.setShouldTxnSetSelection(true);
}
} finally {
editor.endTransaction();

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

@ -258,9 +258,7 @@ function onAccept(event) {
if (gImageMap && gInsertNewIMap) {
// Insert the ImageMap element at beginning of document
var body = editor.rootElement;
editor.setShouldTxnSetSelection(false);
editor.insertNode(gImageMap, body, 0);
editor.setShouldTxnSetSelection(true);
editor.insertNode(gImageMap, body, 0, true /* preserve selection */);
}
} catch (e) {
dump(e);

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

@ -173,9 +173,7 @@ function onAccept(event) {
if (gImageMap && gInsertNewIMap) {
// Insert the ImageMap element at beginning of document
var body = editor.rootElement;
editor.setShouldTxnSetSelection(false);
editor.insertNode(gImageMap, body, 0);
editor.setShouldTxnSetSelection(true);
editor.insertNode(gImageMap, body, 0, true /* preserve selection */);
}
} catch (e) {}

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

@ -93,20 +93,20 @@ function onAccept() {
try {
if (gDialog.editText.checked) {
editor.setShouldTxnSetSelection(false);
while (labelElement.firstChild) {
editor.deleteNode(labelElement.firstChild);
editor.deleteNode(
labelElement.firstChild,
true /* preserve selection */
);
}
if (gDialog.labelText.value) {
editor.insertNode(
editor.document.createTextNode(gDialog.labelText.value),
labelElement,
0
0,
true /* preserve selection */
);
}
editor.setShouldTxnSetSelection(true);
}
editor.cloneAttributes(labelElement, globalElement);

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

@ -314,9 +314,12 @@ function onAccept(event) {
// Insert the anchor into the document,
// but don't let the transaction change the selection
gActiveEditor.setShouldTxnSetSelection(false);
gActiveEditor.insertNode(anchorNode, gHNodeArray[href], 0);
gActiveEditor.setShouldTxnSetSelection(true);
gActiveEditor.insertNode(
anchorNode,
gHNodeArray[href],
0,
true /* preserve selection */
);
}
}
gActiveEditor.endTransaction();

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

@ -617,10 +617,11 @@ function onAccept() {
editor.insertElementAtSelection(selectElement, true);
}
editor.setShouldTxnSetSelection(false);
while (selectElement.lastChild) {
editor.deleteNode(selectElement.lastChild);
editor.deleteNode(
selectElement.lastChild,
true /* preserver selection */
);
}
var offset = 0;
@ -628,11 +629,14 @@ function onAccept() {
if (itemArray[i].level > 1) {
selectElement.lastChild.appendChild(itemArray[i].element);
} else {
editor.insertNode(itemArray[i].element, selectElement, offset++);
editor.insertNode(
itemArray[i].element,
selectElement,
offset++,
true /* preserve selection */
);
}
}
editor.setShouldTxnSetSelection(true);
} finally {
editor.endTransaction();
}

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

@ -151,17 +151,21 @@ function onAccept() {
// undoably set value
var initialText = gDialog.textareaValue.value;
if (initialText != textareaElement.value) {
editor.setShouldTxnSetSelection(false);
while (textareaElement.hasChildNodes()) {
editor.deleteNode(textareaElement.lastChild);
editor.deleteNode(
textareaElement.lastChild,
true /* preserve selection */
);
}
if (initialText) {
var textNode = editor.document.createTextNode(initialText);
editor.insertNode(textNode, textareaElement, 0);
editor.insertNode(
textNode,
textareaElement,
0,
true /* preserve selection */
);
}
editor.setShouldTxnSetSelection(true);
}
} finally {
editor.endTransaction();