3171a932dcf84b0cb3c6bcc7f783217298014cd6.svn-base
62 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
/*
Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/
/**
* Creates a CKEDITOR.dom.range instance that can be used inside a specific
* DOM Document.
* @class Represents a delimited piece of content in a DOM Document.
* It is contiguous in the sense that it can be characterized as selecting all
* of the content between a pair of boundary-points.<br>
* <br>
* This class shares much of the W3C
* <a href="http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html">Document Object Model Range</a>
* ideas and features, adding several range manipulation tools to it, but it's
* not intended to be compatible with it.
* @param {CKEDITOR.dom.document} document The document into which the range
* features will be available.
* @example
* // Create a range for the entire contents of the editor document body.
* var range = new CKEDITOR.dom.range( editor.document );
* range.selectNodeContents( editor.document.getBody() );
* // Delete the contents.
* range.deleteContents();
*/
CKEDITOR.dom.range = function( document )
{
/**
* Node within which the range begins.
* @type {CKEDITOR.NODE_ELEMENT|CKEDITOR.NODE_TEXT}
* @example
* var range = new CKEDITOR.dom.range( editor.document );
* range.selectNodeContents( editor.document.getBody() );
* alert( range.startContainer.getName() ); // "body"
*/
this.startContainer = null;
/**
* Offset within the starting node of the range.
* @type {Number}
* @example
* var range = new CKEDITOR.dom.range( editor.document );
* range.selectNodeContents( editor.document.getBody() );
* alert( range.startOffset ); // "0"
*/
this.startOffset = null;
/**
* Node within which the range ends.
* @type {CKEDITOR.NODE_ELEMENT|CKEDITOR.NODE_TEXT}
* @example
* var range = new CKEDITOR.dom.range( editor.document );
* range.selectNodeContents( editor.document.getBody() );
* alert( range.endContainer.getName() ); // "body"
*/
this.endContainer = null;
/**
* Offset within the ending node of the range.
* @type {Number}
* @example
* var range = new CKEDITOR.dom.range( editor.document );
* range.selectNodeContents( editor.document.getBody() );
* alert( range.endOffset ); // == editor.document.getBody().getChildCount()
*/
this.endOffset = null;
/**
* Indicates that this is a collapsed range. A collapsed range has it's
* start and end boudaries at the very same point so nothing is contained
* in it.
* @example
* var range = new CKEDITOR.dom.range( editor.document );
* range.selectNodeContents( editor.document.getBody() );
* alert( range.collapsed ); // "false"
* range.collapse();
* alert( range.collapsed ); // "true"
*/
this.collapsed = true;
/**
* The document within which the range can be used.
* @type {CKEDITOR.dom.document}
* @example
* // Selects the body contents of the range document.
* range.selectNodeContents( range.document.getBody() );
*/
this.document = document;
};
(function()
{
// Updates the "collapsed" property for the given range object.
var updateCollapsed = function( range )
{
range.collapsed = (
range.startContainer &&
range.endContainer &&
range.startContainer.equals( range.endContainer ) &&
range.startOffset == range.endOffset );
};
// This is a shared function used to delete, extract and clone the range
// contents.
// V2
var execContentsAction = function( range, action, docFrag, mergeThen )
{
range.optimizeBookmark();
var startNode = range.startContainer;
var endNode = range.endContainer;
var startOffset = range.startOffset;
var endOffset = range.endOffset;
var removeStartNode;
var removeEndNode;
// For text containers, we must simply split the node and point to the
// second part. The removal will be handled by the rest of the code .
if ( endNode.type == CKEDITOR.NODE_TEXT )
endNode = endNode.split( endOffset );
else
{
// If the end container has children and the offset is pointing
// to a child, then we should start from it.
if ( endNode.getChildCount() > 0 )
{
// If the offset points after the last node.
if ( endOffset >= endNode.getChildCount() )
{
// Let's create a temporary node and mark it for removal.
endNode = endNode.append( range.document.createText( '' ) );
removeEndNode = true;
}
else
endNode = endNode.getChild( endOffset );
}
}
// For text containers, we must simply split the node. The removal will
// be handled by the rest of the code .
if ( startNode.type == CKEDITOR.NODE_TEXT )
{
startNode.split( startOffset );
// In cases the end node is the same as the start node, the above
// splitting will also split the end, so me must move the end to
// the second part of the split.
if ( startNode.equals( endNode ) )
endNode = startNode.getNext();
}
else
{
// If the start container has children and the offset is pointing
// to a child, then we should start from its previous sibling.
// If the offset points to the first node, we don't have a
// sibling, so let's use the first one, but mark it for removal.
if ( !startOffset )
{
// Let's create a temporary node and mark it for removal.
startNode = startNode.getFirst().insertBeforeMe( range.document.createText( '' ) );
removeStartNode = true;
}
else if ( startOffset >= startNode.getChildCount() )
{
// Let's create a temporary node and mark it for removal.
startNode = startNode.append( range.document.createText( '' ) );
removeStartNode = true;
}
else
startNode = startNode.getChild( startOffset ).getPrevious();
}
// Get the parent nodes tree for the start and end boundaries.
var startParents = startNode.getParents();
var endParents = endNode.getParents();
// Compare them, to find the top most siblings.
var i, topStart, topEnd;
for ( i = 0 ; i < startParents.length ; i++ )
{
topStart = startParents[ i ];
topEnd = endParents[ i ];
// The compared nodes will match until we find the top most
// siblings (different nodes that have the same parent).
// "i" will hold the index in the parents array for the top
// most element.
if ( !topStart.equals( topEnd ) )
break;
}
var clone = docFrag, levelStartNode, levelClone, currentNode, currentSibling;
// Remove all successive sibling nodes for every node in the
// startParents tree.
for ( var j = i ; j < startParents.length ; j++ )
{
levelStartNode = startParents[j];
// For Extract and Clone, we must clone this level.
if ( clone && !levelStartNode.equals( startNode ) ) // action = 0 = Delete
levelClone = clone.append( levelStartNode.clone() );
currentNode = levelStartNode.getNext();
while ( currentNode )
{
// Stop processing when the current node matches a node in the
// endParents tree or if it is the endNode.
if ( currentNode.equals( endParents[ j ] ) || currentNode.equals( endNode ) )
break;
// Cache the next sibling.
currentSibling = currentNode.getNext();
// If cloning, just clone it.
if ( action == 2 ) // 2 = Clone
clone.append( currentNode.clone( true ) );
else
{
// Both Delete and Extract will remove the node.
currentNode.remove();
// When Extracting, move the removed node to the docFrag.
if ( action == 1 ) // 1 = Extract
clone.append( currentNode );
}
currentNode = currentSibling;
}
if ( clone )
clone = levelClone;
}
clone = docFrag;
// Remove all previous sibling nodes for every node in the
// endParents tree.
for ( var k = i ; k < endParents.length ; k++ )
{
levelStartNode = endParents[ k ];
// For Extract and Clone, we must clone this level.
if ( action > 0 && !levelStartNode.equals( endNode ) ) // action = 0 = Delete
levelClone = clone.append( levelStartNode.clone() );
// The processing of siblings may have already been done by the parent.
if ( !startParents[ k ] || levelStartNode.$.parentNode != startParents[ k ].$.parentNode )
{
currentNode = levelStartNode.getPrevious();
while ( currentNode )
{
// Stop processing when the current node matches a node in the
// startParents tree or if it is the startNode.
if ( currentNode.equals( startParents[ k ] ) || currentNode.equals( startNode ) )
break;
// Cache the next sibling.
currentSibling = currentNode.getPrevious();
// If cloning, just clone it.
if ( action == 2 ) // 2 = Clone
clone.$.insertBefore( currentNode.$.cloneNode( true ), clone.$.firstChild ) ;
else
{
// Both Delete and Extract will remove the node.
currentNode.remove();
// When Extracting, mode the removed node to the docFrag.
if ( action == 1 ) // 1 = Extract
clone.$.insertBefore( currentNode.$, clone.$.firstChild );
}
currentNode = currentSibling;
}
}
if ( clone )
clone = levelClone;
}
if ( action == 2 ) // 2 = Clone.
{
// No changes in the DOM should be done, so fix the split text (if any).
var startTextNode = range.startContainer;
if ( startTextNode.type == CKEDITOR.NODE_TEXT )
{
startTextNode.$.data += startTextNode.$.nextSibling.data;
startTextNode.$.parentNode.removeChild( startTextNode.$.nextSibling );
}
var endTextNode = range.endContainer;
if ( endTextNode.type == CKEDITOR.NODE_TEXT && endTextNode.$.nextSibling )
{
endTextNode.$.data += endTextNode.$.nextSibling.data;
endTextNode.$.parentNode.removeChild( endTextNode.$.nextSibling );
}
}
else
{
// Collapse the range.
// If a node has been partially selected, collapse the range between
// topStart and topEnd. Otherwise, simply collapse it to the start. (W3C specs).
if ( topStart && topEnd && ( startNode.$.parentNode != topStart.$.parentNode || endNode.$.parentNode != topEnd.$.parentNode ) )
{
var endIndex = topEnd.getIndex();
// If the start node is to be removed, we must correct the
// index to reflect the removal.
if ( removeStartNode && topEnd.$.parentNode == startNode.$.parentNode )
endIndex--;
// Merge splitted parents.
if ( mergeThen && topStart.type == CKEDITOR.NODE_ELEMENT )
{
var span = CKEDITOR.dom.element.createFromHtml( '<span ' +
'data-cke-bookmark="1" style="display:none"> </span>', range.document );
span.insertAfter( topStart );
topStart.mergeSiblings( false );
range.moveToBookmark( { startNode : span } );
}
else
range.setStart( topEnd.getParent(), endIndex );
}
// Collapse it to the start.
range.collapse( true );
}
// Cleanup any marked node.
if ( removeStartNode )
startNode.remove();
if ( removeEndNode && endNode.$.parentNode )
endNode.remove();
};
var inlineChildReqElements = { abbr:1,acronym:1,b:1,bdo:1,big:1,cite:1,code:1,del:1,dfn:1,em:1,font:1,i:1,ins:1,label:1,kbd:1,q:1,samp:1,small:1,span:1,strike:1,strong:1,sub:1,sup:1,tt:1,u:1,'var':1 };
// Creates the appropriate node evaluator for the dom walker used inside
// check(Start|End)OfBlock.
function getCheckStartEndBlockEvalFunction( isStart )
{
var skipBogus = false,
bookmarkEvaluator = CKEDITOR.dom.walker.bookmark( true ),
nbspRegExp = /^[\t\r\n ]*(?: |\xa0)$/;
return function( node )
{
// First ignore bookmark nodes.
if ( bookmarkEvaluator( node ) )
return true;
if ( node.type == CKEDITOR.NODE_TEXT )
{
// Skip the block filler NBSP.
if ( CKEDITOR.env.ie &&
nbspRegExp.test( node.getText() ) &&
!skipBogus &&
!( isStart && node.getNext() ) )
{
skipBogus = true;
}
// If there's any visible text, then we're not at the start.
else if ( node.hasAscendant( 'pre' ) || CKEDITOR.tools.trim( node.getText() ).length )
return false;
}
else if ( node.type == CKEDITOR.NODE_ELEMENT )
{
// If there are non-empty inline elements (e.g. <img />), then we're not
// at the start.
if ( !inlineChildReqElements[ node.getName() ] )
{
// Skip the padding block br.
if ( !CKEDITOR.env.ie &&
node.is( 'br' ) &&
!skipBogus &&
!( isStart && node.getNext() ) )
{
skipBogus = true;
}
else
return false;
}
}
return true;
};
}
var isBogus = CKEDITOR.dom.walker.bogus();
// Evaluator for CKEDITOR.dom.element::checkBoundaryOfElement, reject any
// text node and non-empty elements unless it's being bookmark text.
function elementBoundaryEval( checkStart )
{
return function( node )
{
// Tolerant bogus br when checking at the end of block.
// Reject any text node unless it's being bookmark
// OR it's spaces.
// Reject any element unless it's being invisible empty. (#3883)
return !checkStart && isBogus( node ) ||
( node.type == CKEDITOR.NODE_TEXT ?
!CKEDITOR.tools.trim( node.getText() ) || !!node.getParent().data( 'cke-bookmark' )
: node.getName() in CKEDITOR.dtd.$removeEmpty );
};
}
var whitespaceEval = new CKEDITOR.dom.walker.whitespaces(),
bookmarkEval = new CKEDITOR.dom.walker.bookmark();
function nonWhitespaceOrBookmarkEval( node )
{
// Whitespaces and bookmark nodes are to be ignored.
return !whitespaceEval( node ) && !bookmarkEval( node );
}
CKEDITOR.dom.range.prototype =
{
clone : function()
{
var clone = new CKEDITOR.dom.range( this.document );
clone.startContainer = this.startContainer;
clone.startOffset = this.startOffset;
clone.endContainer = this.endContainer;
clone.endOffset = this.endOffset;
clone.collapsed = this.collapsed;
return clone;
},
collapse : function( toStart )
{
if ( toStart )
{
this.endContainer = this.startContainer;
this.endOffset = this.startOffset;
}
else
{
this.startContainer = this.endContainer;
this.startOffset = this.endOffset;
}
this.collapsed = true;
},
/**
* The content nodes of the range are cloned and added to a document fragment, which is returned.
* <strong> Note: </strong> Text selection may lost after invoking this method. (caused by text node splitting).
*/
cloneContents : function()
{
var docFrag = new CKEDITOR.dom.documentFragment( this.document );
if ( !this.collapsed )
execContentsAction( this, 2, docFrag );
return docFrag;
},
/**
* Deletes the content nodes of the range permanently from the DOM tree.
* @param {Boolean} [mergeThen] Merge any splitted elements result in DOM true due to partial selection.
*/
deleteContents : function( mergeThen )
{
if ( this.collapsed )
return;
execContentsAction( this, 0, null, mergeThen );
},
/**
* The content nodes of the range are cloned and added to a document fragment,
* meanwhile they're removed permanently from the DOM tree.
* @param {Boolean} [mergeThen] Merge any splitted elements result in DOM true due to partial selection.
*/
extractContents : function( mergeThen )
{
var docFrag = new CKEDITOR.dom.documentFragment( this.document );
if ( !this.collapsed )
execContentsAction( this, 1, docFrag, mergeThen );
return docFrag;
},
/**
* Creates a bookmark object, which can be later used to restore the
* range by using the moveToBookmark function.
* This is an "intrusive" way to create a bookmark. It includes <span> tags
* in the range boundaries. The advantage of it is that it is possible to
* handle DOM mutations when moving back to the bookmark.
* Attention: the inclusion of nodes in the DOM is a design choice and
* should not be changed as there are other points in the code that may be
* using those nodes to perform operations. See GetBookmarkNode.
* @param {Boolean} [serializable] Indicates that the bookmark nodes
* must contain ids, which can be used to restore the range even
* when these nodes suffer mutations (like a clonation or innerHTML
* change).
* @returns {Object} And object representing a bookmark.
*/
createBookmark : function( serializable )
{
var startNode, endNode;
var baseId;
var clone;
var collapsed = this.collapsed;
startNode = this.document.createElement( 'span' );
startNode.data( 'cke-bookmark', 1 );
startNode.setStyle( 'display', 'none' );
// For IE, it must have something inside, otherwise it may be
// removed during DOM operations.
startNode.setHtml( ' ' );
if ( serializable )
{
baseId = 'cke_bm_' + CKEDITOR.tools.getNextNumber();
startNode.setAttribute( 'id', baseId + ( collapsed ? 'C' : 'S' ) );
}
// If collapsed, the endNode will not be created.
if ( !collapsed )
{
endNode = startNode.clone();
endNode.setHtml( ' ' );
if ( serializable )
endNode.setAttribute( 'id', baseId + 'E' );
clone = this.clone();
clone.collapse();
clone.insertNode( endNode );
}
clone = this.clone();
clone.collapse( true );
clone.insertNode( startNode );
// Update the range position.
if ( endNode )
{
this.setStartAfter( startNode );
this.setEndBefore( endNode );
}
else
this.moveToPosition( startNode, CKEDITOR.POSITION_AFTER_END );
return {
startNode : serializable ? baseId + ( collapsed ? 'C' : 'S' ) : startNode,
endNode : serializable ? baseId + 'E' : endNode,
serializable : serializable,
collapsed : collapsed
};
},
/**
* Creates a "non intrusive" and "mutation sensible" bookmark. This
* kind of bookmark should be used only when the DOM is supposed to
* remain stable after its creation.
* @param {Boolean} [normalized] Indicates that the bookmark must
* normalized. When normalized, the successive text nodes are
* considered a single node. To sucessful load a normalized
* bookmark, the DOM tree must be also normalized before calling
* moveToBookmark.
* @returns {Object} An object representing the bookmark.
*/
createBookmark2 : function( normalized )
{
var startContainer = this.startContainer,
endContainer = this.endContainer;
var startOffset = this.startOffset,
endOffset = this.endOffset;
var collapsed = this.collapsed;
var child, previous;
// If there is no range then get out of here.
// It happens on initial load in Safari #962 and if the editor it's
// hidden also in Firefox
if ( !startContainer || !endContainer )
return { start : 0, end : 0 };
if ( normalized )
{
// Find out if the start is pointing to a text node that will
// be normalized.
if ( startContainer.type == CKEDITOR.NODE_ELEMENT )
{
child = startContainer.getChild( startOffset );
// In this case, move the start information to that text
// node.
if ( child && child.type == CKEDITOR.NODE_TEXT
&& startOffset > 0 && child.getPrevious().type == CKEDITOR.NODE_TEXT )
{
startContainer = child;
startOffset = 0;
}
// Get the normalized offset.
if ( child && child.type == CKEDITOR.NODE_ELEMENT )
startOffset = child.getIndex( 1 );
}
// Normalize the start.
while ( startContainer.type == CKEDITOR.NODE_TEXT
&& ( previous = startContainer.getPrevious() )
&& previous.type == CKEDITOR.NODE_TEXT )
{
startContainer = previous;
startOffset += previous.getLength();
}
// Process the end only if not normalized.
if ( !collapsed )
{
// Find out if the start is pointing to a text node that
// will be normalized.
if ( endContainer.type == CKEDITOR.NODE_ELEMENT )
{
child = endContainer.getChild( endOffset );
// In this case, move the start information to that
// text node.
if ( child && child.type == CKEDITOR.NODE_TEXT
&& endOffset > 0 && child.getPrevious().type == CKEDITOR.NODE_TEXT )
{
endContainer = child;
endOffset = 0;
}
// Get the normalized offset.
if ( child && child.type == CKEDITOR.NODE_ELEMENT )
endOffset = child.getIndex( 1 );
}
// Normalize the end.
while ( endContainer.type == CKEDITOR.NODE_TEXT
&& ( previous = endContainer.getPrevious() )
&& previous.type == CKEDITOR.NODE_TEXT )
{
endContainer = previous;
endOffset += previous.getLength();
}
}
}
return {
start : startContainer.getAddress( normalized ),
end : collapsed ? null : endContainer.getAddress( normalized ),
startOffset : startOffset,
endOffset : endOffset,
normalized : normalized,
collapsed : collapsed,
is2 : true // It's a createBookmark2 bookmark.
};
},
moveToBookmark : function( bookmark )
{
if ( bookmark.is2 ) // Created with createBookmark2().
{
// Get the start information.
var startContainer = this.document.getByAddress( bookmark.start, bookmark.normalized ),
startOffset = bookmark.startOffset;
// Get the end information.
var endContainer = bookmark.end && this.document.getByAddress( bookmark.end, bookmark.normalized ),
endOffset = bookmark.endOffset;
// Set the start boundary.
this.setStart( startContainer, startOffset );
// Set the end boundary. If not available, collapse it.
if ( endContainer )
this.setEnd( endContainer, endOffset );
else
this.collapse( true );
}
else // Created with createBookmark().
{
var serializable = bookmark.serializable,
startNode = serializable ? this.document.getById( bookmark.startNode ) : bookmark.startNode,
endNode = serializable ? this.document.getById( bookmark.endNode ) : bookmark.endNode;
// Set the range start at the bookmark start node position.
this.setStartBefore( startNode );
// Remove it, because it may interfere in the setEndBefore call.
startNode.remove();
// Set the range end at the bookmark end node position, or simply
// collapse it if it is not available.
if ( endNode )
{
this.setEndBefore( endNode );
endNode.remove();
}
else
this.collapse( true );
}
},
getBoundaryNodes : function()
{
var startNode = this.startContainer,
endNode = this.endContainer,
startOffset = this.startOffset,
endOffset = this.endOffset,
childCount;
if ( startNode.type == CKEDITOR.NODE_ELEMENT )
{
childCount = startNode.getChildCount();
if ( childCount > startOffset )
startNode = startNode.getChild( startOffset );
else if ( childCount < 1 )
startNode = startNode.getPreviousSourceNode();
else // startOffset > childCount but childCount is not 0
{
// Try to take the node just after the current position.
startNode = startNode.$;
while ( startNode.lastChild )
startNode = startNode.lastChild;
startNode = new CKEDITOR.dom.node( startNode );
// Normally we should take the next node in DFS order. But it
// is also possible that we've already reached the end of
// document.
startNode = startNode.getNextSourceNode() || startNode;
}
}
if ( endNode.type == CKEDITOR.NODE_ELEMENT )
{
childCount = endNode.getChildCount();
if ( childCount > endOffset )
endNode = endNode.getChild( endOffset ).getPreviousSourceNode( true );
else if ( childCount < 1 )
endNode = endNode.getPreviousSourceNode();
else // endOffset > childCount but childCount is not 0
{
// Try to take the node just before the current position.
endNode = endNode.$;
while ( endNode.lastChild )
endNode = endNode.lastChild;
endNode = new CKEDITOR.dom.node( endNode );
}
}
// Sometimes the endNode will come right before startNode for collapsed
// ranges. Fix it. (#3780)
if ( startNode.getPosition( endNode ) & CKEDITOR.POSITION_FOLLOWING )
startNode = endNode;
return { startNode : startNode, endNode : endNode };
},
/**
* Find the node which fully contains the range.
* @param includeSelf
* @param {Boolean} ignoreTextNode Whether ignore CKEDITOR.NODE_TEXT type.
*/
getCommonAncestor : function( includeSelf , ignoreTextNode )
{
var start = this.startContainer,
end = this.endContainer,
ancestor;
if ( start.equals( end ) )
{
if ( includeSelf
&& start.type == CKEDITOR.NODE_ELEMENT
&& this.startOffset == this.endOffset - 1 )
ancestor = start.getChild( this.startOffset );
else
ancestor = start;
}
else
ancestor = start.getCommonAncestor( end );
return ignoreTextNode && !ancestor.is ? ancestor.getParent() : ancestor;
},
/**
* Transforms the startContainer and endContainer properties from text
* nodes to element nodes, whenever possible. This is actually possible
* if either of the boundary containers point to a text node, and its
* offset is set to zero, or after the last char in the node.
*/
optimize : function()
{
var container = this.startContainer;
var offset = this.startOffset;
if ( container.type != CKEDITOR.NODE_ELEMENT )
{
if ( !offset )
this.setStartBefore( container );
else if ( offset >= container.getLength() )
this.setStartAfter( container );
}
container = this.endContainer;
offset = this.endOffset;
if ( container.type != CKEDITOR.NODE_ELEMENT )
{
if ( !offset )
this.setEndBefore( container );
else if ( offset >= container.getLength() )
this.setEndAfter( container );
}
},
/**
* Move the range out of bookmark nodes if they'd been the container.
*/
optimizeBookmark: function()
{
var startNode = this.startContainer,
endNode = this.endContainer;
if ( startNode.is && startNode.is( 'span' )
&& startNode.data( 'cke-bookmark' ) )
this.setStartAt( startNode, CKEDITOR.POSITION_BEFORE_START );
if ( endNode && endNode.is && endNode.is( 'span' )
&& endNode.data( 'cke-bookmark' ) )
this.setEndAt( endNode, CKEDITOR.POSITION_AFTER_END );
},
trim : function( ignoreStart, ignoreEnd )
{
var startContainer = this.startContainer,
startOffset = this.startOffset,
collapsed = this.collapsed;
if ( ( !ignoreStart || collapsed )
&& startContainer && startContainer.type == CKEDITOR.NODE_TEXT )
{
// If the offset is zero, we just insert the new node before
// the start.
if ( !startOffset )
{
startOffset = startContainer.getIndex();
startContainer = startContainer.getParent();
}
// If the offset is at the end, we'll insert it after the text
// node.
else if ( startOffset >= startContainer.getLength() )
{
startOffset = startContainer.getIndex() + 1;
startContainer = startContainer.getParent();
}
// In other case, we split the text node and insert the new
// node at the split point.
else
{
var nextText = startContainer.split( startOffset );
startOffset = startContainer.getIndex() + 1;
startContainer = startContainer.getParent();
// Check all necessity of updating the end boundary.
if ( this.startContainer.equals( this.endContainer ) )
this.setEnd( nextText, this.endOffset - this.startOffset );
else if ( startContainer.equals( this.endContainer ) )
this.endOffset += 1;
}
this.setStart( startContainer, startOffset );
if ( collapsed )
{
this.collapse( true );
return;
}
}
var endContainer = this.endContainer;
var endOffset = this.endOffset;
if ( !( ignoreEnd || collapsed )
&& endContainer && endContainer.type == CKEDITOR.NODE_TEXT )
{
// If the offset is zero, we just insert the new node before
// the start.
if ( !endOffset )
{
endOffset = endContainer.getIndex();
endContainer = endContainer.getParent();
}
// If the offset is at the end, we'll insert it after the text
// node.
else if ( endOffset >= endContainer.getLength() )
{
endOffset = endContainer.getIndex() + 1;
endContainer = endContainer.getParent();
}
// In other case, we split the text node and insert the new
// node at the split point.
else
{
endContainer.split( endOffset );
endOffset = endContainer.getIndex() + 1;
endContainer = endContainer.getParent();
}
this.setEnd( endContainer, endOffset );
}
},
/**
* Expands the range so that partial units are completely contained.
* @param unit {Number} The unit type to expand with.
* @param {Boolean} [excludeBrs=false] Whether include line-breaks when expanding.
*/
enlarge : function( unit, excludeBrs )
{
switch ( unit )
{
case CKEDITOR.ENLARGE_ELEMENT :
if ( this.collapsed )
return;
// Get the common ancestor.
var commonAncestor = this.getCommonAncestor();
var body = this.document.getBody();
// For each boundary
// a. Depending on its position, find out the first node to be checked (a sibling) or, if not available, to be enlarge.
// b. Go ahead checking siblings and enlarging the boundary as much as possible until the common ancestor is not reached. After reaching the common ancestor, just save the enlargeable node to be used later.
var startTop, endTop;
var enlargeable, sibling, commonReached;
// Indicates that the node can be added only if whitespace
// is available before it.
var needsWhiteSpace = false;
var isWhiteSpace;
var siblingText;
// Process the start boundary.
var container = this.startContainer;
var offset = this.startOffset;
if ( container.type == CKEDITOR.NODE_TEXT )
{
if ( offset )
{
// Check if there is any non-space text before the
// offset. Otherwise, container is null.
container = !CKEDITOR.tools.trim( container.substring( 0, offset ) ).length && container;
// If we found only whitespace in the node, it
// means that we'll need more whitespace to be able
// to expand. For example, <i> can be expanded in
// "A <i> [B]</i>", but not in "A<i> [B]</i>".
needsWhiteSpace = !!container;
}
if ( container )
{
if ( !( sibling = container.getPrevious() ) )
enlargeable = container.getParent();
}
}
else
{
// If we have offset, get the node preceeding it as the
// first sibling to be checked.
if ( offset )
sibling = container.getChild( offset - 1 ) || container.getLast();
// If there is no sibling, mark the container to be
// enlarged.
if ( !sibling )
enlargeable = container;
}
while ( enlargeable || sibling )
{
if ( enlargeable && !sibling )
{
// If we reached the common ancestor, mark the flag
// for it.
if ( !commonReached && enlargeable.equals( commonAncestor ) )
commonReached = true;
if ( !body.contains( enlargeable ) )
break;
// If we don't need space or this element breaks
// the line, then enlarge it.
if ( !needsWhiteSpace || enlargeable.getComputedStyle( 'display' ) != 'inline' )
{
needsWhiteSpace = false;
// If the common ancestor has been reached,
// we'll not enlarge it immediately, but just
// mark it to be enlarged later if the end
// boundary also enlarges it.
if ( commonReached )
startTop = enlargeable;
else
this.setStartBefore( enlargeable );
}
sibling = enlargeable.getPrevious();
}
// Check all sibling nodes preceeding the enlargeable
// node. The node wil lbe enlarged only if none of them
// blocks it.
while ( sibling )
{
// This flag indicates that this node has
// whitespaces at the end.
isWhiteSpace = false;
if ( sibling.type == CKEDITOR.NODE_COMMENT )
{
sibling = sibling.getPrevious();
continue;
}
else if ( sibling.type == CKEDITOR.NODE_TEXT )
{
siblingText = sibling.getText();
if ( /[^\s\ufeff]/.test( siblingText ) )
sibling = null;
isWhiteSpace = /[\s\ufeff]$/.test( siblingText );
}
else
{
// If this is a visible element.
// We need to check for the bookmark attribute because IE insists on
// rendering the display:none nodes we use for bookmarks. (#3363)
// Line-breaks (br) are rendered with zero width, which we don't want to include. (#7041)
if ( ( sibling.$.offsetWidth > 0 || excludeBrs && sibling.is( 'br' ) ) && !sibling.data( 'cke-bookmark' ) )
{
// We'll accept it only if we need
// whitespace, and this is an inline
// element with whitespace only.
if ( needsWhiteSpace && CKEDITOR.dtd.$removeEmpty[ sibling.getName() ] )
{
// It must contains spaces and inline elements only.
siblingText = sibling.getText();
if ( (/[^\s\ufeff]/).test( siblingText ) ) // Spaces + Zero Width No-Break Space (U+FEFF)
sibling = null;
else
{
var allChildren = sibling.$.getElementsByTagName( '*' );
for ( var i = 0, child ; child = allChildren[ i++ ] ; )
{
if ( !CKEDITOR.dtd.$removeEmpty[ child.nodeName.toLowerCase() ] )
{
sibling = null;
break;
}
}
}
if ( sibling )
isWhiteSpace = !!siblingText.length;
}
else
sibling = null;
}
}
// A node with whitespaces has been found.
if ( isWhiteSpace )
{
// Enlarge the last enlargeable node, if we
// were waiting for spaces.
if ( needsWhiteSpace )
{
if ( commonReached )
startTop = enlargeable;
else if ( enlargeable )
this.setStartBefore( enlargeable );
}
else
needsWhiteSpace = true;
}
if ( sibling )
{
var next = sibling.getPrevious();
if ( !enlargeable && !next )
{
// Set the sibling as enlargeable, so it's
// parent will be get later outside this while.
enlargeable = sibling;
sibling = null;
break;
}
sibling = next;
}
else
{
// If sibling has been set to null, then we
// need to stop enlarging.
enlargeable = null;
}
}
if ( enlargeable )
enlargeable = enlargeable.getParent();
}
// Process the end boundary. This is basically the same
// code used for the start boundary, with small changes to
// make it work in the oposite side (to the right). This
// makes it difficult to reuse the code here. So, fixes to
// the above code are likely to be replicated here.
container = this.endContainer;
offset = this.endOffset;
// Reset the common variables.
enlargeable = sibling = null;
commonReached = needsWhiteSpace = false;
if ( container.type == CKEDITOR.NODE_TEXT )
{
// Check if there is any non-space text after the
// offset. Otherwise, container is null.
container = !CKEDITOR.tools.trim( container.substring( offset ) ).length && container;
// If we found only whitespace in the node, it
// means that we'll need more whitespace to be able
// to expand. For example, <i> can be expanded in
// "A <i> [B]</i>", but not in "A<i> [B]</i>".
needsWhiteSpace = !( container && container.getLength() );
if ( container )
{
if ( !( sibling = container.getNext() ) )
enlargeable = container.getParent();
}
}
else
{
// Get the node right after the boudary to be checked
// first.
sibling = container.getChild( offset );
if ( !sibling )
enlargeable = container;
}
while ( enlargeable || sibling )
{
if ( enlargeable && !sibling )
{
if ( !commonReached && enlargeable.equals( commonAncestor ) )
commonReached = true;
if ( !body.contains( enlargeable ) )
break;
if ( !needsWhiteSpace || enlargeable.getComputedStyle( 'display' ) != 'inline' )
{
needsWhiteSpace = false;
if ( commonReached )
endTop = enlargeable;
else if ( enlargeable )
this.setEndAfter( enlargeable );
}
sibling = enlargeable.getNext();
}
while ( sibling )
{
isWhiteSpace = false;
if ( sibling.type == CKEDITOR.NODE_TEXT )
{
siblingText = sibling.getText();
if ( /[^\s\ufeff]/.test( siblingText ) )
sibling = null;
isWhiteSpace = /^[\s\ufeff]/.test( siblingText );
}
else if ( sibling.type == CKEDITOR.NODE_ELEMENT )
{
// If this is a visible element.
// We need to check for the bookmark attribute because IE insists on
// rendering the display:none nodes we use for bookmarks. (#3363)
// Line-breaks (br) are rendered with zero width, which we don't want to include. (#7041)
if ( ( sibling.$.offsetWidth > 0 || excludeBrs && sibling.is( 'br' ) ) && !sibling.data( 'cke-bookmark' ) )
{
// We'll accept it only if we need
// whitespace, and this is an inline
// element with whitespace only.
if ( needsWhiteSpace && CKEDITOR.dtd.$removeEmpty[ sibling.getName() ] )
{
// It must contains spaces and inline elements only.
siblingText = sibling.getText();
if ( (/[^\s\ufeff]/).test( siblingText ) )
sibling = null;
else
{
allChildren = sibling.$.getElementsByTagName( '*' );
for ( i = 0 ; child = allChildren[ i++ ] ; )
{
if ( !CKEDITOR.dtd.$removeEmpty[ child.nodeName.toLowerCase() ] )
{
sibling = null;
break;
}
}
}
if ( sibling )
isWhiteSpace = !!siblingText.length;
}
else
sibling = null;
}
}
else
isWhiteSpace = 1;
if ( isWhiteSpace )
{
if ( needsWhiteSpace )
{
if ( commonReached )
endTop = enlargeable;
else
this.setEndAfter( enlargeable );
}
}
if ( sibling )
{
next = sibling.getNext();
if ( !enlargeable && !next )
{
enlargeable = sibling;
sibling = null;
break;
}
sibling = next;
}
else
{
// If sibling has been set to null, then we
// need to stop enlarging.
enlargeable = null;
}
}
if ( enlargeable )
enlargeable = enlargeable.getParent();
}
// If the common ancestor can be enlarged by both boundaries, then include it also.
if ( startTop && endTop )
{
commonAncestor = startTop.contains( endTop ) ? endTop : startTop;
this.setStartBefore( commonAncestor );
this.setEndAfter( commonAncestor );
}
break;
case CKEDITOR.ENLARGE_BLOCK_CONTENTS:
case CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS:
// Enlarging the start boundary.
var walkerRange = new CKEDITOR.dom.range( this.document );
body = this.document.getBody();
walkerRange.setStartAt( body, CKEDITOR.POSITION_AFTER_START );
walkerRange.setEnd( this.startContainer, this.startOffset );
var walker = new CKEDITOR.dom.walker( walkerRange ),
blockBoundary, // The node on which the enlarging should stop.
tailBr, // In case BR as block boundary.
notBlockBoundary = CKEDITOR.dom.walker.blockBoundary(
( unit == CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS ) ? { br : 1 } : null ),
// Record the encountered 'blockBoundary' for later use.
boundaryGuard = function( node )
{
var retval = notBlockBoundary( node );
if ( !retval )
blockBoundary = node;
return retval;
},
// Record the encounted 'tailBr' for later use.
tailBrGuard = function( node )
{
var retval = boundaryGuard( node );
if ( !retval && node.is && node.is( 'br' ) )
tailBr = node;
return retval;
};
walker.guard = boundaryGuard;
enlargeable = walker.lastBackward();
// It's the body which stop the enlarging if no block boundary found.
blockBoundary = blockBoundary || body;
// Start the range either after the end of found block (<p>...</p>[text)
// or at the start of block (<p>[text...), by comparing the document position
// with 'enlargeable' node.
this.setStartAt(
blockBoundary,
!blockBoundary.is( 'br' ) &&
( !enlargeable && this.checkStartOfBlock()
|| enlargeable && blockBoundary.contains( enlargeable ) ) ?
CKEDITOR.POSITION_AFTER_START :
CKEDITOR.POSITION_AFTER_END );
// Avoid enlarging the range further when end boundary spans right after the BR. (#7490)
if ( unit == CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS )
{
var theRange = this.clone();
walker = new CKEDITOR.dom.walker( theRange );
var whitespaces = CKEDITOR.dom.walker.whitespaces(),
bookmark = CKEDITOR.dom.walker.bookmark();
walker.evaluator = function( node ) { return !whitespaces( node ) && !bookmark( node ); };
var previous = walker.previous();
if ( previous && previous.type == CKEDITOR.NODE_ELEMENT && previous.is( 'br' ) )
return;
}
// Enlarging the end boundary.
walkerRange = this.clone();
walkerRange.collapse();
walkerRange.setEndAt( body, CKEDITOR.POSITION_BEFORE_END );
walker = new CKEDITOR.dom.walker( walkerRange );
// tailBrGuard only used for on range end.
walker.guard = ( unit == CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS ) ?
tailBrGuard : boundaryGuard;
blockBoundary = null;
// End the range right before the block boundary node.
enlargeable = walker.lastForward();
// It's the body which stop the enlarging if no block boundary found.
blockBoundary = blockBoundary || body;
// Close the range either before the found block start (text]<p>...</p>) or at the block end (...text]</p>)
// by comparing the document position with 'enlargeable' node.
this.setEndAt(
blockBoundary,
( !enlargeable && this.checkEndOfBlock()
|| enlargeable && blockBoundary.contains( enlargeable ) ) ?
CKEDITOR.POSITION_BEFORE_END :
CKEDITOR.POSITION_BEFORE_START );
// We must include the <br> at the end of range if there's
// one and we're expanding list item contents
if ( tailBr )
this.setEndAfter( tailBr );
}
},
/**
* Descrease the range to make sure that boundaries
* always anchor beside text nodes or innermost element.
* @param {Number} mode ( CKEDITOR.SHRINK_ELEMENT | CKEDITOR.SHRINK_TEXT ) The shrinking mode.
* <dl>
* <dt>CKEDITOR.SHRINK_ELEMENT</dt>
* <dd>Shrink the range boundaries to the edge of the innermost element.</dd>
* <dt>CKEDITOR.SHRINK_TEXT</dt>
* <dd>Shrink the range boudaries to anchor by the side of enclosed text node, range remains if there's no text nodes on boundaries at all.</dd>
* </dl>
* @param {Boolean} selectContents Whether result range anchors at the inner OR outer boundary of the node.
*/
shrink : function( mode, selectContents )
{
// Unable to shrink a collapsed range.
if ( !this.collapsed )
{
mode = mode || CKEDITOR.SHRINK_TEXT;
var walkerRange = this.clone();
var startContainer = this.startContainer,
endContainer = this.endContainer,
startOffset = this.startOffset,
endOffset = this.endOffset,
collapsed = this.collapsed;
// Whether the start/end boundary is moveable.
var moveStart = 1,
moveEnd = 1;
if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )
{
if ( !startOffset )
walkerRange.setStartBefore( startContainer );
else if ( startOffset >= startContainer.getLength( ) )
walkerRange.setStartAfter( startContainer );
else
{
// Enlarge the range properly to avoid walker making
// DOM changes caused by triming the text nodes later.
walkerRange.setStartBefore( startContainer );
moveStart = 0;
}
}
if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )
{
if ( !endOffset )
walkerRange.setEndBefore( endContainer );
else if ( endOffset >= endContainer.getLength( ) )
walkerRange.setEndAfter( endContainer );
else
{
walkerRange.setEndAfter( endContainer );
moveEnd = 0;
}
}
var walker = new CKEDITOR.dom.walker( walkerRange ),
isBookmark = CKEDITOR.dom.walker.bookmark();
walker.evaluator = function( node )
{
return node.type == ( mode == CKEDITOR.SHRINK_ELEMENT ?
CKEDITOR.NODE_ELEMENT : CKEDITOR.NODE_TEXT );
};
var currentElement;
walker.guard = function( node, movingOut )
{
if ( isBookmark( node ) )
return true;
// Stop when we're shrink in element mode while encountering a text node.
if ( mode == CKEDITOR.SHRINK_ELEMENT && node.type == CKEDITOR.NODE_TEXT )
return false;
// Stop when we've already walked "through" an element.
if ( movingOut && node.equals( currentElement ) )
return false;
if ( !movingOut && node.type == CKEDITOR.NODE_ELEMENT )
currentElement = node;
return true;
};
if ( moveStart )
{
var textStart = walker[ mode == CKEDITOR.SHRINK_ELEMENT ? 'lastForward' : 'next']();
textStart && this.setStartAt( textStart, selectContents ? CKEDITOR.POSITION_AFTER_START : CKEDITOR.POSITION_BEFORE_START );
}
if ( moveEnd )
{
walker.reset();
var textEnd = walker[ mode == CKEDITOR.SHRINK_ELEMENT ? 'lastBackward' : 'previous']();
textEnd && this.setEndAt( textEnd, selectContents ? CKEDITOR.POSITION_BEFORE_END : CKEDITOR.POSITION_AFTER_END );
}
return !!( moveStart || moveEnd );
}
},
/**
* Inserts a node at the start of the range. The range will be expanded
* the contain the node.
*/
insertNode : function( node )
{
this.optimizeBookmark();
this.trim( false, true );
var startContainer = this.startContainer;
var startOffset = this.startOffset;
var nextNode = startContainer.getChild( startOffset );
if ( nextNode )
node.insertBefore( nextNode );
else
startContainer.append( node );
// Check if we need to update the end boundary.
if ( node.getParent().equals( this.endContainer ) )
this.endOffset++;
// Expand the range to embrace the new node.
this.setStartBefore( node );
},
moveToPosition : function( node, position )
{
this.setStartAt( node, position );
this.collapse( true );
},
selectNodeContents : function( node )
{
this.setStart( node, 0 );
this.setEnd( node, node.type == CKEDITOR.NODE_TEXT ? node.getLength() : node.getChildCount() );
},
/**
* Sets the start position of a Range.
* @param {CKEDITOR.dom.node} startNode The node to start the range.
* @param {Number} startOffset An integer greater than or equal to zero
* representing the offset for the start of the range from the start
* of startNode.
*/
setStart : function( startNode, startOffset )
{
// W3C requires a check for the new position. If it is after the end
// boundary, the range should be collapsed to the new start. It seams
// we will not need this check for our use of this class so we can
// ignore it for now.
// Fixing invalid range start inside dtd empty elements.
if( startNode.type == CKEDITOR.NODE_ELEMENT
&& CKEDITOR.dtd.$empty[ startNode.getName() ] )
startOffset = startNode.getIndex(), startNode = startNode.getParent();
this.startContainer = startNode;
this.startOffset = startOffset;
if ( !this.endContainer )
{
this.endContainer = startNode;
this.endOffset = startOffset;
}
updateCollapsed( this );
},
/**
* Sets the end position of a Range.
* @param {CKEDITOR.dom.node} endNode The node to end the range.
* @param {Number} endOffset An integer greater than or equal to zero
* representing the offset for the end of the range from the start
* of endNode.
*/
setEnd : function( endNode, endOffset )
{
// W3C requires a check for the new position. If it is before the start
// boundary, the range should be collapsed to the new end. It seams we
// will not need this check for our use of this class so we can ignore
// it for now.
// Fixing invalid range end inside dtd empty elements.
if( endNode.type == CKEDITOR.NODE_ELEMENT
&& CKEDITOR.dtd.$empty[ endNode.getName() ] )
endOffset = endNode.getIndex() + 1, endNode = endNode.getParent();
this.endContainer = endNode;
this.endOffset = endOffset;
if ( !this.startContainer )
{
this.startContainer = endNode;
this.startOffset = endOffset;
}
updateCollapsed( this );
},
setStartAfter : function( node )
{
this.setStart( node.getParent(), node.getIndex() + 1 );
},
setStartBefore : function( node )
{
this.setStart( node.getParent(), node.getIndex() );
},
setEndAfter : function( node )
{
this.setEnd( node.getParent(), node.getIndex() + 1 );
},
setEndBefore : function( node )
{
this.setEnd( node.getParent(), node.getIndex() );
},
setStartAt : function( node, position )
{
switch( position )
{
case CKEDITOR.POSITION_AFTER_START :
this.setStart( node, 0 );
break;
case CKEDITOR.POSITION_BEFORE_END :
if ( node.type == CKEDITOR.NODE_TEXT )
this.setStart( node, node.getLength() );
else
this.setStart( node, node.getChildCount() );
break;
case CKEDITOR.POSITION_BEFORE_START :
this.setStartBefore( node );
break;
case CKEDITOR.POSITION_AFTER_END :
this.setStartAfter( node );
}
updateCollapsed( this );
},
setEndAt : function( node, position )
{
switch( position )
{
case CKEDITOR.POSITION_AFTER_START :
this.setEnd( node, 0 );
break;
case CKEDITOR.POSITION_BEFORE_END :
if ( node.type == CKEDITOR.NODE_TEXT )
this.setEnd( node, node.getLength() );
else
this.setEnd( node, node.getChildCount() );
break;
case CKEDITOR.POSITION_BEFORE_START :
this.setEndBefore( node );
break;
case CKEDITOR.POSITION_AFTER_END :
this.setEndAfter( node );
}
updateCollapsed( this );
},
fixBlock : function( isStart, blockTag )
{
var bookmark = this.createBookmark(),
fixedBlock = this.document.createElement( blockTag );
this.collapse( isStart );
this.enlarge( CKEDITOR.ENLARGE_BLOCK_CONTENTS );
this.extractContents().appendTo( fixedBlock );
fixedBlock.trim();
if ( !CKEDITOR.env.ie )
fixedBlock.appendBogus();
this.insertNode( fixedBlock );
this.moveToBookmark( bookmark );
return fixedBlock;
},
splitBlock : function( blockTag )
{
var startPath = new CKEDITOR.dom.elementPath( this.startContainer ),
endPath = new CKEDITOR.dom.elementPath( this.endContainer );
var startBlockLimit = startPath.blockLimit,
endBlockLimit = endPath.blockLimit;
var startBlock = startPath.block,
endBlock = endPath.block;
var elementPath = null;
// Do nothing if the boundaries are in different block limits.
if ( !startBlockLimit.equals( endBlockLimit ) )
return null;
// Get or fix current blocks.
if ( blockTag != 'br' )
{
if ( !startBlock )
{
startBlock = this.fixBlock( true, blockTag );
endBlock = new CKEDITOR.dom.elementPath( this.endContainer ).block;
}
if ( !endBlock )
endBlock = this.fixBlock( false, blockTag );
}
// Get the range position.
var isStartOfBlock = startBlock && this.checkStartOfBlock(),
isEndOfBlock = endBlock && this.checkEndOfBlock();
// Delete the current contents.
// TODO: Why is 2.x doing CheckIsEmpty()?
this.deleteContents();
if ( startBlock && startBlock.equals( endBlock ) )
{
if ( isEndOfBlock )
{
elementPath = new CKEDITOR.dom.elementPath( this.startContainer );
this.moveToPosition( endBlock, CKEDITOR.POSITION_AFTER_END );
endBlock = null;
}
else if ( isStartOfBlock )
{
elementPath = new CKEDITOR.dom.elementPath( this.startContainer );
this.moveToPosition( startBlock, CKEDITOR.POSITION_BEFORE_START );
startBlock = null;
}
else
{
endBlock = this.splitElement( startBlock );
// In Gecko, the last child node must be a bogus <br>.
// Note: bogus <br> added under <ul> or <ol> would cause
// lists to be incorrectly rendered.
if ( !CKEDITOR.env.ie && !startBlock.is( 'ul', 'ol') )
startBlock.appendBogus() ;
}
}
return {
previousBlock : startBlock,
nextBlock : endBlock,
wasStartOfBlock : isStartOfBlock,
wasEndOfBlock : isEndOfBlock,
elementPath : elementPath
};
},
/**
* Branch the specified element from the collapsed range position and
* place the caret between the two result branches.
* Note: The range must be collapsed and been enclosed by this element.
* @param {CKEDITOR.dom.element} element
* @return {CKEDITOR.dom.element} Root element of the new branch after the split.
*/
splitElement : function( toSplit )
{
if ( !this.collapsed )
return null;
// Extract the contents of the block from the selection point to the end
// of its contents.
this.setEndAt( toSplit, CKEDITOR.POSITION_BEFORE_END );
var documentFragment = this.extractContents();
// Duplicate the element after it.
var clone = toSplit.clone( false );
// Place the extracted contents into the duplicated element.
documentFragment.appendTo( clone );
clone.insertAfter( toSplit );
this.moveToPosition( toSplit, CKEDITOR.POSITION_AFTER_END );
return clone;
},
/**
* Check whether a range boundary is at the inner boundary of a given
* element.
* @param {CKEDITOR.dom.element} element The target element to check.
* @param {Number} checkType The boundary to check for both the range
* and the element. It can be CKEDITOR.START or CKEDITOR.END.
* @returns {Boolean} "true" if the range boundary is at the inner
* boundary of the element.
*/
checkBoundaryOfElement : function( element, checkType )
{
var checkStart = ( checkType == CKEDITOR.START );
// Create a copy of this range, so we can manipulate it for our checks.
var walkerRange = this.clone();
// Collapse the range at the proper size.
walkerRange.collapse( checkStart );
// Expand the range to element boundary.
walkerRange[ checkStart ? 'setStartAt' : 'setEndAt' ]
( element, checkStart ? CKEDITOR.POSITION_AFTER_START : CKEDITOR.POSITION_BEFORE_END );
// Create the walker, which will check if we have anything useful
// in the range.
var walker = new CKEDITOR.dom.walker( walkerRange );
walker.evaluator = elementBoundaryEval( checkStart );
return walker[ checkStart ? 'checkBackward' : 'checkForward' ]();
},
// Calls to this function may produce changes to the DOM. The range may
// be updated to reflect such changes.
checkStartOfBlock : function()
{
var startContainer = this.startContainer,
startOffset = this.startOffset;
// If the starting node is a text node, and non-empty before the offset,
// then we're surely not at the start of block.
if ( startOffset && startContainer.type == CKEDITOR.NODE_TEXT )
{
var textBefore = CKEDITOR.tools.ltrim( startContainer.substring( 0, startOffset ) );
if ( textBefore.length )
return false;
}
// We need to grab the block element holding the start boundary, so
// let's use an element path for it.
var path = new CKEDITOR.dom.elementPath( this.startContainer );
// Creates a range starting at the block start until the range start.
var walkerRange = this.clone();
walkerRange.collapse( true );
walkerRange.setStartAt( path.block || path.blockLimit, CKEDITOR.POSITION_AFTER_START );
var walker = new CKEDITOR.dom.walker( walkerRange );
walker.evaluator = getCheckStartEndBlockEvalFunction( true );
return walker.checkBackward();
},
checkEndOfBlock : function()
{
var endContainer = this.endContainer,
endOffset = this.endOffset;
// If the ending node is a text node, and non-empty after the offset,
// then we're surely not at the end of block.
if ( endContainer.type == CKEDITOR.NODE_TEXT )
{
var textAfter = CKEDITOR.tools.rtrim( endContainer.substring( endOffset ) );
if ( textAfter.length )
return false;
}
// We need to grab the block element holding the start boundary, so
// let's use an element path for it.
var path = new CKEDITOR.dom.elementPath( this.endContainer );
// Creates a range starting at the block start until the range start.
var walkerRange = this.clone();
walkerRange.collapse( false );
walkerRange.setEndAt( path.block || path.blockLimit, CKEDITOR.POSITION_BEFORE_END );
var walker = new CKEDITOR.dom.walker( walkerRange );
walker.evaluator = getCheckStartEndBlockEvalFunction( false );
return walker.checkForward();
},
/**
* Check if elements at which the range boundaries anchor are read-only,
* with respect to "contenteditable" attribute.
*/
checkReadOnly : ( function()
{
function checkNodesEditable( node, anotherEnd )
{
while( node )
{
if ( node.type == CKEDITOR.NODE_ELEMENT )
{
if ( node.getAttribute( 'contentEditable' ) == 'false'
&& !node.data( 'cke-editable' ) )
{
return 0;
}
// Range enclosed entirely in an editable element.
else if ( node.is( 'html' )
|| node.getAttribute( 'contentEditable' ) == 'true'
&& ( node.contains( anotherEnd ) || node.equals( anotherEnd ) ) )
{
break;
}
}
node = node.getParent();
}
return 1;
}
return function()
{
var startNode = this.startContainer,
endNode = this.endContainer;
// Check if elements path at both boundaries are editable.
return !( checkNodesEditable( startNode, endNode ) && checkNodesEditable( endNode, startNode ) );
};
})(),
/**
* Moves the range boundaries to the first/end editing point inside an
* element. For example, in an element tree like
* "<p><b><i></i></b> Text</p>", the start editing point is
* "<p><b><i>^</i></b> Text</p>" (inside <i>).
* @param {CKEDITOR.dom.element} el The element into which look for the
* editing spot.
* @param {Boolean} isMoveToEnd Whether move to the end editable position.
*/
moveToElementEditablePosition : function( el, isMoveToEnd )
{
var nbspRegExp = /^[\t\r\n ]*(?: |\xa0)$/;
function nextDFS( node, childOnly )
{
var next;
if ( node.type == CKEDITOR.NODE_ELEMENT && node.isEditable( false ) )
next = node[ isMoveToEnd ? 'getLast' : 'getFirst' ]( nonWhitespaceOrBookmarkEval );
if ( !childOnly && !next )
next = node[ isMoveToEnd ? 'getPrevious' : 'getNext' ]( nonWhitespaceOrBookmarkEval );
return next;
}
// Handle non-editable element e.g. HR.
if ( el.type == CKEDITOR.NODE_ELEMENT && !el.isEditable( false ) )
{
this.moveToPosition( el, isMoveToEnd ?
CKEDITOR.POSITION_AFTER_END :
CKEDITOR.POSITION_BEFORE_START );
return true;
}
var found = 0;
while ( el )
{
// Stop immediately if we've found a text node.
if ( el.type == CKEDITOR.NODE_TEXT )
{
// Put cursor before block filler.
if ( isMoveToEnd && this.checkEndOfBlock() && nbspRegExp.test( el.getText() ) )
this.moveToPosition( el, CKEDITOR.POSITION_BEFORE_START );
else
this.moveToPosition( el, isMoveToEnd ?
CKEDITOR.POSITION_AFTER_END :
CKEDITOR.POSITION_BEFORE_START );
found = 1;
break;
}
// If an editable element is found, move inside it, but not stop the searching.
if ( el.type == CKEDITOR.NODE_ELEMENT )
{
if ( el.isEditable() )
{
this.moveToPosition( el, isMoveToEnd ?
CKEDITOR.POSITION_BEFORE_END :
CKEDITOR.POSITION_AFTER_START );
found = 1;
}
// Put cursor before padding block br.
else if ( isMoveToEnd && el.is( 'br' ) && this.checkEndOfBlock() )
this.moveToPosition( el, CKEDITOR.POSITION_BEFORE_START );
}
el = nextDFS( el, found );
}
return !!found;
},
/**
*@see {CKEDITOR.dom.range.moveToElementEditablePosition}
*/
moveToElementEditStart : function( target )
{
return this.moveToElementEditablePosition( target );
},
/**
*@see {CKEDITOR.dom.range.moveToElementEditablePosition}
*/
moveToElementEditEnd : function( target )
{
return this.moveToElementEditablePosition( target, true );
},
/**
* Get the single node enclosed within the range if there's one.
*/
getEnclosedNode : function()
{
var walkerRange = this.clone();
// Optimize and analyze the range to avoid DOM destructive nature of walker. (#5780)
walkerRange.optimize();
if ( walkerRange.startContainer.type != CKEDITOR.NODE_ELEMENT
|| walkerRange.endContainer.type != CKEDITOR.NODE_ELEMENT )
return null;
var walker = new CKEDITOR.dom.walker( walkerRange ),
isNotBookmarks = CKEDITOR.dom.walker.bookmark( true ),
isNotWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),
evaluator = function( node )
{
return isNotWhitespaces( node ) && isNotBookmarks( node );
};
walkerRange.evaluator = evaluator;
var node = walker.next();
walker.reset();
return node && node.equals( walker.previous() ) ? node : null;
},
getTouchedStartNode : function()
{
var container = this.startContainer ;
if ( this.collapsed || container.type != CKEDITOR.NODE_ELEMENT )
return container ;
return container.getChild( this.startOffset ) || container ;
},
getTouchedEndNode : function()
{
var container = this.endContainer ;
if ( this.collapsed || container.type != CKEDITOR.NODE_ELEMENT )
return container ;
return container.getChild( this.endOffset - 1 ) || container ;
}
};
})();
CKEDITOR.POSITION_AFTER_START = 1; // <element>^contents</element> "^text"
CKEDITOR.POSITION_BEFORE_END = 2; // <element>contents^</element> "text^"
CKEDITOR.POSITION_BEFORE_START = 3; // ^<element>contents</element> ^"text"
CKEDITOR.POSITION_AFTER_END = 4; // <element>contents</element>^ "text"
CKEDITOR.ENLARGE_ELEMENT = 1;
CKEDITOR.ENLARGE_BLOCK_CONTENTS = 2;
CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS = 3;
// Check boundary types.
// @see CKEDITOR.dom.range.prototype.checkBoundaryOfElement
CKEDITOR.START = 1;
CKEDITOR.END = 2;
CKEDITOR.STARTEND = 3;
// Shrink range types.
// @see CKEDITOR.dom.range.prototype.shrink
CKEDITOR.SHRINK_ELEMENT = 1;
CKEDITOR.SHRINK_TEXT = 2;