/
default.js
1934 lines (1655 loc) · 60.5 KB
/
default.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
/**
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/* globals CKEDITOR */
( function() {
'use strict';
var tools = CKEDITOR.tools,
pastetools = CKEDITOR.plugins.pastetools,
commonFilter = pastetools.filters.common,
Style = commonFilter.styles,
createAttributeStack = commonFilter.createAttributeStack,
getElementIndentation = commonFilter.lists.getElementIndentation,
invalidTags = [
'o:p',
'xml',
'script',
'meta',
'link'
],
shapeTags = [
'v:arc',
'v:curve',
'v:line',
'v:oval',
'v:polyline',
'v:rect',
'v:roundrect',
'v:group'
],
links = {},
inComment = 0,
plug = {},
List,
Heuristics;
/**
* Set of Paste from Word plugin helpers.
*
* @since 4.13.0
* @private
* @member CKEDITOR.plugins.pastetools.filters
*/
CKEDITOR.plugins.pastetools.filters.word = plug;
/**
* Set of Paste from Word plugin helpers.
*
* See {@link CKEDITOR.plugins.pastetools.filters.word}.
*
* @since 4.6.0
* @deprecated 4.13.0
* @private
* @member CKEDITOR.plugins
*/
CKEDITOR.plugins.pastefromword = plug;
/**
* Rules for the Paste from Word filter.
*
* @since 4.13.0
* @private
* @member CKEDITOR.plugins.pastetools.filters.word
*/
plug.rules = function( html, editor, filter ) {
var msoListsDetected = Boolean( html.match( /mso-list:\s*l\d+\s+level\d+\s+lfo\d+/ ) ),
shapesIds = [],
rules = {
root: function( element ) {
element.filterChildren( filter );
CKEDITOR.plugins.pastefromword.lists.cleanup( List.createLists( element ) );
},
elementNames: [
[ ( /^\?xml:namespace$/ ), '' ],
[ /^v:shapetype/, '' ],
[ new RegExp( invalidTags.join( '|' ) ), '' ] // Remove invalid tags.
],
elements: {
'a': function( element ) {
// Redundant anchor created by IE8.
if ( element.attributes.name ) {
if ( element.attributes.name == '_GoBack' ) {
delete element.name;
return;
}
// Garbage links that go nowhere.
if ( element.attributes.name.match( /^OLE_LINK\d+$/ ) ) {
delete element.name;
return;
}
}
if ( element.attributes.href && element.attributes.href.match( /#.+$/ ) ) {
var name = element.attributes.href.match( /#(.+)$/ )[ 1 ];
links[ name ] = element;
}
if ( element.attributes.name && links[ element.attributes.name ] ) {
var link = links[ element.attributes.name ];
link.attributes.href = link.attributes.href.replace( /.*#(.*)$/, '#$1' );
}
},
'div': function( element ) {
// Don't allow to delete page break element (#3220).
if ( editor.plugins.pagebreak && element.attributes[ 'data-cke-pagebreak' ] ) {
return element;
}
Style.createStyleStack( element, filter, editor );
},
'img': function( element ) {
// If the parent is DocumentFragment it does not have any attributes. (https://dev.ckeditor.com/ticket/16912)
if ( element.parent && element.parent.attributes ) {
var attrs = element.parent.attributes,
style = attrs.style || attrs.STYLE;
if ( style && style.match( /mso\-list:\s?Ignore/ ) ) {
element.attributes[ 'cke-ignored' ] = true;
}
}
Style.mapCommonStyles( element );
if ( element.attributes.src && element.attributes.src.match( /^file:\/\// ) &&
element.attributes.alt && element.attributes.alt.match( /^https?:\/\// ) ) {
element.attributes.src = element.attributes.alt;
}
var imgShapesIds = element.attributes[ 'v:shapes' ] ? element.attributes[ 'v:shapes' ].split( ' ' ) : [];
// Check whether attribute contains shapes recognised earlier (stored in global list of shapesIds).
// If so, add additional data-attribute to img tag.
var isShapeFromList = CKEDITOR.tools.array.every( imgShapesIds, function( shapeId ) {
return shapesIds.indexOf( shapeId ) > -1;
} );
if ( imgShapesIds.length && isShapeFromList ) {
// As we don't know how to process shapes we can remove them.
return false;
}
},
'p': function( element ) {
element.filterChildren( filter );
if ( element.attributes.style && element.attributes.style.match( /display:\s*none/i ) ) {
return false;
}
if ( List.thisIsAListItem( editor, element ) ) {
if ( Heuristics.isEdgeListItem( editor, element ) ) {
Heuristics.cleanupEdgeListItem( element );
}
List.convertToFakeListItem( editor, element );
// IE pastes nested paragraphs in list items, which is different from other browsers. (https://dev.ckeditor.com/ticket/16826)
// There's a possibility that list item will contain multiple paragraphs, in that case we want
// to split them with BR.
tools.array.reduce( element.children, function( paragraphsReplaced, node ) {
if ( node.name === 'p' ) {
// If there were already paragraphs replaced, put a br before this paragraph, so that
// it's inline children are displayed in a next line.
if ( paragraphsReplaced > 0 ) {
var br = new CKEDITOR.htmlParser.element( 'br' );
br.insertBefore( node );
}
node.replaceWithChildren();
paragraphsReplaced += 1;
}
return paragraphsReplaced;
}, 0 );
} else {
// In IE list level information is stored in <p> elements inside <li> elements.
var container = element.getAscendant( function( element ) {
return element.name == 'ul' || element.name == 'ol';
} ),
style = tools.parseCssText( element.attributes.style );
if ( container &&
!container.attributes[ 'cke-list-level' ] &&
style[ 'mso-list' ] &&
style[ 'mso-list' ].match( /level/ ) ) {
container.attributes[ 'cke-list-level' ] = style[ 'mso-list' ].match( /level(\d+)/ )[1];
}
// Adapt paragraph formatting to editor's convention according to enter-mode (#423).
if ( editor.config.enterMode == CKEDITOR.ENTER_BR ) {
// We suffer from attribute/style lost in this situation.
delete element.name;
element.add( new CKEDITOR.htmlParser.element( 'br' ) );
}
}
Style.createStyleStack( element, filter, editor );
},
'pre': function( element ) {
if ( List.thisIsAListItem( editor, element ) ) List.convertToFakeListItem( editor, element );
Style.createStyleStack( element, filter, editor );
},
'h1': function( element ) {
if ( List.thisIsAListItem( editor, element ) ) List.convertToFakeListItem( editor, element );
Style.createStyleStack( element, filter, editor );
},
'h2': function( element ) {
if ( List.thisIsAListItem( editor, element ) ) List.convertToFakeListItem( editor, element );
Style.createStyleStack( element, filter, editor );
},
'h3': function( element ) {
if ( List.thisIsAListItem( editor, element ) ) List.convertToFakeListItem( editor, element );
Style.createStyleStack( element, filter, editor );
},
'h4': function( element ) {
if ( List.thisIsAListItem( editor, element ) ) List.convertToFakeListItem( editor, element );
Style.createStyleStack( element, filter, editor );
},
'h5': function( element ) {
if ( List.thisIsAListItem( editor, element ) ) List.convertToFakeListItem( editor, element );
Style.createStyleStack( element, filter, editor );
},
'h6': function( element ) {
if ( List.thisIsAListItem( editor, element ) ) List.convertToFakeListItem( editor, element );
Style.createStyleStack( element, filter, editor );
},
'font': function( element ) {
if ( element.getHtml().match( /^\s*$/ ) ) {
// There might be font tag directly in document fragment, we cannot replace it with a textnode as this generates
// superfluous spaces in output. What later might be transformed into empty paragraphs, so just remove such element.
if ( element.parent.type === CKEDITOR.NODE_ELEMENT ) {
new CKEDITOR.htmlParser.text( ' ' ).insertAfter( element );
}
return false;
}
if ( editor && editor.config.pasteFromWordRemoveFontStyles === true && element.attributes.size ) {
// font[size] are still used by old IEs for font size.
delete element.attributes.size;
}
// Create style stack for td/th > font if only class
// and style attributes are present. Such markup is produced by Excel.
if ( CKEDITOR.dtd.tr[ element.parent.name ] &&
CKEDITOR.tools.arrayCompare( CKEDITOR.tools.object.keys( element.attributes ), [ 'class', 'style' ] ) ) {
Style.createStyleStack( element, filter, editor );
} else {
createAttributeStack( element, filter );
}
},
'ul': function( element ) {
if ( !msoListsDetected ) {
// List should only be processed if we're sure we're working with Word. (https://dev.ckeditor.com/ticket/16593)
return;
}
// Edge case from 11683 - an unusual way to create a level 2 list.
if ( element.parent.name == 'li' && tools.indexOf( element.parent.children, element ) === 0 ) {
Style.setStyle( element.parent, 'list-style-type', 'none' );
}
List.dissolveList( element );
return false;
},
'li': function( element ) {
Heuristics.correctLevelShift( element );
if ( !msoListsDetected ) {
return;
}
element.attributes.style = Style.normalizedStyles( element, editor );
Style.pushStylesLower( element );
},
'ol': function( element ) {
if ( !msoListsDetected ) {
// List should only be processed if we're sure we're working with Word. (https://dev.ckeditor.com/ticket/16593)
return;
}
// Fix edge-case where when a list skips a level in IE11, the <ol> element
// is implicitly surrounded by a <li>.
if ( element.parent.name == 'li' && tools.indexOf( element.parent.children, element ) === 0 ) {
Style.setStyle( element.parent, 'list-style-type', 'none' );
}
List.dissolveList( element );
return false;
},
'span': function( element ) {
element.filterChildren( filter );
element.attributes.style = Style.normalizedStyles( element, editor );
if ( !element.attributes.style ||
// Remove garbage bookmarks that disrupt the content structure.
element.attributes.style.match( /^mso\-bookmark:OLE_LINK\d+$/ ) ||
element.getHtml().match( /^(\s| )+$/ ) ) {
commonFilter.elements.replaceWithChildren( element );
return false;
}
if ( element.attributes.style.match( /FONT-FAMILY:\s*Symbol/i ) ) {
element.forEach( function( node ) {
node.value = node.value.replace( / /g, '' );
}, CKEDITOR.NODE_TEXT, true );
}
Style.createStyleStack( element, filter, editor );
},
'v:imagedata': remove,
// This is how IE8 presents images.
'v:shape': function( element ) {
// There are 3 paths:
// 1. There is regular `v:shape` (no `v:imagedata` inside).
// 2. There is a simple situation with `v:shape` with `v:imagedata` inside. We can remove such element and rely on `img` tag found later on.
// 3. There is a complicated situation where we cannot find proper `img` tag after `v:shape` or there is some canvas element.
// a) If shape is a child of v:group, then most probably it belongs to canvas, so we need to treat it as in path 1.
// b) In other cases, most probably there is no related `img` tag. We need to transform `v:shape` into `img` tag (IE8 integration).
var duplicate = false,
child = element.getFirst( 'v:imagedata' );
// Path 1:
if ( child === null ) {
shapeTagging( element );
return;
}
// Path 2:
// Sometimes a child with proper ID might be nested in other tag.
element.parent.find( function( child ) {
if ( child.name == 'img' && child.attributes &&
child.attributes[ 'v:shapes' ] == element.attributes.id ) {
duplicate = true;
}
}, true );
if ( duplicate ) {
return false;
} else {
// Path 3:
var src = '';
// 3.a) Filter out situation when canvas is used. In such scenario there is v:group containing v:shape containing v:imagedata.
// We streat such v:shapes as in Path 1.
if ( element.parent.name === 'v:group' ) {
shapeTagging( element );
return;
}
// 3.b) Most probably there is no img tag later on, so we need to transform this v:shape into img. This should only happen on IE8.
element.forEach( function( child ) {
if ( child.attributes && child.attributes.src ) {
src = child.attributes.src;
}
}, CKEDITOR.NODE_ELEMENT, true );
element.filterChildren( filter );
element.name = 'img';
element.attributes.src = element.attributes.src || src;
delete element.attributes.type;
}
return;
},
'style': function() {
// We don't want to let any styles in. Firefox tends to add some.
return false;
},
'object': function( element ) {
// The specs about object `data` attribute:
// Address of the resource as a valid URL. At least one of data and type must be defined.
// If there is not `data`, skip the object element. (https://dev.ckeditor.com/ticket/17001)
return !!( element.attributes && element.attributes.data );
},
// Integrate page breaks with `pagebreak` plugin (#2598).
'br': function( element ) {
if ( !editor.plugins.pagebreak ) {
return;
}
var styles = tools.parseCssText( element.attributes.style, true );
// Safari uses `break-before` instead of `page-break-before` to recognize page breaks.
if ( styles[ 'page-break-before' ] === 'always' || styles[ 'break-before' ] === 'page' ) {
var pagebreakEl = CKEDITOR.plugins.pagebreak.createElement( editor );
return CKEDITOR.htmlParser.fragment.fromHtml( pagebreakEl.getOuterHtml() ).children[ 0 ];
}
}
},
attributes: {
'style': function( styles, element ) {
// Returning false deletes the attribute.
return Style.normalizedStyles( element, editor ) || false;
},
'class': function( classes ) {
// The (el\d+)|(font\d+) are default Excel classes for table cells and text.
return falseIfEmpty( classes.replace( /(el\d+)|(font\d+)|msonormal|msolistparagraph\w*/ig, '' ) );
},
'cellspacing': remove,
'cellpadding': remove,
'border': remove,
'v:shapes': remove,
'o:spid': remove
},
comment: function( element ) {
if ( element.match( /\[if.* supportFields.*\]/ ) ) {
inComment++;
}
if ( element == '[endif]' ) {
inComment = inComment > 0 ? inComment - 1 : 0;
}
return false;
},
text: function( content, node ) {
if ( inComment ) {
return '';
}
var grandparent = node.parent && node.parent.parent;
if ( grandparent && grandparent.attributes && grandparent.attributes.style && grandparent.attributes.style.match( /mso-list:\s*ignore/i ) ) {
return content.replace( / /g, ' ' );
}
return content;
}
};
tools.array.forEach( shapeTags, function( shapeTag ) {
rules.elements[ shapeTag ] = shapeTagging;
} );
return rules;
function shapeTagging( element ) {
// Check if regular or canvas shape (#1088).
if ( element.attributes[ 'o:gfxdata' ] || element.parent.name === 'v:group' ) {
shapesIds.push( element.attributes.id );
}
}
};
/**
* Namespace containing list-oriented helper methods.
*
* @private
* @since 4.13.0
* @member CKEDITOR.plugins.pastetools.filters.word
*/
plug.lists = {
/**
* Checks if a given element is a list item-alike.
*
* @private
* @since 4.13.0
* @param {CKEDITOR.editor} editor
* @param {CKEDITOR.htmlParser.element} element
* @returns {Boolean}
* @member CKEDITOR.plugins.pastetools.filters.word.lists
*/
thisIsAListItem: function( editor, element ) {
if ( Heuristics.isEdgeListItem( editor, element ) ) {
return true;
}
/*jshint -W024 */
// Normally a style of the sort that looks like "mso-list: l0 level1 lfo1"
// indicates a list element, but the same style may appear in a <p> that's within a <li>.
if ( ( element.attributes.style && element.attributes.style.match( /mso\-list:\s?l\d/ ) &&
element.parent.name !== 'li' ) ||
element.attributes[ 'cke-dissolved' ] ||
element.getHtml().match( /<!\-\-\[if !supportLists]\-\->/ )
) {
return true;
}
return false;
/*jshint +W024 */
},
/**
* Converts an element to an element with the `cke:li` tag name.
*
* @private
* @since 4.13.0
* @param {CKEDITOR.editor} editor
* @param {CKEDITOR.htmlParser.element} element
* @member CKEDITOR.plugins.pastetools.filters.word.lists
*/
convertToFakeListItem: function( editor, element ) {
if ( Heuristics.isDegenerateListItem( editor, element ) ) {
Heuristics.assignListLevels( editor, element );
}
// A dummy call to cache parsed list info inside of cke-list-* attributes.
this.getListItemInfo( element );
if ( !element.attributes[ 'cke-dissolved' ] ) {
// The symbol is usually the first text node descendant
// of the element that doesn't start with a whitespace character;
var symbol;
element.forEach( function( element ) {
// Sometimes there are custom markers represented as images.
// They can be recognized by the distinctive alt attribute value.
if ( !symbol && element.name == 'img' &&
element.attributes[ 'cke-ignored' ] &&
element.attributes.alt == '*' ) {
symbol = '·';
// Remove the "symbol" now, since it's the best opportunity to do so.
element.remove();
}
}, CKEDITOR.NODE_ELEMENT );
element.forEach( function( element ) {
if ( !symbol && !element.value.match( /^ / ) ) {
symbol = element.value;
}
}, CKEDITOR.NODE_TEXT );
// Without a symbol this isn't really a list item.
if ( typeof symbol == 'undefined' ) {
return;
}
element.attributes[ 'cke-symbol' ] = symbol.replace( /(?: | ).*$/, '' );
List.removeSymbolText( element );
}
var styles = element.attributes && tools.parseCssText( element.attributes.style );
// Default list has 40px padding. To correct indentation we need to reduce margin-left by 40px for each list level.
// Additionally margin has to be reduced by sum of margins of each parent, however it can't be done until list are structured in a tree (#2870).
// Note margin left is absent in IE pasted content.
if ( styles[ 'margin-left' ] ) {
var margin = styles[ 'margin-left' ],
level = element.attributes[ 'cke-list-level' ];
// Ignore negative margins (#2870).
margin = Math.max( CKEDITOR.tools.convertToPx( margin ) - 40 * level, 0 );
if ( margin ) {
styles[ 'margin-left' ] = margin + 'px';
} else {
delete styles[ 'margin-left' ];
}
element.attributes.style = CKEDITOR.tools.writeCssText( styles );
}
// Converting to a normal list item would implicitly wrap the element around an <ul>.
element.name = 'cke:li';
},
/**
* Converts any fake list items contained within `root` into real `<li>` elements.
*
* @private
* @since 4.13.0
* @param {CKEDITOR.htmlParser.element} root
* @returns {CKEDITOR.htmlParser.element[]} An array of converted elements.
* @member CKEDITOR.plugins.pastetools.filters.word.lists
*/
convertToRealListItems: function( root ) {
var listElements = [];
// Select and clean up list elements.
root.forEach( function( element ) {
if ( element.name == 'cke:li' ) {
element.name = 'li';
listElements.push( element );
}
}, CKEDITOR.NODE_ELEMENT, false );
return listElements;
},
removeSymbolText: function( element ) { // ...from a list element.
var symbol = element.attributes[ 'cke-symbol' ],
// Find the first element which contains symbol to be replaced (#2690).
node = element.findOne( function( node ) {
// Since symbol may contains special characters we use `indexOf` (instead of RegExp) which is sufficient (#877).
return node.value && node.value.indexOf( symbol ) > -1;
}, true ),
parent;
if ( node ) {
node.value = node.value.replace( symbol, '' );
parent = node.parent;
if ( parent.getHtml().match( /^(\s| )*$/ ) && parent !== element ) {
parent.remove();
} else if ( !node.value ) {
node.remove();
}
}
},
setListSymbol: function( list, symbol, level ) {
level = level || 1;
var style = tools.parseCssText( list.attributes.style );
if ( list.name == 'ol' ) {
if ( list.attributes.type || style[ 'list-style-type' ] ) return;
var typeMap = {
'[ivx]': 'lower-roman',
'[IVX]': 'upper-roman',
'[a-z]': 'lower-alpha',
'[A-Z]': 'upper-alpha',
'\\d': 'decimal'
};
for ( var type in typeMap ) {
if ( List.getSubsectionSymbol( symbol ).match( new RegExp( type ) ) ) {
style[ 'list-style-type' ] = typeMap[ type ];
break;
}
}
list.attributes[ 'cke-list-style-type' ] = style[ 'list-style-type' ];
} else {
var symbolMap = {
'·': 'disc',
'o': 'circle',
'§': 'square' // In Word this is a square.
};
if ( !style[ 'list-style-type' ] && symbolMap[ symbol ] ) {
style[ 'list-style-type' ] = symbolMap[ symbol ];
}
}
List.setListSymbol.removeRedundancies( style, level );
( list.attributes.style = CKEDITOR.tools.writeCssText( style ) ) || delete list.attributes.style;
},
setListStart: function( list ) {
var symbols = [],
offset = 0;
for ( var i = 0; i < list.children.length; i++ ) {
symbols.push( list.children[ i ].attributes[ 'cke-symbol' ] || '' );
}
// When a list starts with a sublist, use the next element as a start indicator.
if ( !symbols[ 0 ] ) {
offset++;
}
// Attribute set in setListSymbol()
switch ( list.attributes[ 'cke-list-style-type' ] ) {
case 'lower-roman':
case 'upper-roman':
list.attributes.start = List.toArabic( List.getSubsectionSymbol( symbols[ offset ] ) ) - offset;
break;
case 'lower-alpha':
case 'upper-alpha':
list.attributes.start = List.getSubsectionSymbol( symbols[ offset ] ).replace( /\W/g, '' ).toLowerCase().charCodeAt( 0 ) - 96 - offset;
break;
case 'decimal':
list.attributes.start = ( parseInt( List.getSubsectionSymbol( symbols[ offset ] ), 10 ) - offset ) || 1;
break;
}
if ( list.attributes.start == '1' ) {
delete list.attributes.start;
}
delete list.attributes[ 'cke-list-style-type' ];
},
/**
* Numbering helper.
*
* @since 4.13.0
* @member CKEDITOR.plugins.pastetools.filters.word.lists
*/
numbering: {
/**
* Converts the list marker value into a decimal number.
*
* var toNumber = CKEDITOR.plugins.pastefromword.lists.numbering.toNumber;
*
* console.log( toNumber( 'XIV', 'upper-roman' ) ); // Logs 14.
* console.log( toNumber( 'd', 'lower-alpha' ) ); // Logs 4.
* console.log( toNumber( '35', 'decimal' ) ); // Logs 35.
* console.log( toNumber( '404', 'foo' ) ); // Logs 1.
*
* @param {String} marker
* @param {String} markerType Marker type according to CSS `list-style-type` values.
* @returns {Number}
* @member CKEDITOR.plugins.pastetools.filters.word.lists.numbering
*/
toNumber: function( marker, markerType ) {
// Functions copied straight from old PFW implementation, no need to reinvent the wheel.
function fromAlphabet( str ) {
var alpahbets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
str = str.toUpperCase();
var l = alpahbets.length,
retVal = 1;
for ( var x = 1; str.length > 0; x *= l ) {
retVal += alpahbets.indexOf( str.charAt( str.length - 1 ) ) * x;
str = str.substr( 0, str.length - 1 );
}
return retVal;
}
function fromRoman( str ) {
var romans = [
[ 1000, 'M' ],
[ 900, 'CM' ],
[ 500, 'D' ],
[ 400, 'CD' ],
[ 100, 'C' ],
[ 90, 'XC' ],
[ 50, 'L' ],
[ 40, 'XL' ],
[ 10, 'X' ],
[ 9, 'IX' ],
[ 5, 'V' ],
[ 4, 'IV' ],
[ 1, 'I' ]
];
str = str.toUpperCase();
var l = romans.length,
retVal = 0;
for ( var i = 0; i < l; ++i ) {
for ( var j = romans[ i ], k = j[ 1 ].length; str.substr( 0, k ) == j[ 1 ]; str = str.substr( k ) )
retVal += j[ 0 ];
}
return retVal;
}
if ( markerType == 'decimal' ) {
return Number( marker );
} else if ( markerType == 'upper-roman' || markerType == 'lower-roman' ) {
return fromRoman( marker.toUpperCase() );
} else if ( markerType == 'lower-alpha' || markerType == 'upper-alpha' ) {
return fromAlphabet( marker );
} else {
return 1;
}
},
/**
* Returns a list style based on the Word marker content.
*
* var getStyle = CKEDITOR.plugins.pastefromword.lists.numbering.getStyle;
*
* console.log( getStyle( '4' ) ); // Logs: "decimal"
* console.log( getStyle( 'b' ) ); // Logs: "lower-alpha"
* console.log( getStyle( 'P' ) ); // Logs: "upper-alpha"
* console.log( getStyle( 'i' ) ); // Logs: "lower-roman"
* console.log( getStyle( 'X' ) ); // Logs: "upper-roman"
*
*
* **Implementation note:** Characters `c` and `d` are not converted to roman on purpose. It is 100 and 500 respectively, so
* you rarely go with a list up until this point, while it is common to start with `c` and `d` in alpha.
*
* @param {String} marker Marker content retained from Word, e.g. `1`, `7`, `XI`, `b`.
* @returns {String} Resolved marker type.
* @member CKEDITOR.plugins.pastetools.filters.word.lists.numbering
*/
getStyle: function( marker ) {
var typeMap = {
'i': 'lower-roman',
'v': 'lower-roman',
'x': 'lower-roman',
'l': 'lower-roman',
'm': 'lower-roman',
'I': 'upper-roman',
'V': 'upper-roman',
'X': 'upper-roman',
'L': 'upper-roman',
'M': 'upper-roman'
},
firstCharacter = marker.slice( 0, 1 ),
type = typeMap[ firstCharacter ];
if ( !type ) {
type = 'decimal';
if ( firstCharacter.match( /[a-z]/ ) ) {
type = 'lower-alpha';
}
if ( firstCharacter.match( /[A-Z]/ ) ) {
type = 'upper-alpha';
}
}
return type;
}
},
// Taking into account cases like "1.1.2." etc. - get the last element.
getSubsectionSymbol: function( symbol ) {
return ( symbol.match( /([\da-zA-Z]+).?$/ ) || [ 'placeholder', '1' ] )[ 1 ];
},
setListDir: function( list ) {
var dirs = { ltr: 0, rtl: 0 };
list.forEach( function( child ) {
if ( child.name == 'li' ) {
var dir = child.attributes.dir || child.attributes.DIR || '';
if ( dir.toLowerCase() == 'rtl' ) {
dirs.rtl++;
} else {
dirs.ltr++;
}
}
}, CKEDITOR.ELEMENT_NODE );
if ( dirs.rtl > dirs.ltr ) {
list.attributes.dir = 'rtl';
}
},
createList: function( element ) {
// "o" symbolizes a circle in unordered lists.
if ( ( element.attributes[ 'cke-symbol' ].match( /([\da-np-zA-NP-Z]).?/ ) || [] )[ 1 ] ) {
return new CKEDITOR.htmlParser.element( 'ol' );
}
return new CKEDITOR.htmlParser.element( 'ul' );
},
/**
* @private
* @since 4.13.0
* @param {CKEDITOR.htmlParser.element} root An element to be looked through for lists.
* @returns {CKEDITOR.htmlParser.element[]} An array of created list items.
* @member CKEDITOR.plugins.pastetools.filters.word.lists
*/
createLists: function( root ) {
var element, level, i, j,
listElements = List.convertToRealListItems( root );
if ( listElements.length === 0 ) {
return [];
}
// Chop data into continuous lists.
var lists = List.groupLists( listElements );
// Create nested list structures.
for ( i = 0; i < lists.length; i++ ) {
var list = lists[ i ],
firstLevel1Element = list[ 0 ];
// To determine the type of the top-level list a level 1 element is needed.
for ( j = 0; j < list.length; j++ ) {
if ( list[ j ].attributes[ 'cke-list-level' ] == 1 ) {
firstLevel1Element = list[ j ];
break;
}
}
var containerStack = [ List.createList( firstLevel1Element ) ],
// List wrapper (ol/ul).
innermostContainer = containerStack[ 0 ],
allContainers = [ containerStack[ 0 ] ];
// Insert first known list item before the list wrapper.
innermostContainer.insertBefore( list[ 0 ] );
for ( j = 0; j < list.length; j++ ) {
element = list[ j ];
level = element.attributes[ 'cke-list-level' ];
while ( level > containerStack.length ) {
var content = List.createList( element );
var children = innermostContainer.children;
if ( children.length > 0 ) {
children[ children.length - 1 ].add( content );
} else {
var container = new CKEDITOR.htmlParser.element( 'li', {
style: 'list-style-type:none'
} );
container.add( content );
innermostContainer.add( container );
}
containerStack.push( content );
allContainers.push( content );
innermostContainer = content;
if ( level == containerStack.length ) {
List.setListSymbol( content, element.attributes[ 'cke-symbol' ], level );
}
}
while ( level < containerStack.length ) {
containerStack.pop();
innermostContainer = containerStack[ containerStack.length - 1 ];
if ( level == containerStack.length ) {
List.setListSymbol( innermostContainer, element.attributes[ 'cke-symbol' ], level );
}
}
// For future reference this is where the list elements are actually put into the lists.
element.remove();
innermostContainer.add( element );
}
// Try to set the symbol for the root (level 1) list.
var level1Symbol;
if ( containerStack[ 0 ].children.length ) {
level1Symbol = containerStack[ 0 ].children[ 0 ].attributes[ 'cke-symbol' ];
if ( !level1Symbol && containerStack[ 0 ].children.length > 1 ) {
level1Symbol = containerStack[0].children[1].attributes[ 'cke-symbol' ];
}
if ( level1Symbol ) {
List.setListSymbol( containerStack[ 0 ], level1Symbol );
}
}
// This can be done only after all the list elements are where they should be.
for ( j = 0; j < allContainers.length; j++ ) {
List.setListStart( allContainers[ j ] );
}
// Last but not least apply li[start] if needed, also this needs to be done once ols are final.
for ( j = 0; j < list.length; j++ ) {
this.determineListItemValue( list[ j ] );
}
}
// Adjust left margin based on parents sum of parents left margin (#2870).
CKEDITOR.tools.array.forEach( listElements, function( element ) {
var listParents = getParentListItems( element ),
leftOffset = getTotalMarginLeft( listParents ),
styles, marginLeft;
if ( !leftOffset ) {
return;
}
element.attributes = element.attributes || {};
styles = CKEDITOR.tools.parseCssText( element.attributes.style );
marginLeft = styles[ 'margin-left' ] || 0;
marginLeft = Math.max( parseInt( marginLeft, 10 ) - leftOffset, 0 );
if ( marginLeft ) {
styles[ 'margin-left' ] = marginLeft + 'px';
} else {
delete styles[ 'margin-left' ];
}
element.attributes.style = CKEDITOR.tools.writeCssText( styles );
} );
return listElements;
function getParentListItems( element ) {
var parents = [],
parent = element.parent;
while ( parent ) {
if ( parent.name === 'li' ) {
parents.push( parent );
}
parent = parent.parent;
}