-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathselection.ts
1034 lines (921 loc) · 38.7 KB
/
selection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==LICENSE-BEGIN==
// Copyright 2017 European Digital Reading Lab. All rights reserved.
// Licensed to the Readium Foundation under one or more contributor license agreements.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==
// import { convertRangeToTextFragment } from "./textFragment";
import { IRangeInfo, ISelectedTextInfo, ISelectionInfo } from "../../common/selection"; // TextFragment
import { ReadiumElectronWebviewWindow } from "./state";
import { ipcRenderer } from "electron";
import { R2_EVENT_READING_LOCATION_CLEAR_SELECTION } from "../../common/events";
const IS_DEV = (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "dev");
// https://developer.mozilla.org/en-US/docs/Web/API/Selection
// const win = global.window as ReadiumElectronWebviewWindow;
function dumpDebug(
msg: string,
startNode: Node, startOffset: number,
endNode: Node, endOffset: number,
getCssSelector: (element: Element) => string,
) {
console.log("$$$$$$$$$$$$$$$$$ " + msg);
console.log("**** START");
console.log("Node type (1=element, 3=text): " + startNode.nodeType);
if (startNode.nodeType === Node.ELEMENT_NODE) {
console.log("CSS Selector: " + getCssSelector(startNode as Element));
console.log("Element children count: " + startNode.childNodes.length);
if (startOffset >= 0 && startOffset < startNode.childNodes.length) {
console.log("Child node type (1=element, 3=text): " + startNode.childNodes[startOffset].nodeType);
if (startNode.childNodes[endOffset].nodeType === Node.ELEMENT_NODE) {
console.log("Child CSS Selector: " + getCssSelector(startNode.childNodes[endOffset] as Element));
}
} else {
console.log("startOffset >= 0 && startOffset < startNode.childNodes.length ... " +
startOffset + " // " + startNode.childNodes.length);
}
}
if (startNode.parentNode && startNode.parentNode.nodeType === Node.ELEMENT_NODE) {
console.log("- Parent CSS Selector: " + getCssSelector(startNode.parentNode as Element));
console.log("- Parent element children count: " + startNode.parentNode.childNodes.length);
}
console.log("Offset: " + startOffset);
console.log("**** END");
console.log("Node type (1=element, 3=text): " + endNode.nodeType);
if (endNode.nodeType === Node.ELEMENT_NODE) {
console.log("CSS Selector: " + getCssSelector(endNode as Element));
console.log("Element children count: " + endNode.childNodes.length);
if (endOffset >= 0 && endOffset < endNode.childNodes.length) {
console.log("Child node type (1=element, 3=text): " + endNode.childNodes[endOffset].nodeType);
if (endNode.childNodes[endOffset].nodeType === Node.ELEMENT_NODE) {
console.log("Child CSS Selector: " + getCssSelector(endNode.childNodes[endOffset] as Element));
}
} else {
console.log("endOffset >= 0 && endOffset < endNode.childNodes.length ... " +
endOffset + " // " + endNode.childNodes.length);
}
}
if (endNode.parentNode && endNode.parentNode.nodeType === Node.ELEMENT_NODE) {
console.log("- Parent CSS Selector: " + getCssSelector(endNode.parentNode as Element));
console.log("- Parent element children count: " + endNode.parentNode.childNodes.length);
}
console.log("Offset: " + endOffset);
console.log("$$$$$$$$$$$$$$$$$");
}
let _selectionChangeTimeout: number | undefined = undefined;
let _ignoreSelectionChangeEvent: boolean = false;
export const setSelectionChangeAction = (win: ReadiumElectronWebviewWindow, func: () => void) => {
win.document?.addEventListener("selectionchange", (_ev: Event) => {
if (_selectionChangeTimeout !== undefined) {
win.clearTimeout(_selectionChangeTimeout);
_selectionChangeTimeout = undefined;
}
if (_ignoreSelectionChangeEvent) {
// console.log("############ selectionchange SKIP");
_ignoreSelectionChangeEvent = false;
return;
}
// console.log("############ selectionchange OK");
func();
});
};
export function clearCurrentSelection(win: ReadiumElectronWebviewWindow) {
const selection = win.getSelection();
if (!selection) {
return;
}
// if (!selection.isCollapsed) {
// _ignoreSelectionChangeEvent = true;
// }
_ignoreSelectionChangeEvent = true;
_selectionChangeTimeout = win.setTimeout(() => {
_selectionChangeTimeout = undefined;
// console.log("############ selectionchange TIMEOUT");
_ignoreSelectionChangeEvent = false;
}, 200);
selection.removeAllRanges();
// selection.empty();
// selection.collapseToStart();
if (win.READIUM2.locationHashOverrideInfo?.selectionInfo) {
win.READIUM2.locationHashOverrideInfo.selectionInfo = undefined;
}
ipcRenderer.sendToHost(R2_EVENT_READING_LOCATION_CLEAR_SELECTION);
}
export const collapseWhitespaces = (str: string) => {
return str.replace(/[\r\n]/g, " ").replace(/\s\s+/g, " ");
};
export const cleanupStr = (str: string) => {
return collapseWhitespaces(str).trim();
};
export function getCurrentSelectionInfo(
win: ReadiumElectronWebviewWindow,
getCssSelector: (element: Element) => string,
computeElementCFI: (node: Node) => string | undefined,
computeElementXPath: (node: Node) => string | undefined,
):
ISelectionInfo | undefined {
const selection = win.getSelection();
if (!selection) {
return undefined;
}
if (selection.isCollapsed) {
console.log("^^^ SELECTION COLLAPSED.");
return undefined;
}
// rawText is in fact already clean! (Selection API does it)
const rawText = selection.toString();
const cleanText = collapseWhitespaces(rawText);
if (cleanText.length === 0) {
console.log("^^^ SELECTION TEXT EMPTY.");
return undefined;
}
if (!selection.anchorNode || !selection.focusNode) {
return undefined;
}
const r = selection.rangeCount === 1 ? selection.getRangeAt(0) :
createOrderedRange(selection.anchorNode, selection.anchorOffset, selection.focusNode, selection.focusOffset);
if (!r || r.collapsed) {
console.log("$$$$$$$$$$$$$$$$$ CANNOT GET NON-COLLAPSED SELECTION RANGE?!");
return undefined;
}
const range = normalizeRange(r);
if (range.collapsed) {
console.log("$$$$$$$$$$$$$$$$$ RANGE COLLAPSED AFTER NORMALISE?!");
return undefined;
}
if (IS_DEV) {
if (range.startContainer !== r.startContainer) {
console.log(">>>>>>>>>>>>>>>>>>>>>>> SELECTION RANGE NORMALIZE diff: startContainer");
console.log(range.startContainer);
console.log(r.startContainer);
}
if (range.startOffset !== r.startOffset) {
console.log(">>>>>>>>>>>>>>>>>>>>>>> SELECTION RANGE NORMALIZE diff: startOffset");
console.log(`${range.startOffset} !== ${r.startOffset}`);
}
if (range.endContainer !== r.endContainer) {
console.log(">>>>>>>>>>>>>>>>>>>>>>> SELECTION RANGE NORMALIZE diff: endContainer");
console.log(range.endContainer);
console.log(r.endContainer);
}
if (range.endOffset !== r.endOffset) {
console.log(">>>>>>>>>>>>>>>>>>>>>>> SELECTION RANGE NORMALIZE diff: endOffset");
console.log(`${range.endOffset} !== ${r.endOffset}`);
}
}
const tuple = convertRange(range, getCssSelector, computeElementCFI, computeElementXPath);
if (!tuple) {
console.log("^^^ SELECTION RANGE INFO FAIL?!");
return undefined;
}
const rangeInfo = tuple[0];
const textInfo = tuple[1];
if (IS_DEV) {
if (textInfo.cleanText !== cleanText) {
console.log(">>>>>>>>>>>>>>>>>>>>>>> SELECTION TEXT INFO diff: cleanText");
console.log(`${textInfo.cleanText} !== ${cleanText}`);
}
if (textInfo.rawText !== rawText) {
console.log(">>>>>>>>>>>>>>>>>>>>>>> SELECTION TEXT INFO diff: rawText");
console.log(`${textInfo.rawText} !== ${rawText}`);
}
}
// selection.removeAllRanges();
if (IS_DEV && win.READIUM2.DEBUG_VISUALS) {
const restoredRange = convertRangeInfo(win.document, rangeInfo);
if (restoredRange) {
if (restoredRange.startOffset === range.startOffset &&
restoredRange.endOffset === range.endOffset &&
restoredRange.startContainer === range.startContainer &&
restoredRange.endContainer === range.endContainer) {
console.log("SELECTION RANGE RESTORED OKAY (dev check).");
} else {
console.log("SELECTION RANGE RESTORE FAIL (dev check).");
// tslint:disable-next-line:max-line-length
dumpDebug("SELECTION", selection.anchorNode, selection.anchorOffset, selection.focusNode, selection.focusOffset, getCssSelector);
// tslint:disable-next-line:max-line-length
dumpDebug("ORDERED RANGE FROM SELECTION", range.startContainer, range.startOffset, range.endContainer, range.endOffset, getCssSelector);
// tslint:disable-next-line:max-line-length
dumpDebug("RESTORED RANGE", restoredRange.startContainer, restoredRange.startOffset, restoredRange.endContainer, restoredRange.endOffset, getCssSelector);
}
// setTimeout(() => {
// selection.addRange(restoredRange);
// }, 500);
} else {
console.log("CANNOT RESTORE SELECTION RANGE ??!");
// setTimeout(() => {
// selection.addRange(range);
// }, 500);
}
} else {
// selection.addRange(range);
}
// let textFragment: TextFragment | undefined;
// try {
// textFragment = convertRangeToTextFragment(range);
// } catch (err) {
// console.log("!?convertRangeToTextFragment!?");
// console.log(err);
// }
return {
textFragment: undefined,
rangeInfo,
cleanBefore: textInfo.cleanBefore,
cleanText: textInfo.cleanText,
cleanAfter: textInfo.cleanAfter,
rawBefore: textInfo.rawBefore,
rawText: textInfo.rawText,
rawAfter: textInfo.rawAfter,
};
}
export function createOrderedRange(startNode: Node, startOffset: number, endNode: Node, endOffset: number):
Range | undefined {
const range = new Range(); // document.createRange()
range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);
if (!range.collapsed) {
// console.log(">>> createOrderedRange RANGE OK");
return range;
}
console.log(">>> createOrderedRange COLLAPSED ... RANGE REVERSE?");
const rangeReverse = new Range(); // document.createRange()
rangeReverse.setStart(endNode, endOffset);
rangeReverse.setEnd(startNode, startOffset);
if (!rangeReverse.collapsed) {
console.log(">>> createOrderedRange RANGE REVERSE OK.");
return range;
}
console.log(">>> createOrderedRange RANGE REVERSE ALSO COLLAPSED?!");
return undefined;
// let startNode = startNode_;
// let startOffset = startOffset_;
// if (startNode.nodeType === Node.ELEMENT_NODE) {
// if (startOffset >= 0 && startOffset < startNode.childNodes.length) {
// startNode = startNode.childNodes[startOffset];
// }
// startOffset = -1;
// }
// let endNode = endNode_;
// let endOffset = endOffset_;
// if (endNode.nodeType === Node.ELEMENT_NODE) {
// if (endOffset >= 0 && endOffset < endNode.childNodes.length) {
// endNode = endNode.childNodes[endOffset];
// }
// endOffset = -1;
// }
// const position = startNode.compareDocumentPosition(endNode);
// const reverse1 = position === 0 && startOffset > endOffset;
// // tslint:disable-next-line:no-bitwise
// const reverse2 = position & Node.DOCUMENT_POSITION_PRECEDING;
// const reverse = reverse1 || reverse2;
// console.log("{{{{{{{{{{{ reverse: " + reverse + " (" + reverse1 + " // " + reverse2 + ")");
// const range = new Range(); // document.createRange()
// if (startOffset >= 0 && endOffset >= 0) {
// range.setStart(reverse ? endNode : startNode, reverse ? endOffset : startOffset);
// range.setEnd(reverse ? startNode : endNode, reverse ? startOffset : endOffset);
// } else {
// if (reverse) {
// if (endOffset < 0) {
// range.setStartAfter(endNode);
// } else {
// range.setStart(endNode, endOffset);
// }
// if (startOffset < 0) {
// range.setEndBefore(startNode);
// } else {
// range.setEnd(startNode, startOffset);
// }
// } else {
// if (startOffset < 0) {
// range.setStartAfter(startNode);
// } else {
// range.setStart(startNode, startOffset);
// }
// if (endOffset < 0) {
// range.setEndBefore(endNode);
// } else {
// range.setEnd(endNode, endOffset);
// }
// }
// }
// return range;
}
export function convertRange(
range: Range,
getCssSelector: (element: Element) => string,
computeElementCFI: (node: Node) => string | undefined,
computeElementXPath: (node: Node) => string | undefined,
):
[IRangeInfo, ISelectedTextInfo] | undefined {
// -----------------
const startIsElement = range.startContainer.nodeType === Node.ELEMENT_NODE;
const startContainerElement = startIsElement ?
range.startContainer as Element :
((range.startContainer.parentNode && range.startContainer.parentNode.nodeType === Node.ELEMENT_NODE) ?
range.startContainer.parentNode as Element : undefined);
if (!startContainerElement) {
return undefined;
}
const startContainerChildTextNodeIndex = startIsElement ? -1 :
Array.from(startContainerElement.childNodes).indexOf(range.startContainer as ChildNode);
if (startContainerChildTextNodeIndex < -1) {
return undefined;
}
const startContainerElementCssSelector = getCssSelector(startContainerElement);
// -----------------
const endIsElement = range.endContainer.nodeType === Node.ELEMENT_NODE;
const endContainerElement = endIsElement ?
range.endContainer as Element :
((range.endContainer.parentNode && range.endContainer.parentNode.nodeType === Node.ELEMENT_NODE) ?
range.endContainer.parentNode as Element : undefined);
if (!endContainerElement) {
return undefined;
}
const endContainerChildTextNodeIndex = endIsElement ? -1 :
Array.from(endContainerElement.childNodes).indexOf(range.endContainer as ChildNode);
if (endContainerChildTextNodeIndex < -1) {
return undefined;
}
const endContainerElementCssSelector = getCssSelector(endContainerElement);
// -----------------
const commonElementAncestor = getCommonAncestorElement(range.startContainer, range.endContainer);
if (!commonElementAncestor) {
console.log("^^^ NO RANGE COMMON ANCESTOR?!");
return undefined;
}
if (range.commonAncestorContainer) {
const rangeCommonAncestorElement = range.commonAncestorContainer.nodeType === Node.ELEMENT_NODE ?
range.commonAncestorContainer : range.commonAncestorContainer.parentNode;
if (rangeCommonAncestorElement && rangeCommonAncestorElement.nodeType === Node.ELEMENT_NODE) {
if (commonElementAncestor !== rangeCommonAncestorElement) {
console.log(">>>>>> COMMON ANCESTOR CONTAINER DIFF??!");
console.log(getCssSelector(commonElementAncestor));
console.log(getCssSelector(rangeCommonAncestorElement as Element));
}
}
}
// -----------------
const SELECTION_BEFORE_AFTER_TEXT_LENGTH = 30;
let rawBefore = "";
const rawText = range.toString();
let rawAfter = "";
let cleanBefore = "";
const cleanText = collapseWhitespaces(rawText);
let cleanAfter = "";
let currentParent = commonElementAncestor;
while (currentParent) {
if (currentParent.tagName?.toLowerCase() === "html") {
break;
}
const beforeNeedsToGrow = cleanBefore.length < SELECTION_BEFORE_AFTER_TEXT_LENGTH;
const afterNeedsToGrow = cleanAfter.length < SELECTION_BEFORE_AFTER_TEXT_LENGTH;
if (!beforeNeedsToGrow && !afterNeedsToGrow) {
break;
}
if (beforeNeedsToGrow) {
try {
const rangeBefore = new Range(); // commonElementAncestor.ownerDocument.createRange()
rangeBefore.setStartBefore(currentParent);
// rangeBefore.setStart(currentParent, 0);
// rangeBefore.setEndBefore(range.startContainer);
rangeBefore.setEnd(range.startContainer, range.startOffset);
rawBefore = rangeBefore.toString();
cleanBefore = collapseWhitespaces(rawBefore);
if (cleanBefore.length > SELECTION_BEFORE_AFTER_TEXT_LENGTH) {
cleanBefore = cleanBefore.substring(cleanBefore.length - SELECTION_BEFORE_AFTER_TEXT_LENGTH, cleanBefore.length);
}
} catch (ex1) {
console.log(ex1);
}
}
if (afterNeedsToGrow) {
try {
const rangeAfter = new Range(); // commonElementAncestor.ownerDocument.createRange()
rangeAfter.setStart(range.endContainer, range.endOffset);
rangeAfter.setEndAfter(currentParent);
rawAfter = rangeAfter.toString();
cleanAfter = collapseWhitespaces(rawAfter);
if (cleanAfter.length > SELECTION_BEFORE_AFTER_TEXT_LENGTH) {
cleanAfter = cleanAfter.substring(0, SELECTION_BEFORE_AFTER_TEXT_LENGTH);
}
} catch (ex2) {
console.log(ex2);
}
}
if (currentParent.tagName?.toLowerCase() === "body") {
break;
}
currentParent = currentParent.parentNode as Element;
}
if (cleanBefore.length) {
let j = 0;
let i = rawBefore.length - 1;
let wasWhiteSpace = false;
for (; i >= 0; i--) {
const isWhiteSpace = /[\r\n\s]/.test(rawBefore[i]);
if (isWhiteSpace && i !== 0 && i !== rawBefore.length - 1 && wasWhiteSpace) {
wasWhiteSpace = isWhiteSpace;
continue;
}
wasWhiteSpace = isWhiteSpace;
j++;
if (j >= cleanBefore.length) {
break;
}
}
rawBefore = rawBefore.substring(i, rawBefore.length);
}
if (cleanAfter.length) {
let j = 0;
let i = 0;
let wasWhiteSpace = false;
for (; i < rawAfter.length; i++) {
const isWhiteSpace = /[\r\n\s]/.test(rawAfter[i]);
if (isWhiteSpace && i !== 0 && i !== rawAfter.length - 1 && wasWhiteSpace) {
wasWhiteSpace = isWhiteSpace;
continue;
}
wasWhiteSpace = isWhiteSpace;
j++;
if (j >= cleanAfter.length) {
break;
}
}
rawAfter = rawAfter.substring(0, i + 1);
}
// -----------------
const rootElementCfi = computeElementCFI(commonElementAncestor);
// console.log(`ROOT CFI: ${rootElementCfi}`);
// console.log(commonElementAncestor.outerHTML);
const startElementCfi = computeElementCFI(startContainerElement);
// console.log(`START CFI: ${startElementCfi}`);
// console.log(startContainerElement.outerHTML);
const startElementXPath = computeElementXPath(startContainerElement);
const endElementCfi = computeElementCFI(endContainerElement);
// console.log(`END CFI: ${endElementCfi}`);
// console.log(endContainerElement.outerHTML);
const endElementXPath = computeElementXPath(endContainerElement);
let cfi: string | undefined;
if (rootElementCfi && startElementCfi && endElementCfi) {
let startElementOrTextCfi = startElementCfi;
if (!startIsElement) {
const startContainerChildTextNodeIndexForCfi =
getChildTextNodeCfiIndex(startContainerElement, range.startContainer as Text);
// startContainerChildTextNodeIndex ===
// Array.from(startContainerElement.childNodes).indexOf(range.startContainer as ChildNode)
startElementOrTextCfi = startElementCfi + "/" +
startContainerChildTextNodeIndexForCfi + ":" + range.startOffset;
} else {
if (range.startOffset >= 0 && range.startOffset < startContainerElement.childNodes.length) {
const childNode = startContainerElement.childNodes[range.startOffset];
if (childNode.nodeType === Node.ELEMENT_NODE) {
startElementOrTextCfi = startElementCfi + "/" + ((range.startOffset + 1) * 2);
} else {
const cfiTextNodeIndex = getChildTextNodeCfiIndex(startContainerElement, childNode as Text);
startElementOrTextCfi = startElementCfi + "/" + cfiTextNodeIndex; // + ":0";
}
} else {
const cfiIndexOfLastElement = ((startContainerElement.childElementCount) * 2);
const lastChildNode = startContainerElement.childNodes[startContainerElement.childNodes.length - 1];
if (lastChildNode.nodeType === Node.ELEMENT_NODE) {
startElementOrTextCfi = startElementCfi + "/" + (cfiIndexOfLastElement + 1);
} else {
startElementOrTextCfi = startElementCfi + "/" + (cfiIndexOfLastElement + 2);
}
}
}
// console.log(`START TEXT CFI: ${startTextCfi}`);
let endElementOrTextCfi = endElementCfi;
if (!endIsElement) {
const endContainerChildTextNodeIndexForCfi =
getChildTextNodeCfiIndex(endContainerElement, range.endContainer as Text);
// endContainerChildTextNodeIndex ===
// Array.from(endContainerElement.childNodes).indexOf(range.endContainer as ChildNode)
endElementOrTextCfi = endElementCfi + "/" +
endContainerChildTextNodeIndexForCfi + ":" + range.endOffset;
} else {
if (range.endOffset >= 0 && range.endOffset < endContainerElement.childNodes.length) {
const childNode = endContainerElement.childNodes[range.endOffset];
if (childNode.nodeType === Node.ELEMENT_NODE) {
endElementOrTextCfi = endElementCfi + "/" + ((range.endOffset + 1) * 2);
} else {
const cfiTextNodeIndex = getChildTextNodeCfiIndex(endContainerElement, childNode as Text);
endElementOrTextCfi = endElementCfi + "/" + cfiTextNodeIndex; // + ":0";
}
} else {
const cfiIndexOfLastElement = ((endContainerElement.childElementCount) * 2);
const lastChildNode = endContainerElement.childNodes[endContainerElement.childNodes.length - 1];
if (lastChildNode.nodeType === Node.ELEMENT_NODE) {
endElementOrTextCfi = endElementCfi + "/" + (cfiIndexOfLastElement + 1);
} else {
endElementOrTextCfi = endElementCfi + "/" + (cfiIndexOfLastElement + 2);
}
}
}
// console.log(`END TEXT CFI: ${endTextCfi}`);
cfi = rootElementCfi + "," +
startElementOrTextCfi.replace(rootElementCfi, "") + "," +
endElementOrTextCfi.replace(rootElementCfi, "");
}
// -----------------
return [{
cfi,
endContainerChildTextNodeIndex,
endContainerElementCFI: endElementCfi,
endContainerElementXPath: endElementXPath,
endContainerElementCssSelector,
endOffset: range.endOffset,
startContainerChildTextNodeIndex,
startContainerElementCFI: startElementCfi,
startContainerElementXPath: startElementXPath,
startContainerElementCssSelector,
startOffset: range.startOffset,
}, {
cleanBefore,
cleanText,
cleanAfter,
rawBefore,
rawText,
rawAfter,
}];
}
export function convertRangeInfo(documant: Document, rangeInfo: IRangeInfo):
Range | undefined {
const startElement = documant.querySelector(rangeInfo.startContainerElementCssSelector);
if (!startElement) {
console.log("^^^ convertRangeInfo NO START ELEMENT CSS SELECTOR?!", rangeInfo.startContainerElementCssSelector);
return undefined;
}
let startContainer: Node = startElement;
if (rangeInfo.startContainerChildTextNodeIndex >= 0) {
if (rangeInfo.startContainerChildTextNodeIndex >= startElement.childNodes.length) {
// tslint:disable-next-line:max-line-length
console.log("^^^ convertRangeInfo rangeInfo.startContainerChildTextNodeIndex >= startElement.childNodes.length?!");
return undefined;
}
startContainer = startElement.childNodes[rangeInfo.startContainerChildTextNodeIndex];
if (startContainer.nodeType !== Node.TEXT_NODE) {
console.log("^^^ convertRangeInfo startContainer.nodeType !== Node.TEXT_NODE?!");
return undefined;
}
}
const endElement = documant.querySelector(rangeInfo.endContainerElementCssSelector);
if (!endElement) {
console.log("^^^ convertRangeInfo NO END ELEMENT CSS SELECTOR?!", rangeInfo.endContainerElementCssSelector);
return undefined;
}
let endContainer: Node = endElement;
if (rangeInfo.endContainerChildTextNodeIndex >= 0) {
if (rangeInfo.endContainerChildTextNodeIndex >= endElement.childNodes.length) {
// tslint:disable-next-line:max-line-length
console.log("^^^ convertRangeInfo rangeInfo.endContainerChildTextNodeIndex >= endElement.childNodes.length?!");
return undefined;
}
endContainer = endElement.childNodes[rangeInfo.endContainerChildTextNodeIndex];
if (endContainer.nodeType !== Node.TEXT_NODE) {
console.log("^^^ convertRangeInfo endContainer.nodeType !== Node.TEXT_NODE?!");
return undefined;
}
}
return createOrderedRange(startContainer, rangeInfo.startOffset, endContainer, rangeInfo.endOffset);
}
function getCommonAncestorElement(node1: Node, node2: Node): Element | undefined {
if (node1.nodeType === Node.ELEMENT_NODE && node1 === node2) {
return node1 as Element;
}
if (node1.nodeType === Node.ELEMENT_NODE && node1.contains(node2)) {
return node1 as Element;
}
if (node2.nodeType === Node.ELEMENT_NODE && node2.contains(node1)) {
return node2 as Element;
}
const node1ElementAncestorChain: Element[] = [];
let parent: Node | null = node1.parentNode;
while (parent && parent.nodeType === Node.ELEMENT_NODE) {
node1ElementAncestorChain.push(parent as Element);
parent = parent.parentNode;
}
const node2ElementAncestorChain: Element[] = [];
parent = node2.parentNode;
while (parent && parent.nodeType === Node.ELEMENT_NODE) {
node2ElementAncestorChain.push(parent as Element);
parent = parent.parentNode;
}
let commonAncestor = node1ElementAncestorChain.find((node1ElementAncestor) => {
return node2ElementAncestorChain.indexOf(node1ElementAncestor) >= 0;
});
if (!commonAncestor) {
commonAncestor = node2ElementAncestorChain.find((node2ElementAncestor) => {
return node1ElementAncestorChain.indexOf(node2ElementAncestor) >= 0;
});
}
return commonAncestor;
}
function isCfiTextNode(node: Node) {
return node.nodeType !== Node.ELEMENT_NODE;
// return node.nodeType === Node.TEXT_NODE ||
// node.nodeType === Node.COMMENT_NODE ||
// node.nodeType === Node.CDATA_SECTION_NODE; // other?
}
function getChildTextNodeCfiIndex(element: Element, child: Text): number {
let found = -1;
let textNodeIndex = -1;
let previousWasElement = false;
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < element.childNodes.length; i++) {
const childNode = element.childNodes[i];
const isText = isCfiTextNode(childNode);
if (isText || previousWasElement) {
textNodeIndex += 2;
}
if (isText) {
if (childNode === child) {
found = textNodeIndex;
break;
}
}
previousWasElement = childNode.nodeType === Node.ELEMENT_NODE;
}
return found;
}
// function getChildTextNode(element: Element, index: number): Text | undefined {
// let i = -1;
// for (const childNode of element.childNodes) {
// if (i > index) {
// return undefined;
// }
// if (childNode.nodeType === Node.TEXT_NODE) {
// i++;
// if (i === index) {
// return childNode as Text;
// }
// }
// }
// return undefined;
// }
// function getChildTextNodeIndex(element: Element, child: Text, forCfi: boolean): number {
// let found = -1;
// let textNodeIndex = -1;
// let previousWasElement = false;
// // tslint:disable-next-line:prefer-for-of
// for (let i = 0; i < element.childNodes.length; i++) {
// const childNode = element.childNodes[i];
// if (childNode.nodeType !== Node.TEXT_NODE && childNode.nodeType !== Node.ELEMENT_NODE) {
// continue;
// }
// if (forCfi) {
// if (childNode.nodeType === Node.TEXT_NODE || previousWasElement) {
// textNodeIndex += 2;
// }
// }
// if (childNode.nodeType === Node.TEXT_NODE) {
// if (!forCfi) {
// textNodeIndex++;
// }
// if (childNode === child) {
// found = textNodeIndex;
// break;
// }
// }
// previousWasElement = childNode.nodeType === Node.ELEMENT_NODE;
// }
// return found;
// }
// https://github.com/GoogleChromeLabs/text-fragments-polyfill/blob/53375fea08665bac009bb0aa01a030e065c3933d/src/fragment-generation-utils.js#L1418
// moveRangeEdgesToTextNodes() / normalizeRange() ?
// https://github.com/apache/incubator-annotator/issues/51
// https://github.com/apache/incubator-annotator/commit/c7e98eeac3e07e45b14b94e2aec2add8f137aea9
// https://github.com/apache/incubator-annotator/pull/98/files#diff-557f9d738d69e6fad35b0780d8b722f6f00b36676c20d46d33d4fad35912d940
// https://github.com/apache/incubator-annotator/blob/main/packages/dom/src/normalize-range.ts
/**
* Normalise a {@link https://developer.mozilla.org/en-US/docs/Web/API/Range |
* Range} such that ranges spanning the same text become exact equals.
*
* *Note: in this context ‘text’ means any characters, including whitespace.*
* Normalises a range such that both its start and end are text nodes, and that
* if there are equivalent text selections it takes the narrowest option (i.e.
* it prefers the start not to be at the end of a text node, and vice versa).
*
* If there is no text between the start and end, they thus collapse onto one a
* single position; and if there are multiple equivalent positions, it takes the
* first one; or, if scope is passed, the first equivalent falling within scope.
*
* Note that if the given range does not contain non-empty text nodes, it may
* end up pointing at a text node outside of it (before it if possible, else
* after). If the document does not contain any text nodes, an error is thrown.
*/
export function normalizeRange(r: Range): Range {
const range = r.cloneRange(); // new Range(); // document.createRange()
const documant = range.startContainer.ownerDocument;
if (!documant) {
range.collapse();
return range;
}
const walker = documant.createTreeWalker(documant, NodeFilter.SHOW_TEXT,
// {
// acceptNode(_node: Text) {
// return NodeFilter.FILTER_ACCEPT;
// },
// }
);
const resStart = snapBoundaryPointToTextNode(
range.startContainer,
range.startOffset,
walker,
);
if (!resStart) {
range.collapse();
return range;
}
let startContainer = resStart[0];
let startOffset = resStart[1];
walker.currentNode = startContainer;
while (startOffset === startContainer.length && walker.nextNode()) {
startContainer = walker.currentNode as Text;
startOffset = 0;
}
range.setStart(startContainer, startOffset);
const resEnd = snapBoundaryPointToTextNode(
range.endContainer,
range.endOffset,
walker,
);
if (!resEnd) {
range.collapse();
return range;
}
let endContainer = resEnd[0];
let endOffset = resEnd[1];
walker.currentNode = endContainer;
while (endOffset === 0 && walker.previousNode()) {
endContainer = walker.currentNode as Text;
endOffset = endContainer.length;
}
range.setEnd(endContainer, endOffset);
return range;
}
function snapBoundaryPointToTextNode(
node: Node,
offset: number,
walker: TreeWalker,
): [Text, number] | undefined {
if (isText(node)) {
return [node, offset];
}
let curNode: Node | undefined;
if (isCharacterData(node)) {
curNode = node;
} else if (offset < node.childNodes.length) {
curNode = node.childNodes[offset];
} else {
curNode = node;
while (!curNode.nextSibling) {
if (!curNode.parentNode) {
return undefined;
}
curNode = curNode.parentNode;
}
curNode = curNode.nextSibling;
}
if (isText(curNode)) {
return [curNode, 0];
}
// const documant = node.ownerDocument ?? (node as Document);
// const walker = documant.createTreeWalker(documant, NodeFilter.SHOW_TEXT);
walker.currentNode = curNode;
if (walker.nextNode()) {
return [walker.currentNode as Text, 0];
} else if (walker.previousNode()) {
return [walker.currentNode as Text, (walker.currentNode as Text).length];
} else {
return undefined;
}
}
function isText(node: Node): node is Text {
return node.nodeType === Node.TEXT_NODE;
}
function isCharacterData(node: Node): node is CharacterData {
return (
node.nodeType === Node.PROCESSING_INSTRUCTION_NODE ||
node.nodeType === Node.COMMENT_NODE ||
node.nodeType === Node.TEXT_NODE
);
}
// https://github.com/webmodules/range-normalize/pull/2
// "Normalizes" the DOM Range instance, such that slight variations in the start
// and end containers end up being normalized to the same "base" representation.
// The aim is to always have `startContainer` and `endContainer` pointing to
// TextNode instances.
// Pseudo-logic is as follows:
// - Expand the boundaries if they fall between siblings.
// - Narrow the boundaries until they point at leaf nodes.
// - Is the start container excluded by its offset?
// - Move it to the next leaf Node, but not past the end container.
// - Is the start container a leaf Node but not a TextNode?
// - Set the start boundary to be before the Node.
// - Is the end container excluded by its offset?
// - Move it to the previous leaf Node, but not past the start container.
// - Is the end container a leaf Node but not a TextNode?
// - Set the end boundary to be after the Node.
export function normalizeRange_(r: Range): Range {
const range = r.cloneRange(); // new Range(); // document.createRange()
let sc = range.startContainer;
let so = range.startOffset;
let ec = range.endContainer;
let eo = range.endOffset;
// Move the start container to the last leaf before any sibling boundary.
if (sc.childNodes.length && so > 0) {
sc = lastLeaf(sc.childNodes[so - 1]);
so = (sc as CharacterData).length || 0;
}
// Move the end container to the first leaf after any sibling boundary.
if (eo < ec.childNodes.length) {
ec = firstLeaf(ec.childNodes[eo]);
eo = 0;
}
// Move each container inward until it reaches a leaf Node.
let start: Node | null = firstLeaf(sc);
let end: Node | null = lastLeaf(ec);
// Define a predicate to check if a Node is a leaf Node inside the Range.
function isLeafNodeInRange(node: Node): boolean {
if (node.childNodes.length) {
return false;
}
const length = (node as CharacterData).length || 0;
if (node === sc && so === length) {
return false;
}
if (node === ec && eo === 0) {
return false;
}
return true;
}
// Move the start container until it is included or collapses to the end.
while (start && !isLeafNodeInRange(start) && start !== end) {
start = documentForward(start);
}
if (start === sc) {
range.setStart(sc, so);
} else if (start !== null) {
if (start.nodeType === 3) {
range.setStart(start, 0);
} else {
range.setStartBefore(start);
}
}
// Move the end container until it is included or collapses to the start.
while (end && !isLeafNodeInRange(end) && end !== start) {
end = documentReverse(end);
}
if (end === ec) {
range.setEnd(ec, eo);
} else if (end !== null) {
if (end.nodeType === 3) {
range.setEnd(end, (end as CharacterData).length);
} else {
range.setEndAfter(end);
}
}
return range;
}
// Return the next Node in a document order traversal.
// This order is equivalent to a classic pre-order.
function documentForward(node: Node): Node | null {
if (node.firstChild) {
return node.firstChild;
}
let n: Node | null = node;
while (!n.nextSibling) {
n = n.parentNode;
if (!n) {
return null;
}
}