Cleanup some squire code

This commit is contained in:
the-djmaze 2023-08-28 12:05:02 +02:00
parent e9770e17f6
commit 6b42620ad3

View file

@ -220,52 +220,36 @@ const
},
fixCursor = (node, root) => {
// In Webkit and Gecko, block level elements are collapsed and
// unfocusable if they have no content (:empty). To remedy this, a <BR> must be
// inserted. In Opera and IE, we just need a textnode in order for the
// cursor to appear.
let self = root.__squire__;
let originalNode = node;
let fixer, child;
if (node === root) {
if (!(child = node.firstChild) || child.nodeName === 'BR') {
fixer = self.createDefaultBlock();
if (child) {
child.replaceWith(fixer);
let fixer = null;
if (node instanceof Text) {
return node;
}
else {
node.append(fixer);
}
node = fixer;
fixer = null;
}
}
if (node.nodeType === TEXT_NODE) {
return originalNode;
}
if (isInline(node)) {
let child = node.firstChild;
if (cantFocusEmptyTextNodes) {
while (child && child instanceof Text && !child.data) {
node.removeChild(child);
child = node.firstChild;
while (isWebKit && child?.nodeType === TEXT_NODE && !child.data) {
child.remove();
child = node.firstChild;
}
}
if (!child) {
fixer = self._addZWS();
if (cantFocusEmptyTextNodes) {
fixer = document.createTextNode(ZWS);
} else {
fixer = document.createTextNode("");
}
// } else if (!node.querySelector('BR')) {
// } else if (!node.innerText.trim().length) {
} else if (node.matches(':empty')) {
fixer = createElement('BR');
while ((child = node.lastElementChild) && !isInline(child)) {
node = child;
}
} else if (node instanceof Element && !node.querySelector("BR")) {
fixer = createElement("BR");
let parent = node;
let child;
while ((child = parent.lastElementChild) && !isInline(child)) {
parent = child;
}
}
if (fixer) {
try {
node.append(fixer);
node.appendChild(fixer);
} catch (error) {
didError({
name: 'Squire: fixCursor ' + error,
@ -274,8 +258,7 @@ const
});
}
}
return originalNode;
return node;
},
// Recursively examine container nodes and wrap any inline children.
@ -959,10 +942,7 @@ const
let startBlock = getStartBlockOfRange(range, root);
let nodeAfterCursor;
if (!startBlock) {
return false;
}
if (startBlock) {
// If in the middle or end of a text node, we're not at the boundary.
if (startContainer.nodeType === TEXT_NODE) {
if (startOffset) {
@ -971,14 +951,10 @@ const
nodeAfterCursor = startContainer;
} else {
nodeAfterCursor = getNodeAfter(startContainer, startOffset);
if (nodeAfterCursor && !root.contains(nodeAfterCursor)) {
nodeAfterCursor = null;
}
// The cursor was right at the end of the document
if (!nodeAfterCursor) {
if (!nodeAfterCursor || !root.contains(nodeAfterCursor)) {
nodeAfterCursor = getNodeBefore(startContainer, startOffset);
if (nodeAfterCursor.nodeType === TEXT_NODE &&
nodeAfterCursor.length) {
if (nodeAfterCursor.nodeType === TEXT_NODE && nodeAfterCursor.length) {
return false;
}
}
@ -989,6 +965,7 @@ const
contentWalker.currentNode = nodeAfterCursor;
return !contentWalker.previousNode();
}
},
rangeDoesEndAtBlockBoundary = (range, root) => {