Merge pull request #6880 from penpot/elenatorro-fix-editor-crash-on-deleting-entire-selection-firefox

🐛 Handle empty paragraph on entire selected text deletion
This commit is contained in:
Aitor Moreno
2025-07-28 17:39:25 +02:00
committed by GitHub
3 changed files with 29 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) KALEIDOS INC
*/
import { isElement } from "./Element.js";
export const TAG = "DIV";
export const TYPE = "editor";
export const QUERY = `[data-itype="${TYPE}"]`;
/**
* Returns true if passed node is the editor element.
*
* @param {Node} node
* @returns {boolean}
*/
export function isEditor(node) {
if (!node) return false;
if (!isElement(node, TAG)) return false;
if (node.dataset.itype !== TYPE) return false;
return true;
}

View File

@@ -9,6 +9,7 @@
import { isInline } from "./Inline.js";
import { isLineBreak } from "./LineBreak.js";
import { isParagraph } from "./Paragraph.js";
import { isEditor } from "./Editor.js";
import { isRoot } from "./Root.js";
/**
@@ -60,5 +61,6 @@ export function getClosestTextNode(node) {
if (isInline(node)) return node.firstChild;
if (isParagraph(node)) return node.firstChild.firstChild;
if (isRoot(node)) return node.firstChild.firstChild.firstChild;
if (isEditor(node)) return node.firstChild.firstChild.firstChild.firstChild;
throw new Error("Cannot find a text node");
}

View File

@@ -1524,6 +1524,7 @@ export class SelectionController extends EventTarget {
const startNode = getClosestTextNode(this.#range.startContainer);
const endNode = getClosestTextNode(this.#range.endContainer);
const startOffset = this.#range.startOffset;
const endOffset = this.#range.endOffset;