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

Ignore ZWS when determining if at block boundary

These are just added to allow Chrome to focus an empty text node, but
they should not be considered "real" content.
This commit is contained in:
Neil Jenkins 2023-09-27 14:51:26 +10:00
parent ee01d36a0f
commit 2d37608aac

View file

@ -1,7 +1,7 @@
import { isInline, isBlock } from '../node/Category'; import { isInline, isBlock } from '../node/Category';
import { getPreviousBlock, getNextBlock } from '../node/Block'; import { getPreviousBlock, getNextBlock } from '../node/Block';
import { getNodeBeforeOffset, getNodeAfterOffset } from '../node/Node'; import { getNodeBeforeOffset, getNodeAfterOffset } from '../node/Node';
import { notWS } from '../Constants'; import { ZWS, notWS } from '../Constants';
import { isNodeContainedInRange } from './Boundaries'; import { isNodeContainedInRange } from './Boundaries';
import { TreeIterator, SHOW_ELEMENT_OR_TEXT } from '../node/TreeIterator'; import { TreeIterator, SHOW_ELEMENT_OR_TEXT } from '../node/TreeIterator';
@ -82,9 +82,12 @@ const rangeDoesStartAtBlockBoundary = (
// If in the middle or end of a text node, we're not at the boundary. // If in the middle or end of a text node, we're not at the boundary.
if (startContainer instanceof Text) { if (startContainer instanceof Text) {
if (startOffset) { const text = startContainer.data;
for (let i = startOffset; i > 0; i -= 1) {
if (text.charAt(i - 1) !== ZWS) {
return false; return false;
} }
}
nodeAfterCursor = startContainer; nodeAfterCursor = startContainer;
} else { } else {
nodeAfterCursor = getNodeAfterOffset(startContainer, startOffset); nodeAfterCursor = getNodeAfterOffset(startContainer, startOffset);
@ -121,12 +124,15 @@ const rangeDoesEndAtBlockBoundary = (range: Range, root: Element): boolean => {
let currentNode: Node; let currentNode: Node;
// If in a text node with content, and not at the end, we're not // If in a text node with content, and not at the end, we're not
// at the boundary // at the boundary. Ignore ZWS.
if (endContainer instanceof Text) { if (endContainer instanceof Text) {
const length = endContainer.data.length; const text = endContainer.data;
if (length && endOffset < length) { const length = text.length;
for (let i = endOffset; i < length; i += 1) {
if (text.charAt(i) !== ZWS) {
return false; return false;
} }
}
currentNode = endContainer; currentNode = endContainer;
} else { } else {
currentNode = getNodeBeforeOffset(endContainer, endOffset); currentNode = getNodeBeforeOffset(endContainer, endOffset);