0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 07:13:08 -05:00

Squire: Fix crash removing formatting

The fixer node may get removed when merging adjacent inlines, so move
the cleanup to before this. For safety, also properly check for non-null
nodes so we don't crash if it's still elided for some reason.
This commit is contained in:
Neil Jenkins 2023-10-09 11:52:58 +11:00
parent 311b8ee39e
commit 1ed32e3db7
2 changed files with 16 additions and 14 deletions

View file

@ -1524,7 +1524,7 @@ class Squire {
// We need a node in the selection to break the surrounding
// formatted text.
let fixer: Node | Text | undefined;
let fixer: Node | Text | null | undefined;
if (range.collapsed) {
if (cantFocusEmptyTextNodes) {
fixer = document.createTextNode(ZWS);
@ -1615,6 +1615,20 @@ class Squire {
replaceWith(el, empty(el));
});
if (cantFocusEmptyTextNodes && fixer) {
// Clean up any previous ZWS in this block. They are not needed,
// and this works around a Chrome bug where it doesn't render the
// text in some situations with multiple ZWS(!)
fixer = fixer.parentNode;
let block = fixer;
while (block && isInline(block)) {
block = block.parentNode;
}
if (block) {
removeZWS(block, fixer);
}
}
// Merge adjacent inlines:
this._getRangeAndRemoveBookmark(range);
if (fixer) {
@ -1622,18 +1636,6 @@ class Squire {
}
mergeInlines(root, range);
if (cantFocusEmptyTextNodes && fixer) {
// Clean up any previous ZWS in this block. They are not needed,
// and this works around a Chrome bug where it doesn't render the
// text in some situations with multiple ZWS(!)
fixer = fixer.parentNode!;
let block = fixer;
while (isInline(block)) {
block = block.parentNode!;
}
removeZWS(block, fixer);
}
return range;
}

View file

@ -34,7 +34,7 @@ const isLineBreak = (br: Element, isLBIfEmptyBlock: boolean): boolean => {
// contained ZWS space then remove it too. We may want to keep one ZWS node at
// the bottom of the tree so the block can be selected. Define that node as the
// keepNode.
const removeZWS = (root: Node, keepNode?: Node): void => {
const removeZWS = (root: Node, keepNode?: Node | null): void => {
const walker = new TreeIterator<Text>(root, SHOW_TEXT);
let textNode: Text | null;
let index: number;