Skip to content

Commit 591b108

Browse files
committed
imp(dom-adapter): cover case with different inputs
1 parent f8d9575 commit 591b108

File tree

1 file changed

+19
-11
lines changed
  • packages/dom-adapters/src/CaretAdapter

1 file changed

+19
-11
lines changed

packages/dom-adapters/src/CaretAdapter/index.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,22 +236,30 @@ export class CaretAdapter extends EventTarget {
236236
}
237237

238238
const selection = document.getSelection()!;
239-
240-
let absoluteStartOffset = getAbsoluteRangeOffset(input, selection.anchorNode!, selection.anchorOffset);
241-
let absoluteEndOffset = getAbsoluteRangeOffset(input, selection.focusNode!, selection.focusOffset);
239+
240+
let isStartEqualsCurrent = false;
241+
let isEndEqualsCurrent = false;
242+
243+
const start = getBoundaryPointByAbsoluteOffset(input, textRange[0]);
244+
const end = getBoundaryPointByAbsoluteOffset(input, textRange[1]);
242245

243246
/**
244-
* For right-to-left selection, we need to swap start and end offsets to compare with model range
247+
* If selection is outside of the input, it is different from the model range
245248
*/
246-
if (absoluteStartOffset > absoluteEndOffset) {
247-
[absoluteStartOffset, absoluteEndOffset] = [absoluteEndOffset, absoluteStartOffset];
248-
}
249+
if (input.contains(selection.anchorNode!) && input.contains(selection.focusNode!)) {
250+
let absoluteStartOffset = getAbsoluteRangeOffset(input, selection.anchorNode!, selection.anchorOffset);
251+
let absoluteEndOffset = getAbsoluteRangeOffset(input, selection.focusNode!, selection.focusOffset);
249252

250-
const start = getBoundaryPointByAbsoluteOffset(input, textRange[0]);
251-
const end = getBoundaryPointByAbsoluteOffset(input, textRange[1]);
253+
/**
254+
* For right-to-left selection, we need to swap start and end offsets to compare with model range
255+
*/
256+
if (absoluteStartOffset > absoluteEndOffset) {
257+
[absoluteStartOffset, absoluteEndOffset] = [absoluteEndOffset, absoluteStartOffset];
258+
}
252259

253-
const isStartEqualsCurrent = textRange[0] === absoluteStartOffset;
254-
const isEndEqualsCurrent = textRange[1] === absoluteEndOffset;
260+
isStartEqualsCurrent = textRange[0] === absoluteStartOffset;
261+
isEndEqualsCurrent = textRange[1] === absoluteEndOffset;
262+
}
255263

256264
/**
257265
* If selection is already the same, we don't need to update it to not interrupt browser's behaviour

0 commit comments

Comments
 (0)