forked from alohaeditor/Aloha-Editor
/
dom.js
executable file
·1888 lines (1702 loc) · 71 KB
/
dom.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
/* dom.js is part of Aloha Editor project http://aloha-editor.org
*
* Aloha Editor is a WYSIWYG HTML5 inline editing library and editor.
* Copyright (c) 2010-2012 Gentics Software GmbH, Vienna, Austria.
* Contributors http://aloha-editor.org/contribution.php
*
* Aloha Editor is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* Aloha Editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* As an additional permission to the GNU GPL version 2, you may distribute
* non-source (e.g., minimized or compacted) forms of the Aloha-Editor
* source code without the copy of the GNU GPL normally required,
* provided you include this license notice and a URL through which
* recipients can access the Corresponding Source.
*/
/*jslint eqeq: true */
// Ensure GENTICS Namespace
window.GENTICS = window.GENTICS || {};
window.GENTICS.Utils = window.GENTICS.Utils || {};
define(['jquery', 'util/class', 'aloha/ecma5shims'], function (jQuery, Class, $_) {
"use strict";
var GENTICS = window.GENTICS,
// Class = window.Class,
// http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-1841493061
Node = {
'ELEMENT_NODE': 1,
'ATTRIBUTE_NODE': 2,
'TEXT_NODE': 3,
'CDATA_SECTION_NODE': 4,
'ENTITY_REFERENCE_NODE': 5,
'ENTITY_NODE': 6,
'PROCESSING_INSTRUCTION_NODE': 7,
'COMMENT_NODE': 8,
'DOCUMENT_NODE': 9,
'DOCUMENT_TYPE_NODE': 10,
'DOCUMENT_FRAGMENT_NODE': 11,
'NOTATION_NODE': 12,
//The two nodes are disconnected. Order between disconnected nodes is always implementation-specific.
'DOCUMENT_POSITION_DISCONNECTED': 0x01,
//The second node precedes the reference node.
'DOCUMENT_POSITION_PRECEDING': 0x02,
//The node follows the reference node.
'DOCUMENT_POSITION_FOLLOWING': 0x04,
//The node contains the reference node. A node which contains is always preceding, too.
'DOCUMENT_POSITION_CONTAINS': 0x08,
//The node is contained by the reference node. A node which is contained is always following, too.
'DOCUMENT_POSITION_CONTAINED_BY': 0x10,
//The determination of preceding versus following is implementation-specific.
'DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC': 0x20
},
blockElementNames = {
'P': true,
'H1': true,
'H2': true,
'H3': true,
'H4': true,
'H5': true,
'H6': true,
'LI': true
};
/**
* Can't use elem.childNodes.length because
* http://www.quirksmode.org/dom/w3c_core.html
* "IE up to 8 does not count empty text nodes."
*
* Taken from Dom2.js
*/
function numChildren(elem) {
var count = 0;
var child = elem.firstChild;
while (child) {
count += 1;
child = child.nextSibling;
}
return count;
}
/**
* Taken from Dom2.js
*/
function nodeLength(node) {
if (1 === node.nodeType) {
return numChildren(node);
}
if (3 === node.nodeType) {
return node.length;
}
return 0;
}
/**
* Checks if the element given is an aloha-editing-p helper, added by split.
*
* @param {HTMLElement} node
* @return {Boolean} True if the given element is an
* aloha-editing-paragraph.
*/
function isAlohaEditingP(node) {
return (
node.className === 'aloha-editing-p'
&& nodeLength(node) === 1
&& node.children[0].nodeName === 'BR'
&& node.children[0].className === 'aloha-end-br'
);
}
/**
* Starting from the given node, will walk forward (right-ward) through the
* node until an element is found that matches the predicate `match` or we
* reach the last element in the tree inside the editing host.
*
* @param {HTMLElement} node An element that must be inside an editable.
* @param {function(HTMLElement):Boolean} match A prediate function to
* determine wether or not the
* node matches one we are
* looking for.
* @return {HTMLElement} The matched node that is forward in the DOM tree
* from `node`; null if nothing can be found that
* matches `match`.
*/
function findNodeForward(node, match) {
if (!node) {
return null;
}
if (match(node)) {
return node;
}
var next = node.firstChild
|| node.nextSibling
|| (
node.parentNode
&& !GENTICS.Utils.Dom.isEditingHost(node.parentNode)
&& node.parentNode.nextSibling
);
return next ? findNodeForward(next, match) : null;
}
function isVisiblyEmpty(node) {
if (!node) {
return true;
}
// TODO: use isChildlessElement()
if ('BR' === node.nodeName) {
return false;
}
if (node.nodeType === Node.TEXT_NODE) {
// TODO: would prefer to use
// (Html.isWhitespaces(node) || Html.isZeroWidthCharacters(node))
// but cannot because of circular dependency
if (node.data.search(/\S/) === -1) {
return true;
}
// Fix for IE with zero-width characters
if (1 === node.data.length && node.data.charCodeAt(0) >= 0x2000) {
return true;
}
return false;
}
var numChildren = nodeLength(node);
if (0 === numChildren) {
return true;
}
var children = node.childNodes;
var i;
for (i = 0; i < numChildren; i++) {
if (!isVisiblyEmpty(children[i])) {
return false;
}
}
return true;
}
/**
* Checks for the opposite condition of isVisiblyEmpty().
*
* @param {HTMLElement} node
* @return {Boolean} True if the given node is visible not empty.
*/
function isNotVisiblyEmpty(node) {
return !isVisiblyEmpty(node);
}
/**
* Checks whether the given element is a "phantom" element-- ie: an element
* that is either invisible or an aloha-editing-paragraph element.
*
* @param {HTMLElement} node
* @return {Boolean} True if the element is a "phantom" element.
*/
function isPhantomNode(node) {
return isVisiblyEmpty(node) || isAlohaEditingP(node);
}
/**
* Inserts the DOM node `element` appropriately during a split operation.
*
* The element `head` is used to reference where the element should be
* inserted. Depending on the structure of this `head` node, the `element`
* will either replace/overwrite `head` or be appending immediately after
* `head`.
*
* @param {HTMLElement} head The "head" element of the "head and tail"
* nodes resulting from splitting a DOM element.
* @param {HTMLElement} element The element to be inserted between the head
* and tail split parts.
*/
function insertAfterSplit(head, element) {
if (head.nodeType !== Node.TEXT_NODE && isPhantomNode(head)) {
jQuery(head).replaceWith(element);
} else {
jQuery(head).after(element);
}
}
/**
* @namespace GENTICS.Utils
* @class Dom provides methods to get information about the DOM and to manipulate it
* @singleton
*/
var Dom = Class.extend({
/**
* Regex to find word characters.
*/
wordRegex: /[\u0041-\u005A\u0061-\u007A\u00AA\u00B5\u00BA\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u0525\u0531-\u0556\u0559\u0561-\u0587\u05D0-\u05EA\u05F0-\u05F2\u0621-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971\u0972\u0979-\u097F\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C33\u0C35-\u0C39\u0C3D\u0C58\u0C59\u0C60\u0C61\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDE\u0CE0\u0CE1\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D28\u0D2A-\u0D39\u0D3D\u0D60\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC\u0EDD\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8B\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10D0-\u10FA\u10FC\u1100-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191C\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19C1-\u19C7\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4B\u1B83-\u1BA0\u1BAE\u1BAF\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1CE9-\u1CEC\u1CEE-\u1CF1\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u2094\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2183\u2184\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CEE\u2D00-\u2D25\u2D30-\u2D65\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005\u3006\u3031-\u3035\u303B\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31B7\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCB\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA65F\uA662-\uA66E\uA67F-\uA697\uA6A0-\uA6E5\uA717-\uA71F\uA722-\uA788\uA78B\uA78C\uA7FB-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA80-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uABC0-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA2D\uFA30-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]/,
/**
* Regex to find non-word characters.
*/
nonWordRegex: /[^\u0041-\u005A\u0061-\u007A\u00AA\u00B5\u00BA\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u0525\u0531-\u0556\u0559\u0561-\u0587\u05D0-\u05EA\u05F0-\u05F2\u0621-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971\u0972\u0979-\u097F\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C33\u0C35-\u0C39\u0C3D\u0C58\u0C59\u0C60\u0C61\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDE\u0CE0\u0CE1\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D28\u0D2A-\u0D39\u0D3D\u0D60\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC\u0EDD\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8B\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10D0-\u10FA\u10FC\u1100-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191C\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19C1-\u19C7\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4B\u1B83-\u1BA0\u1BAE\u1BAF\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1CE9-\u1CEC\u1CEE-\u1CF1\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u2094\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2183\u2184\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CEE\u2D00-\u2D25\u2D30-\u2D65\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005\u3006\u3031-\u3035\u303B\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31B7\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCB\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA65F\uA662-\uA66E\uA67F-\uA697\uA6A0-\uA6E5\uA717-\uA71F\uA722-\uA788\uA78B\uA78C\uA7FB-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA80-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uABC0-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA2D\uFA30-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]/,
/**
* Tags which can safely be merged
* @hide
*/
mergeableTags: ['b', 'code', 'del', 'em', 'i', 'ins', 'strong', 'sub', 'sup', '#text'],
/**
* Tags which do not mark word boundaries
* @hide
*/
nonWordBoundaryTags: ['a', 'b', 'code', 'del', 'em', 'i', 'ins', 'span', 'strong', 'sub', 'sup', '#text'],
/**
* Tags which are considered 'nonempty', even if they have no children (or not data)
* TODO: finish this list
* @hide
*/
nonEmptyTags: ['br'],
/**
* Tags which make up Flow Content or Phrasing Content, according to the HTML 5 specification,
* @see http://dev.w3.org/html5/spec/Overview.html#flow-content
* @see http://dev.w3.org/html5/spec/Overview.html#phrasing-content
* @hide
*/
tags: {
'flow': ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'bdi', 'bdo', 'blockquote', 'br', 'button', 'canvas', 'cite', 'code', 'command', 'datalist', 'del', 'details', 'dfn', 'div', 'dl', 'em', 'embed', 'fieldset', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'keygen', 'label', 'map', 'mark', 'math', 'menu', 'meter', 'nav', 'noscript', 'object', 'ol', 'output', 'p', 'pre', 'progress', 'q', 'ruby', 's', 'samp', 'script', 'section', 'select', 'small', 'span', 'strong', 'style', 'sub', 'sup', 'svg', 'table', 'textarea', 'time', 'u', 'ul', 'var', 'video', 'wbr', '#text'],
'phrasing': ['a', 'abbr', 'area', 'audio', 'b', 'bdi', 'bdo', 'br', 'button', 'canvas', 'cite', 'code', 'command', 'datalist', 'del', 'dfn', 'em', 'embed', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'keygen', 'label', 'map', 'mark', 'math', 'meter', 'noscript', 'object', 'output', 'progress', 'q', 'ruby', 'samp', 'script', 'select', 'small', 'span', 'strong', 'sub', 'sup', 'svg', 'textarea', 'time', 'u', 'var', 'video', 'wbr', '#text']
},
/**
* Possible children of tags, according to the HTML 5
* specification.
* See http://dev.w3.org/html5/spec/Overview.html#elements-1
* Moved to http://www.whatwg.org/specs/web-apps/current-work/#elements-1
* @hide
*/
children: {
'a': 'phrasing', // transparent
'abbr': 'phrasing',
'address': 'flow',
'area': 'empty',
'article': 'flow',
'aside': 'flow',
'audio': 'source', // transparent
'b': 'phrasing',
'base': 'empty',
'bdo': 'phrasing',
'blockquote': 'phrasing',
'body': 'flow',
'br': 'empty',
'button': 'phrasing',
'canvas': 'phrasing', // transparent
'caption': 'flow',
'cite': 'phrasing',
'code': 'phrasing',
'col': 'empty',
'colgroup': 'col',
'command': 'empty',
'datalist': ['phrasing', 'option'],
'dd': 'flow',
'del': 'phrasing',
'div': 'flow',
'details': ['summary', 'flow'],
'dfn': 'flow',
'dl': ['dt', 'dd'],
'dt': 'phrasing', // varies
'em': 'phrasing',
'embed': 'empty',
'fieldset': ['legend', 'flow'],
'figcaption': 'flow',
'figure': ['figcaption', 'flow'],
'footer': 'flow',
'form': 'flow',
'h1': 'phrasing',
'h2': 'phrasing',
'h3': 'phrasing',
'h4': 'phrasing',
'h5': 'phrasing',
'h6': 'phrasing',
//head
'header': 'flow',
'hgroup': ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
'hr': 'empty',
//html :)
'i': 'phrasing',
'iframe': '#text',
'img': 'empty',
'input': 'empty',
'ins': 'phrasing', // transparent
'kbd': 'phrasing',
'keygen': 'empty',
'label': 'phrasing',
'legend': 'phrasing',
'li': 'flow',
'link': 'empty',
'map': 'area', // transparent
'mark': 'phrasing',
'menu': ['li', 'flow'],
'meta': 'empty',
'meter': 'phrasing',
'nav': 'flow',
'noscript': 'phrasing', // varies
'object': 'param', // transparent
'ol': 'li',
'optgroup': 'option',
'option': '#text',
'output': 'phrasing',
'p': 'phrasing',
'param': 'empty',
'pre': 'phrasing',
'progress': 'phrasing',
'q': 'phrasing',
'rp': 'phrasing',
'rt': 'phrasing',
'ruby': ['phrasing', 'rt', 'rp'],
's': 'phrasing',
'samp': 'pharsing',
'script': '#script', //script
'section': 'flow',
'select': ['option', 'optgroup'],
'small': 'phrasing',
'source': 'empty',
'span': 'phrasing',
'strong': 'phrasing',
'style': 'phrasing', // varies
'sub': 'phrasing',
'summary': 'phrasing',
'sup': 'phrasing',
'table': ['caption', 'colgroup', 'thead', 'tbody', 'tfoot', 'tr'],
'tbody': 'tr',
'td': 'flow',
'textarea': '#text',
'tfoot': 'tr',
'th': 'phrasing',
'thead': 'tr',
'time': 'phrasing',
'title': '#text',
'tr': ['th', 'td'],
'track': 'empty',
'u': 'phrasing',
'ul': 'li',
'var': 'phrasing',
'video': 'source', // transparent
'wbr': 'empty'
},
/**
* List of nodenames of blocklevel elements
* TODO: finish this list
* @hide
*/
blockLevelElements: ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'div', 'pre'],
/**
* List of nodenames of list elements
* @hide
*/
listElements: ['li', 'ol', 'ul'],
/**
* Splits a DOM element at the given position up until the limiting object(s), so that it is valid HTML again afterwards.
* @param {RangeObject} range Range object that indicates the position of the splitting.
* This range will be updated, so that it represents the same range as before the split.
* @param {jQuery} limit Limiting node(s) for the split.
* The limiting node will not be included in the split itself.
* If no limiting object is set, the document body will be the limiting object.
* @param {boolean} atEnd If set to true, the DOM will be splitted at the end of the range otherwise at the start.
* @return {object} jQuery object containing the two root DOM objects of the split, true if the DOM did not need to be split or false if the DOM could not be split
* @method
*/
split: function (range, limit, atEnd) {
var splitElement = jQuery(range.startContainer),
splitPosition = range.startOffset,
updateRange,
path,
parents,
newDom,
insertElement,
secondPart,
i,
pathLength,
element,
jqelement,
children,
newElement,
next,
prev,
offset;
if (atEnd) {
splitElement = jQuery(range.endContainer);
splitPosition = range.endOffset;
}
if (limit.length < 1) {
limit = jQuery(document.body);
}
// we may have to update the range if it is not collapsed and we are splitting at the start
updateRange = (!range.isCollapsed() && !atEnd);
// find the path up to the highest object that will be splitted
parents = splitElement.parents().get();
parents.unshift(splitElement.get(0));
jQuery.each(parents, function (index, element) {
var isLimit = limit.filter(
function () {
return this == element;
}
).length;
if (isLimit) {
if (index > 0) {
path = parents.slice(0, index);
}
return false;
}
});
// nothing found to split -> return here
if (!path) {
return true;
}
path = path.reverse();
// iterate over the path, create new dom nodes for every element and move
// the contents right of the split to the new element
for (i = 0, pathLength = path.length; i < pathLength; ++i) {
element = path[i];
if (i === pathLength - 1) {
// last element in the path -> we have to split it
// split the last part into two parts
if (element.nodeType === 3) {
// text node
secondPart = document.createTextNode(element.data.substring(splitPosition, element.data.length));
element.data = element.data.substring(0, splitPosition);
if (this.isEmpty(secondPart) && jQuery('br', newDom).length === 0) {
secondPart = jQuery('<br/>').addClass('aloha-end-br');
}
} else {
// other nodes
jqelement = jQuery(element);
children = jqelement.contents();
newElement = jqelement.clone(false).empty();
secondPart = newElement.append(children.slice(splitPosition, children.length)).get(0);
jQuery(secondPart).addClass('aloha-editing-p');
if (secondPart.childNodes.length === 1 && secondPart.childNodes.item(0).nodeName.toLowerCase() === 'br') {
jQuery(secondPart.childNodes.item(0)).addClass('aloha-end-br');
}
}
// update the range if necessary
if (updateRange && range.endContainer === element) {
range.endContainer = secondPart;
range.endOffset -= splitPosition;
range.clearCaches();
}
// add the second part
if (insertElement) {
insertElement.prepend(secondPart);
} else {
jQuery(element).after(secondPart);
}
} else {
// create the new element of the same type and prepend it to the previously created element
newElement = jQuery(element).clone(false).empty();
if (!newDom) {
newDom = newElement;
} else {
insertElement.prepend(newElement);
}
insertElement = newElement;
// move all contents right of the split to the new element
while (true) {
next = path[i + 1].nextSibling;
if (!next) {
break;
}
insertElement.append(next);
}
// update the range if necessary
if (updateRange && range.endContainer === element) {
range.endContainer = newElement.get(0);
prev = path[i + 1];
offset = 0;
while (true) {
prev = prev.previousSibling;
if (!prev) {
break;
}
offset++;
}
range.endOffset -= offset;
range.clearCaches();
}
}
}
// append the new dom
jQuery(path[0]).after(newDom);
return jQuery([path[0], newDom ? newDom.get(0) : secondPart]);
},
/**
* Check whether the HTML 5 specification allows direct nesting of the given DOM
* objects.
* @param {object} outerDOMObject
* outer (nesting) DOM Object
* @param {object} innerDOMObject
* inner (nested) DOM Object
* @return {boolean} true when the nesting is allowed, false if not
* @method
*/
allowsNesting: function (outerDOMObject, innerDOMObject) {
if (!outerDOMObject || !outerDOMObject.nodeName || !innerDOMObject || !innerDOMObject.nodeName) {
return false;
}
var outerNodeName = outerDOMObject.nodeName.toLowerCase(),
innerNodeName = innerDOMObject.nodeName.toLowerCase();
if (!this.children[outerNodeName]) {
return false;
}
// check whether the nesting is configured by node names (like for table)
if (this.children[outerNodeName] == innerNodeName) {
return true;
}
if (jQuery.isArray(this.children[outerNodeName]) && jQuery.inArray(innerNodeName, this.children[outerNodeName]) >= 0) {
return true;
}
if (jQuery.isArray(this.tags[this.children[outerNodeName]])
&& jQuery.inArray(innerNodeName, this.tags[this.children[outerNodeName]]) >= 0) {
return true;
}
return false;
},
/**
* Apply the given markup additively to the given range. The given rangeObject will be modified if necessary
* @param {GENTICS.Utils.RangeObject} rangeObject range to which the markup shall be added
* @param {jQuery} markup markup to be applied as jQuery object
* @param {boolean} allownesting true when nesting of the added markup is allowed, false if not (default: false)
* @method
*/
addMarkup: function (rangeObject, markup, nesting) {
// split partially contained text nodes at the start and end of the range
if (rangeObject.startContainer.nodeType === 3
&& rangeObject.startOffset > 0
&& rangeObject.startOffset < rangeObject.startContainer.data.length) {
this.split(rangeObject, jQuery(rangeObject.startContainer).parent(), false);
}
if (rangeObject.endContainer.nodeType === 3 && rangeObject.endOffset > 0 && rangeObject.endOffset < rangeObject.endContainer.data.length) {
this.split(rangeObject, jQuery(rangeObject.endContainer).parent(), true);
}
// get the range tree
var rangeTree = rangeObject.getRangeTree();
this.recursiveAddMarkup(rangeTree, markup, rangeObject, nesting);
// cleanup DOM
this.doCleanup({
'merge': true,
'removeempty': true
}, rangeObject);
},
/**
* Recursive helper method to add the given markup to the range
* @param rangeTree rangetree at the current level
* @param markup markup to be applied
* @param rangeObject range object, which eventually is updated
* @param nesting true when nesting of the added markup is allowed, false if not
* @hide
*/
recursiveAddMarkup: function (rangeTree, markup, rangeObject, nesting) {
var i, innerRange, rangeLength;
// iterate through all rangetree objects of that level
for (i = 0, rangeLength = rangeTree.length; i < rangeLength; ++i) {
// check whether the rangetree object is fully contained and the markup may be wrapped around the object
if (rangeTree[i].type == 'full' && this.allowsNesting(markup.get(0), rangeTree[i].domobj)) {
// we wrap the object, when
// 1. nesting of markup is allowed or the node is not of the markup to be added
// 2. the node an element node or a non-empty text node
if ((nesting || rangeTree[i].domobj.nodeName != markup.get(0).nodeName) && (rangeTree[i].domobj.nodeType !== 3 || jQuery.trim(rangeTree[i].domobj.data).length !== 0)) {
// wrap the object
jQuery(rangeTree[i].domobj).wrap(markup);
// TODO eventually update the range (if it changed)
// when nesting is not allowed, we remove the markup from the inner element
if (!nesting && rangeTree[i].domobj.nodeType !== 3) {
innerRange = new GENTICS.Utils.RangeObject();
innerRange.startContainer = innerRange.endContainer = rangeTree[i].domobj.parentNode;
innerRange.startOffset = 0;
innerRange.endOffset = innerRange.endContainer.childNodes.length;
this.removeMarkup(innerRange, markup, jQuery(rangeTree[i].domobj.parentNode));
}
}
} else {
// TODO check whether the object may be replaced by the given markup
//if (false) {
// TODO replace
//} else {
// recurse into the children (if any), but not if nesting is not
// allowed and the object is of the markup to be added
if ((nesting || (rangeTree[i].domobj && rangeTree[i].domobj.nodeName !== markup.get(0).nodeName)) && rangeTree[i].children && rangeTree[i].children.length > 0) {
this.recursiveAddMarkup(rangeTree[i].children, markup);
}
}
}
},
/**
* Find the highest occurrence of a node with given nodename within the parents
* of the start. When limit objects are given, the search stops there.
* The limiting object is of the found type, it won't be considered
* @param {DOMObject} start start object
* @param {String} nodeName name of the node to search for (case-insensitive)
* @param {jQuery} limit Limiting node(s) as jQuery object (if none given, the search will stop when there are no more parents)
* @return {DOMObject} the found DOM object or undefined
* @method
*/
findHighestElement: function (start, nodeName, limit) {
nodeName = nodeName.toLowerCase();
// this will be the highest found markup object (up to a limit object)
var highestObject,
// blah
testObject = start,
// helper function to stop when we reach a limit object
isLimit = limit ? function () {
return limit.filter(
function () {
return testObject == this;
}
).length;
} : function () {
return false;
};
// now get the highest parent that has the given markup (until we reached
// one of the limit objects or there are no more parent nodes)
while (!isLimit() && testObject) {
if (testObject.nodeName.toLowerCase() === nodeName) {
highestObject = testObject;
}
testObject = testObject.parentNode;
}
return highestObject;
},
/**
* Remove the given markup from the given range. The given rangeObject will be modified if necessary
* TODO: add parameter deep/shallow
* @param {GENTICS.Utils.RangeObject} rangeObject range from which the markup shall be removed
* @param {jQuery} markup markup to be removed as jQuery object
* @param {jQuery} limit Limiting node(s) as jQuery object
* @method
*/
removeMarkup: function (rangeObject, markup, limit) {
var nodeName = markup.get(0).nodeName,
startSplitLimit = this.findHighestElement(rangeObject.startContainer, nodeName, limit),
endSplitLimit = this.findHighestElement(rangeObject.endContainer, nodeName, limit),
didSplit = false,
highestObject,
root,
rangeTree;
if (startSplitLimit && rangeObject.startOffset > 0) {
// when the start is in the start of its container, we don't split
this.split(rangeObject, jQuery(startSplitLimit).parent(), false);
didSplit = true;
}
if (endSplitLimit) {
// when the end is in the end of its container, we don't split
if (rangeObject.endContainer.nodeType === 3 && rangeObject.endOffset < rangeObject.endContainer.data.length) {
this.split(rangeObject, jQuery(endSplitLimit).parent(), true);
didSplit = true;
}
if (rangeObject.endContainer.nodeType === 1 && rangeObject.endOffset < rangeObject.endContainer.childNodes.length) {
this.split(rangeObject, jQuery(endSplitLimit).parent(), true);
didSplit = true;
}
}
// when we split the DOM, we maybe need to correct the range
if (didSplit) {
rangeObject.correctRange();
}
// find the highest occurrence of the markup
highestObject = this.findHighestElement(rangeObject.getCommonAncestorContainer(), nodeName, limit);
root = highestObject ? highestObject.parentNode : rangeObject.getCommonAncestorContainer();
if (root) {
// construct the range tree
rangeTree = rangeObject.getRangeTree(root);
// remove the markup from the range tree
this.recursiveRemoveMarkup(rangeTree, markup);
// cleanup DOM
this.doCleanup({
'merge': true,
'removeempty': true
}, rangeObject, root);
}
},
/**
* TODO: pass the range itself and eventually update it if necessary
* Recursive helper method to remove the given markup from the range
* @param rangeTree rangetree at the current level
* @param markup markup to be applied
* @hide
*/
recursiveRemoveMarkup: function (rangeTree, markup) {
var i, rangeLength, content;
// iterate over the rangetree objects of this level
for (i = 0, rangeLength = rangeTree.length; i < rangeLength; ++i) {
// check whether the object is the markup to be removed and is fully into the range
if (rangeTree[i].type == 'full' && rangeTree[i].domobj.nodeName == markup.get(0).nodeName) {
// found the markup, so remove it
content = jQuery(rangeTree[i].domobj).contents();
if (content.length > 0) {
// when the object has children, we unwrap them
content.first().unwrap();
} else {
// obj has no children, so just remove it
jQuery(rangeTree[i].domobj).remove();
}
}
// if the object has children, we do the recursion now
if (rangeTree[i].children) {
this.recursiveRemoveMarkup(rangeTree[i].children, markup);
}
}
},
/**
* Cleanup the DOM, starting with the given startobject (or the common ancestor container of the given range)
* ATTENTION: If range is a selection you need to update the selection after doCleanup
* Cleanup modes (given as properties in 'cleanup'):
* <pre>
* - merge: merges multiple successive nodes of same type, if this is allowed, starting at the children of the given node (defaults to false)
* - removeempty: removes empty element nodes (defaults to false)
* - mergeable: Custom function to predicate whether or not a given
* element should be mergeable. Overrides `mergeableTags'.
* </pre>
* Example for calling this method:<br/>
* <code>GENTICS.Utils.Dom.doCleanup({merge:true,removeempty:false}, range)</code>
* @param {object} cleanup type of cleanup to be done
* @param {GENTICS.Utils.RangeObject} rangeObject range which is eventually updated
* @param {DOMObject} start start object, if not given, the commonancestorcontainer is used as startobject insted
* @return {boolean} true when the range (startContainer/startOffset/endContainer/endOffset) was modified, false if not
* @method
*/
doCleanup: function (cleanup, rangeObject, start) {
var that = this,
prevNode,
modifiedRange,
startObject,
startOffset,
endOffset;
if (typeof cleanup === 'undefined') {
cleanup = {};
}
if (typeof cleanup.merge === 'undefined') {
cleanup.merge = false;
}
if (typeof cleanup.removeempty === 'undefined') {
cleanup.removeempty = false;
}
if (typeof start === 'undefined' && rangeObject) {
start = rangeObject.getCommonAncestorContainer();
}
// remember the previous node here (successive nodes of same type will be merged into this)
prevNode = false;
// check whether the range needed to be modified during merging
modifiedRange = false;
// get the start object
startObject = jQuery(start);
startOffset = rangeObject.startOffset;
endOffset = rangeObject.endOffset;
// iterate through all sub nodes
startObject.contents().each(function () {
var index;
// Try to read the nodeType property and return if we do not have permission
// ie.: frame document to an external URL
var nodeType;
try {
nodeType = this.nodeType;
index = that.getIndexInParent(this);
} catch (e) {
return;
}
// decide further actions by node type
switch (nodeType) {
// found a non-text node
case 1:
if (prevNode && prevNode.nodeName == this.nodeName) {
// found a successive node of same type
// now we check whether the selection starts or ends in the mother node after the current node
if (rangeObject.startContainer === startObject && startOffset > index) {
// there will be one less object, so reduce the startOffset by one
rangeObject.startOffset -= 1;
// set the flag for range modification
modifiedRange = true;
}
if (rangeObject.endContainer === startObject && endOffset > index) {
// there will be one less object, so reduce the endOffset by one
rangeObject.endOffset -= 1;
// set the flag for range modification
modifiedRange = true;
}
// merge the contents of this node into the previous one
jQuery(prevNode).append(jQuery(this).contents());
// after merging, we eventually need to cleanup the prevNode again
modifiedRange |= that.doCleanup(cleanup, rangeObject, prevNode);
// remove this node
jQuery(this).remove();
} else {
// do the recursion step here
modifiedRange |= that.doCleanup(cleanup, rangeObject, this);
// eventually remove empty elements
var removed = false;
if (cleanup.removeempty) {
if (GENTICS.Utils.Dom.isBlockLevelElement(this) && this.childNodes.length === 0) {
// jQuery(this).remove();
removed = true;
}
if (jQuery.inArray(this.nodeName.toLowerCase(), that.mergeableTags) >= 0 && jQuery(this).text().length === 0 && this.childNodes.length === 0) {
// jQuery(this).remove();
removed = true;
}
}
// when the current node was not removed, we eventually store it as previous (mergeable) tag
if (!removed) {
if (cleanup.mergeable
? cleanup.mergeable(this)
: jQuery.inArray(this.nodeName.toLowerCase(), that.mergeableTags) >= 0) {
prevNode = this;
} else {
prevNode = false;
}
} else {
// now we check whether the selection starts or ends in the mother node of this
if (rangeObject.startContainer === this.parentNode && startOffset > index) {
// there will be one less object, so reduce the startOffset by one
rangeObject.startOffset = rangeObject.startOffset - 1;
// set the flag for range modification
modifiedRange = true;
}
if (rangeObject.endContainer === this.parentNode && endOffset > index) {
// there will be one less object, so reduce the endOffset by one
rangeObject.endOffset = rangeObject.endOffset - 1;
// set the flag for range modification
modifiedRange = true;
}
// remove this text node
jQuery(this).remove();
}
}
break;
// found a text node
case 3:
// found a text node
if (prevNode && prevNode.nodeType === 3 && cleanup.merge) {
// the current text node will be merged into the last one, so
// check whether the selection starts or ends in the current
// text node
if (rangeObject.startContainer === this) {
// selection starts in the current text node
// update the start container to the last node
rangeObject.startContainer = prevNode;
// update the start offset
rangeObject.startOffset += prevNode.nodeValue.length;
// set the flag for range modification
modifiedRange = true;
} else if (rangeObject.startContainer === prevNode.parentNode && rangeObject.startOffset === that.getIndexInParent(prevNode) + 1) {
// selection starts right between the previous and current text nodes (which will be merged)
// update the start container to the previous node
rangeObject.startContainer = prevNode;
// set the start offset
rangeObject.startOffset = prevNode.nodeValue.length;
// set the flag for range modification
modifiedRange = true;
}
if (rangeObject.endContainer === this) {
// selection ends in the current text node
// update the end container to be the last node
rangeObject.endContainer = prevNode;
// update the end offset
rangeObject.endOffset += prevNode.nodeValue.length;
// set the flag for range modification
modifiedRange = true;
} else if (rangeObject.endContainer === prevNode.parentNode && rangeObject.endOffset === that.getIndexInParent(prevNode) + 1) {
// selection ends right between the previous and current text nodes (which will be merged)
// update the end container to the previous node
rangeObject.endContainer = prevNode;
// set the end offset
rangeObject.endOffset = prevNode.nodeValue.length;
// set the flag for range modification
modifiedRange = true;
}
// now append the contents of the current text node into the previous
prevNode.data += this.data;