From bc6252b1a43a732ebf14824f9c14bcf7be91564e Mon Sep 17 00:00:00 2001 From: Joe Eli McIlvain Date: Thu, 31 Mar 2022 12:41:18 -0700 Subject: [PATCH] Fix edge case for zero-length decoration in empty leaf. This change ensures that a zero-length decoration is never treated as encompassing the entire leaf, handling an edge case where an empty decoration inside an empty leaf was being treated as encompassing the entire leaf and thus preventing the "before" and "after" leaves we would otherwise see (when a zero-length decoration is applied to any non-empty leaf). This fixes issues encountered in the `slate-page-decorations` library for the edge case of an empty paragraph node being located on the edge of a page boundary. --- packages/slate/src/interfaces/text.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/slate/src/interfaces/text.ts b/packages/slate/src/interfaces/text.ts index a7a8a490eb..8221ee3672 100644 --- a/packages/slate/src/interfaces/text.ts +++ b/packages/slate/src/interfaces/text.ts @@ -113,8 +113,12 @@ export const Text: TextInterface = { const offset = o o += length - // If the range encompases the entire leaf, add the range. - if (start.offset <= offset && end.offset >= o) { + // If the range encompases the entire leaf (and the range is non-empty), add the range. + if ( + start.offset <= offset && + end.offset >= o && + start.offset < end.offset + ) { Object.assign(leaf, rest) next.push(leaf) continue