This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
domutil.js
1453 lines (1322 loc) · 49.6 KB
/
domutil.js
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
/**
* @module domutil
* @desc Utilities that manipulate or query the DOM tree.
* @author Louis-Dominique Dubeau
* @license MPL 2.0
* @copyright 2013, 2014 Mangalam Research Center for Buddhist Languages
*/
define(/** @lends module:domutil */ function (require, exports, module) {
"use strict";
var $ = require("jquery");
var rangy = require("rangy");
var util = require("./util");
function indexOf(a, target) {
var length = a.length;
for (var i = 0; i < length; ++i) {
if (a[i] === target) return i;
}
return -1;
}
/**
* Gets the first range in the selection.
* @param {Window} win The window for which we want the selection.
* @returns {Range|undefined} The first range in the
* selection. Undefined if there is no selection or no range.
*/
function getSelectionRange(win) {
var sel = rangy.getSelection(win);
if (sel === undefined || sel.rangeCount < 1)
return undefined;
return sel.getRangeAt(0);
}
/**
* Creates a range from two points in a document.
*
* @param {Node} startContainer
* @param {integer} startOffset
* @param {Node} endContainer
* @param {integer} endOffset
* @returns {{range: Range, reversed: boolean}} The <code>range</code>
* field is a rangy range. The <code>reversed</code> field is
* <code>true</code> if the range is reversed, that is, if the end
* comes before the start.
*/
function rangeFromPoints(startContainer, startOffset,
endContainer, endOffset) {
var range = rangy.createRange(startContainer.ownerDocument);
var reversed = false;
if (rangy.dom.comparePoints(startContainer, startOffset,
endContainer, endOffset) <= 0) {
range.setStart(startContainer, startOffset);
range.setEnd(endContainer, endOffset);
}
else {
range.setStart(endContainer, endOffset);
range.setEnd(startContainer, startOffset);
reversed = true;
}
return {range: range, reversed: reversed};
}
/**
* Focuses the node itself or if the node is a text node, focuses the
* parent.
*
* @param {Node} node Node to focus.
* @throws {Error} If the node is neither a text node nor an
* element. Trying to focus something other than these is almost
* certainly an algorithmic bug.
*/
function focusNode(node) {
var node_type = node && node.nodeType;
switch(node_type) {
case Node.TEXT_NODE:
node.parentNode.focus();
break;
case Node.ELEMENT_NODE:
node.focus();
break;
default:
throw new Error("tried to focus something other than a text node or " +
"an element.");
}
}
/**
*
* <p>This function determines the caret position if the caret was
* moved forward.</p>
*
* <p>This function does not fully emulate how a browser moves the
* caret. The sole emulation it performs is to check whether
* whitespace matters or not. It skips whitespace that does not
* matter.</p>
*
* @param {Array} caret A caret position where the search starts. This
* should be an array of length two that has in first position the
* node where the caret is and in second position the offset in that
* node. This pair is to be interpreted in the same way node, offset
* pairs are interpreted in selection or range objects.
*
* @param {Node} container A DOM node which indicates the container
* within which caret movements must be contained.
*
* @param {boolean} no_text If true, and a text node would be
* returned, the function will instead return the parent of the text
* node.
*
* @returns {Array|null} Returns the next caret position, or null if such
* position does not exist. The <code>container</code> parameter
* constrains movements to positions inside it.
*/
function nextCaretPosition(caret, container, no_text) {
var node = caret[0];
var offset = caret[1];
if (no_text === undefined)
no_text = true;
var found = false;
var window = node.ownerDocument.defaultView;
var parent;
search_loop:
while(!found) {
parent = node.parentNode;
switch(node.nodeType) {
case Node.TEXT_NODE:
if (offset >= node.length ||
// If the parent node is set to normal whitespace
// handling, then moving the caret forward by one
// position will skip this whitespace.
(parent.lastChild === node &&
window.getComputedStyle(parent, null).whiteSpace ===
"normal" && /^\s+$/.test(node.data.slice(offset)))) {
// We would move outside the container
if (container !== undefined && node === container)
break search_loop;
offset = indexOf(parent.childNodes, node) + 1;
node = parent;
}
else {
offset++;
found = true;
}
break;
case Node.ELEMENT_NODE:
if (offset >= node.childNodes.length) {
// If we've hit the end of what we can search, stop.
if (parent === null ||
parent === node.ownerDocument ||
// We would move outside the container
(container !== undefined && node === container))
break search_loop;
offset = indexOf(parent.childNodes, node) + 1;
node = parent;
found = true;
}
else {
node = node.childNodes[offset];
offset = 0;
if (!(node.childNodes.length > 0 &&
node.childNodes[offset].nodeType ===
Node.TEXT_NODE))
found = true;
}
break;
}
}
if (!found)
return null;
parent = node.parentNode;
return (no_text && node.nodeType === Node.TEXT_NODE) ?
[parent, indexOf(parent.childNodes, node)] : [node, offset];
}
/**
*
* <p>This function determines the caret position if the caret was
* moved backwards.</p>
*
* <p>This function does not fully emulate how a browser moves the
* caret. The sole emulation it performs is to check whether
* whitespace matters or not. It skips whitespace that does not
* matter.</p>
*
* @param {Array} caret A caret position where the search starts. This
* should be an array of length two that has in first position the
* node where the caret is and in second position the offset in that
* node. This pair is to be interpreted in the same way node, offset
* pairs are interpreted in selection or range objects.
*
* @param {Node} container A DOM node which indicates the container
* within which caret movements must be contained.
*
* @param {boolean} no_text If true, and a text node would be
* returned, the function will instead return the parent of the text
* node.
*
* @returns {Array} Returns the previous caret position, or null if
* such position does not exist.
*/
function prevCaretPosition(caret, container, no_text) {
var node = caret[0];
var offset = caret[1];
if (no_text === undefined)
no_text = true;
var found = false;
var parent;
search_loop:
while(!found) {
offset--;
parent = node.parentNode;
switch(node.nodeType) {
case Node.TEXT_NODE:
if (offset < 0 ||
// If the parent node is set to normal whitespace
// handling, then moving the caret back by one
// position will skip this whitespace.
(parent.firstChild === node &&
window.getComputedStyle(parent, null).whiteSpace ===
"normal" && /^\s+$/.test(node.data.slice(0, offset)))) {
// We would move outside the container
if (container !== undefined && node === container)
break search_loop;
offset = indexOf(parent.childNodes, node);
node = parent;
}
else
found = true;
break;
case Node.ELEMENT_NODE:
if (offset < 0 || node.childNodes.length === 0) {
// If we've hit the end of what we can search, stop.
if (parent === null ||
parent === node.ownerDocument ||
// We would move outside the container
(container !== undefined && node === container))
break search_loop;
offset = indexOf(parent.childNodes, node);
node = parent;
found = true;
}
// If node.childNodes.length === 0, the first branch would
// have been taken. No need to test that offset indexes to
// something that exists.
else {
node = node.childNodes[offset];
if (node.nodeType === Node.ELEMENT_NODE) {
offset = node.childNodes.length;
if (!(node.childNodes.length > 0 &&
node.childNodes[offset - 1].nodeType ===
Node.TEXT_NODE))
found = true;
}
else
offset = node.length + 1;
}
break;
}
}
if (!found)
return null;
parent = node.parentNode;
return (no_text && node.nodeType === Node.TEXT_NODE) ?
[parent, indexOf(parent.childNodes, node)] : [node, offset];
}
/**
* Given two trees A and B of DOM nodes, this function finds the node
* in tree B which corresponds to a node in tree A. The two trees must
* be structurally identical. If tree B is cloned from tree A, it will
* satisfy this requirement. This function does not work with
* attribute nodes.
*
* @param {Node} tree_a The root of the first tree.
* @param {Node} tree_b The root of the second tree.
* @param {Node} node_in_a A node in the first tree.
* @returns {Node} The node which corresponds to ``node_in_a`` in
* ``tree_b``.
* @throws {Error} If ``node_in_a`` is not ``tree_a`` or a child of ``tree_a``.
*/
function correspondingNode(tree_a, tree_b, node_in_a) {
var path = [];
var current = node_in_a;
while(current !== tree_a) {
var parent = current.parentNode;
if (!parent)
throw new Error("node_in_a is not tree_a or a child of tree_a");
path.unshift(indexOf(parent.childNodes, current));
current = parent;
}
var ret = tree_b;
while(path.length)
ret = ret.childNodes[path.shift()];
return ret;
}
/**
* Makes a placeholder element
*
* @param {string} text The text to put in the placeholder.
* @returns {Node} A node.
*/
function makePlaceholder(text) {
var span = document.createElement("span");
span.className = "_placeholder";
span.setAttribute("contenteditable", "true");
span.innerHTML = text || " ";
return span;
}
/**
* Splits a text node into two nodes. This function takes care to
* modify the DOM tree only once.
*
* @param {Node} text_node The text node to split into two text nodes.
* @param {integer} index The offset into the text node where to split.
* @returns {Array.<Node>} The first element of the array is the text
* node which contains the text before index and the second element
* of the array is the text node which contains the text after the
* index.
*/
function splitTextNode(text_node, index) {
var carets = _insertIntoText(text_node, index, undefined, false);
return [carets[0][0], carets[1][0]];
}
/**
* <p>Inserts an element into text, effectively splitting the text node in
* two. This function takes care to modify the DOM tree only once.</p>
*
* <p>This function must have <code>this</code> set to an object that
* has the <code>insertNodeAt</code> and <code>deleteNode</code> set
* to functions with the signatures of {@link
* module:domutil~insertNodeAt insertNodeAt} and {@link
* module:domutil~mergeTextNodes mergeTextNodes}. It optionally can
* have a <code>insertFragAt</code> function with the same signature
* as <code>insertNodeAt</code>.</p>
*
* @param {Node} text_node The text node that will be cut in two by the new
* element.
* @param {integer} index The offset into the text node where
* the new element is to be inserted.
* @param {Node} node The node to insert.
* @returns {Array} The first element of the array is a caret position
* (i.e. a pair of container and offset) marking the boundary between
* what comes before the material inserted and the material
* inserted. The second element of the array is a caret position
* marking the boundary between the material inserted and what comes
* after. If I insert "foo" at position 2 in "abcd", then the final
* result would be "abfoocd" and the first caret would mark the
* boundary between "ab" and "foo" and the second caret the boundary
* between "foo" and "cd".
* @throws {Error} If the node to insert is undefined or null.
*/
function genericInsertIntoText(text_node, index, node) {
// This function is meant to be called with this set to a proper
// value.
/* jshint validthis:true */
if (!node)
throw new Error("must pass an actual node to insert");
return _genericInsertIntoText.apply(this, arguments);
}
/**
* Inserts an element into text, effectively splitting the text node in
* two. This function takes care to modify the DOM tree only once.
*
* <p>This function must have <code>this</code> set to an object that
* has the <code>insertNodeAt</code> and <code>deleteNode</code> set
* to functions with the signatures of {@link
* module:domutil~insertNodeAt insertNodeAt} and {@link
* module:domutil~mergeTextNodes mergeTextNodes}. It optionally can
* have an <code>insertFragAt</code> function with the same signature
* as <code>insertNodeAt</code>.</p>
*
* @private
* @param {Node} text_node The text node that will be cut in two by the new
* element.
* @param {integer} index The offset into the text node where
* the new element is to be inserted.
* @param {Node} node The node to insert. If this parameter evaluates
* to <code>false</code>, then this function effectively splits the
* text node into two parts.
* @param {boolean} [clean=true] The operation must clean contiguous
* text nodes so as to merge them and must not create empty
* nodes. <strong>This code assumes that the text node into which data
* is added is not preceded or followed by another text node and that
* it is not empty.</strong> In other words, if the DOM tree on which
* this code is used does not have consecutive text nodes and no empty
* nodes, then after the call, it still won't.
* @returns {Array} The first element of the array is a caret position
* (i.e. a pair of container and offset) marking the boundary between
* what comes before the material inserted and the material
* inserted. The second element of the array is a caret position
* marking the boundary between the material inserted and what comes
* after. If I insert "foo" at position 2 in "abcd", then the final
* result would be "abfoocd" and the first caret would mark the
* boundary between "ab" and "foo" and the second caret the boundary
* between "foo" and "cd".
* @throws {Error} If <code>text_node</code> is not a text node.
*/
function _genericInsertIntoText(text_node, index, node, clean) {
// This function is meant to be called with this set to a proper
// value.
/* jshint validthis:true */
if (text_node.nodeType !== Node.TEXT_NODE)
throw new Error("insertIntoText called on non-text");
var start_caret;
var end_caret;
if (clean === undefined)
clean = true;
// Normalize
if (index < 0)
index = 0;
else if (index > text_node.length)
index = text_node.length;
var search_node, prev, next;
var is_fragment = node && (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE);
var parent = text_node.parentNode;
var text_node_at = indexOf(parent.childNodes, text_node);
if (clean && (!node || (is_fragment && node.childNodes.length === 0)))
{
start_caret = end_caret = [text_node, index];
}
else {
var frag = document.createDocumentFragment();
prev = document.createTextNode(text_node.data.slice(0, index));
frag.appendChild(prev);
if (node)
frag.appendChild(node);
next = document.createTextNode(text_node.data.slice(index));
var next_len = next.length;
frag.appendChild(next);
if (clean)
frag.normalize();
if (clean && index === 0)
start_caret = [parent, text_node_at];
else
start_caret = [frag.firstChild, index];
if (clean && index === text_node.length)
end_caret = [parent, text_node_at + frag.childNodes.length];
else
end_caret = [frag.lastChild, frag.lastChild.length - next_len];
this.deleteNode(text_node);
if (this.insertFragAt)
this.insertFragAt(parent, text_node_at, frag);
else
while (frag.firstChild)
this.insertNodeAt(parent, text_node_at++, frag.firstChild);
}
return [start_caret, end_caret];
}
var plain_dom_mockup = {
insertNodeAt: insertNodeAt,
insertFragAt: insertNodeAt,
deleteNode: deleteNode
};
/**
* See {@link module:domutil~_genericInsertIntoText _genericInsertIntoText}.
*
* @function
* @private
*/
var _insertIntoText = _genericInsertIntoText.bind(plain_dom_mockup);
/**
* Inserts an element into text, effectively splitting the text node in
* two. This function takes care to modify the DOM tree only once.
* @function
* @param {Node} text_node The text node that will be cut in two by the new
* element.
* @param {integer} index The offset into the text node where
* the new element is to be inserted.
* @param {Node} node The node to insert.
* @returns {Array} The first element of the array is a caret position
* (i.e. a pair of container and offset) marking the boundary between
* what comes before the material inserted and the material
* inserted. The second element of the array is a caret position
* marking the boundary between the material inserted and what comes
* after. If I insert "foo" at position 2 in "abcd", then the final
* result would be "abfoocd" and the first caret would mark the
* boundary between "ab" and "foo" and the second caret the boundary
* between "foo" and "cd".
*/
var insertIntoText = genericInsertIntoText.bind(plain_dom_mockup);
/**
* Inserts text into a node. This function will use already existing
* text nodes whenever possible rather than create a new text node.
*
* @param {Node} node The node where the text is to be inserted.
* @param {integer} index The location in the node where the text is
* to be inserted.
* @param {string} text The text to insert.
* @returns {Array.<Node>} The first element of the array is the node
* that was modified to insert the text. It will be
* <code>undefined</code> if no node was modified. The second element
* is the text node which contains the new text. The two elements are
* defined and equal if a text node was modified to contain the newly
* inserted text. They are unequal if a new text node had to be
* created to contain the new text. A return value of
* <code>[undefined, undefined]</code> means that no modification
* occurred (because the text passed was "").
* @throws {Error} If <code>node</code> is not an element or text Node type.
*/
function genericInsertText(node, index, text) {
// This function is meant to be called with this set to a proper
// value.
/* jshint validthis:true */
if (text === "")
return [undefined, undefined];
var text_node;
work:
while (true) {
switch(node.nodeType) {
case Node.ELEMENT_NODE:
var child = node.childNodes[index];
if (child && child.nodeType === Node.TEXT_NODE) {
// Prepend to already existing text node.
node = child;
index = 0;
continue work;
}
var prev = node.childNodes[index - 1];
if (prev && prev.nodeType === Node.TEXT_NODE) {
// Append to already existing text node.
node = prev;
index = node.length;
continue work;
}
// We have to create a text node
text_node = document.createTextNode(text);
this.insertNodeAt(node, index, text_node);
node = undefined;
break work;
case Node.TEXT_NODE:
var pre = node.data.slice(0, index);
var post = node.data.slice(index);
this.setTextNodeValue(node, pre + text + post);
text_node = node;
break work;
default:
throw new Error("unexpected node type: " + node.nodeType);
}
}
return [node, text_node];
}
/**
* Inserts text into a node. This function will use already existing
* text nodes whenever possible rather than create a new text node.
* @function
* @param {Node} node The node where the text is to be inserted.
* @param {integer} index The location in the node where the text is
* to be inserted.
* @param {string} text The text to insert.
* @returns {Array.<Node>} The first element of the array is the node
* that was modified to insert the text. It will be
* <code>undefined</code> if no node was modified. The second element
* is the text node which contains the new text. The two elements are
* defined and equal if a text node was modified to contain the newly
* inserted text. They are unequal if a new text node had to be
* created to contain the new text. A return value of
* <code>[undefined, undefined]</code> means that no modification
* occurred (because the text passed was "").
* @throws {Error} If <code>node</code> is not an element or text Node type.
*/
var insertText = genericInsertText.bind({
insertNodeAt: insertNodeAt,
setTextNodeValue: function (node, value) {
node.data = value;
}
});
/**
* Deletes text from a text node. If the text node becomes empty, it
* is deleted.
*
* @param {Node} node The text node from which to delete text.
* @param {integer} index The index at which to delete text.
* @param {integer} length The length of text to delete.
* @throws {Error} If <code>node</code> is not a text Node type.
*/
function deleteText(node, index, length) {
if (node.nodeType === Node.TEXT_NODE) {
node.deleteData(index, length);
if (!node.length)
node.parentNode.removeChild(node);
}
else
throw new Error("deleteText called on non-text");
}
/**
* This function recursively links two DOM trees through the jQuery
* <code>.data()</code> method. For an element in the first tree the data
* item named "wed_mirror_node" points to the corresponding element
* in the second tree, and vice-versa. It is presumed that the two
* DOM trees are perfect mirrors of each other, although no test is
* performed to confirm this.
*
* @param {Node} root_a A DOM node.
* @param {Node} root_b A second DOM node.
*
*/
function linkTrees(root_a, root_b) {
$.data(root_a, "wed_mirror_node", root_b);
$.data(root_b, "wed_mirror_node", root_a);
if (root_a.nodeType === Node.ELEMENT_NODE) {
for(var i = 0; i < root_a.childNodes.length; ++i) {
var child_a = root_a.childNodes[i];
var child_b = root_b.childNodes[i];
linkTrees(child_a, child_b);
}
}
}
/**
* This function recursively unlinks a DOM tree though the jQuery
* <code>.data()</code> method.
*
* @param {Node} root A DOM node.
*
*/
function unlinkTree(root) {
$.removeData(root, "wed_mirror_node");
if (root.nodeType === Node.ELEMENT_NODE)
for(var i = 0; i < root.childNodes.length; ++i)
unlinkTree(root.childNodes[i]);
}
/**
* <p>Returns the first descendant or the node passed to the function
* if the node happens to not have a descendant. The descendant returned
* is the deepest one which is first in its parent.</p>
*
* <p>When passed
* <code><p><b>A</b><b><q>B</q></b></p></code>
* this code would return the text node "A" because it has no children
* and is first.</p>
*
* @param {Node} node The node to search.
* @returns {Node} The first node which is both first in its parent
* and has no children.
*/
function firstDescendantOrSelf(node) {
while (node && node.childNodes && node.childNodes.length)
node = node.firstChild;
return node;
}
/**
* Removes the node. Mainly for use with the generic functions defined here.
*
* @param {Node} node The node to remove.
*/
function deleteNode(node) {
node.parentNode.removeChild(node);
}
/**
* Inserts a node at the position specified. Mainly for use with the
* generic functions defined here.
*
* @param {Node} parent The node which will become the parent of the
* inserted node.
* @param {integer} index The position at which to insert the node
* into the parent.
* @param {Node} node The node to insert.
*/
function insertNodeAt(parent, index, node) {
parent.insertBefore(node, parent.childNodes[index] || null);
}
/**
* Merges a text node with the next text node, if present. When called
* on something which is not a text node or if the next node is not
* text, does nothing. Mainly for use with the generic functions defined here.
*
* @param {Node} node The node to merge with the next node.
* @returns {Array} A two-element array. It is a caret position
* between the two parts that were merged, or between the two nodes
* that were not merged (because they were not both text).
*/
function mergeTextNodes(node) {
var next = node.nextSibling;
if (node.nodeType === Node.TEXT_NODE &&
next && next.nodeType === Node.TEXT_NODE) {
var offset = node.length;
node.appendData(next.data);
next.parentNode.removeChild(next);
return [node, offset];
}
var parent = node.parentNode;
return [parent, indexOf(parent.childNodes, node) + 1];
}
/**
* <p>Removes the contents between the start and end carets from the DOM
* tree. If two text nodes become adjacent, they are merged.</p>
*
* <p>This function must have <code>this</code> set to an object that
* has the <code>deleteText</code>, <code>deleteNode</code> and
* <code>mergeTextNodes</code> symbols set to functions with the
* signatures of {@link module:domutil~deleteText deleteText}, {@link
* module:domutil~deleteNode deleteNode} and {@link
* module:domutil~mergeTextNodes mergeTextNodes}.
*
* @param {Array} start_caret Start caret position.
* @param {Array} end_caret Ending caret position.
* @returns {Array} A pair of items. The first item is the caret
* position indicating where the cut happened. The second item is a
* list of nodes, the cut contents.
* @throws {Error} If Nodes in the range are not in the same element.
*/
function genericCutFunction (start_caret, end_caret) {
if (!isWellFormedRange({startContainer: start_caret[0],
startOffset: start_caret[1],
endContainer: end_caret[0],
endOffset: end_caret[1]}))
throw new Error("range is not well-formed");
// This function is meant to be called with this set to a proper
// value.
/* jshint validthis:true */
var start_container = start_caret[0];
var start_offset = start_caret[1];
var end_container = end_caret[0];
var end_offset = end_caret[1];
var parent = start_container.parentNode;
var final_caret;
var start_text;
if (start_container.nodeType === Node.TEXT_NODE &&
start_offset === 0) {
// We are at the start of a text node, move up to the parent.
start_offset = indexOf(parent.childNodes, start_container);
start_container = parent;
parent = start_container.parent;
}
if (start_container.nodeType === Node.TEXT_NODE) {
var same_container = start_container === end_container;
var start_container_offset = indexOf(
parent.childNodes, start_container);
var end_text_offset = same_container ? end_offset :
start_container.length;
start_text = parent.ownerDocument.createTextNode(
start_container.data.slice(start_offset, end_text_offset));
this.deleteText(start_container, start_offset, start_text.length);
final_caret = (start_container.parentNode) ?
[start_container, start_offset] :
// Selection was such that the text node was emptied.
[parent, start_container_offset];
if (same_container)
// Both the start and end were in the same node, so the
// deleteText operation above did everything needed.
return [final_caret, [start_text]];
// Alter our start to take care of the rest
start_offset = (start_container.parentNode) ?
// Look after the text node we just modified.
start_container_offset + 1 :
// Selection was such that the text node was emptied, and
// thus removed. So stay at the same place.
start_container_offset;
start_container = parent;
}
else
final_caret = [start_container, start_offset];
var end_text;
if (end_container.nodeType === Node.TEXT_NODE) {
parent = end_container.parentNode;
var end_container_offset = indexOf(
parent.childNodes, end_container);
end_text = parent.ownerDocument.createTextNode(
end_container.data.slice(0, end_offset));
this.deleteText(end_container, 0, end_offset);
// Alter our end to take care of the rest
end_offset = end_container_offset;
end_container = parent;
}
// At this point, the following checks must hold
if (start_container !== end_container)
throw new Error("internal error in cut: containers unequal");
if (start_container.nodeType !== Node.ELEMENT_NODE)
throw new Error("internal error in cut: not an element");
var return_nodes = [];
end_offset--;
// Doing it in reverse allows us to not worry about offsets
// getting out of whack.
while (end_offset >= start_offset) {
return_nodes.unshift(end_container.childNodes[end_offset]);
this.deleteNode(end_container.childNodes[end_offset]);
end_offset--;
}
if (start_text)
return_nodes.unshift(start_text);
if (end_text)
return_nodes.push(end_text);
// At this point, end_offset points to the node that is before the
// list of nodes removed.
if (end_container.childNodes[end_offset])
this.mergeTextNodes(end_container.childNodes[end_offset]);
return [final_caret, return_nodes];
}
/**
* Returns the <strong>element</strong> nodes that contain the start
* and the end of the range. If an end of the range happens to be in a
* text node, the element node will be that node's parent.
*
* @private
* @param {Object} range An object which has the
* <code>startContainer</code>, <code>startOffset</code>,
* <code>endContainer</code>, <code>endOffset</code> attributes
* set. The interpretation of these values is the same as for DOM
* <code>Range</code> objects. Therefore, the object passed can be a
* DOM range.
* @returns {Array} A pair of nodes.
* @throws {Error} If a node in <code>range</code> is not of element
* or text Node types.
*/
function nodePairFromRange(range) {
var start_node;
switch(range.startContainer.nodeType) {
case Node.TEXT_NODE:
start_node = range.startContainer.parentNode;
break;
case Node.ELEMENT_NODE:
start_node = range.startContainer;
break;
default:
throw new Error("unexpected node type: " +
range.startContainer.nodeType);
}
var end_node;
switch(range.endContainer.nodeType) {
case Node.TEXT_NODE:
end_node = range.endContainer.parentNode;
break;
case Node.ELEMENT_NODE:
end_node = range.endContainer;
break;
default:
throw new Error("unexpected node type: " + range.endContainer.nodeType);
}
return [start_node, end_node];
}
/**
* Determines whether a range is well-formed. A well-formed range is
* one which starts and ends in the same element.
*
* @param {Object} range An object which has the
* <code>startContainer</code>, <code>startOffset</code>,
* <code>endContainer</code>, <code>endOffset</code> attributes
* set. The interpretation of these values is the same as for DOM
* <code>Range</code> objects. Therefore, the object passed can be a
* DOM range.
* @returns {boolean} <code>true</code> if the range is well-formed.
* <code>false</code> if not.
*/
function isWellFormedRange(range) {
var pair = nodePairFromRange(range);
return pair[0] === pair[1];
}
/**
* Dumps the current selection to the console.
*
* @param {string} msg A message to output in front of the range
* information.
* @param {Window} win The window for which to dump selection
* information.
*/
function dumpCurrentSelection(msg, win) {
dumpRange(msg, getSelectionRange(win));
}
/**
* Dumps a range to the console.
*
* @param {string} msg A message to output in front of the range
* information.
* @param {Window} range The range.
*/
function dumpRange(msg, range) {
if (!range)
console.log(msg, "no range");
else
console.log(msg,
range.startContainer,
range.startOffset,
range.endContainer,
range.endOffset);
}
/**
* Dumps a range to a string.
*
* @param {string} msg A message to output in front of the range
* information.
* @param {Window} range The range.
*/
function dumpRangeToString(msg, range) {
var ret;
if (!range)
ret = [msg, "no range"];
else
ret = [msg,
range.startContainer.outerHTML,
range.startOffset,
range.endContainer.outerHTML,
range.endOffset];
return ret.join(", ");
}
/**
* Checks whether a point is in the element's contents. This means
* inside the element and <strong>not</strong> inside one of the
* scrollbars that the element may have. The coordinates passed must
* be <strong>relative to the document.</strong> If the coordinates
* are taken from an event, this means passing <code>pageX</code> and
* <code>pageY</code>.
*
* @param {Node} element The element to check.
* @param {number} x The x coordinate <strong>relative to the
* document.</strong>
* @param {number} y The y coordinate <strong>relative to the
* document.</strong>
* @returns {boolean} <code>true</code> if inside, <code>false</code>
* if not.
*/
function pointInContents(element, x, y) {
// Convert the coordinates relative to the document to coordinates
// relative to the element.
var body = element.ownerDocument.body;
// Using clientLeft and clientTop is not equivalent to using the rect.
var rect = element.getBoundingClientRect();
x -= rect.left + body.scrollLeft;
y -= rect.top + body.scrollTop;
return ((x >= 0) && (y >= 0) &&
(x < element.clientWidth) && (y < element.clientHeight));
}