/
default.js
2314 lines (1960 loc) · 71.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-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or http://ckeditor.com/license
*/
/* globals CKEDITOR */
( function() {
var List,
Style,
Heuristics,
filter,
tools = CKEDITOR.tools,
invalidTags = [
'o:p',
'xml',
'script',
'meta',
'link'
],
links = {},
inComment = 0;
/**
* Set of Paste from Word plugin helpers.
*
* @since 4.6.0
* @private
* @member CKEDITOR.plugins
*/
CKEDITOR.plugins.pastefromword = {};
CKEDITOR.cleanWord = function( mswordHtml, editor ) {
var msoListsDetected = Boolean( mswordHtml.match( /mso-list:\s*l\d+\s+level\d+\s+lfo\d+/ ) );
// Before filtering inline all the styles to allow because some of them are available only in style
// sheets. This step is skipped in IEs due to their flaky support for custom types in dataTransfer. (#16847)
if ( CKEDITOR.plugins.clipboard.isCustomDataTypesSupported ) {
mswordHtml = CKEDITOR.plugins.pastefromword.styles.inliner.inline( mswordHtml ).getBody().getHtml();
}
// Sometimes Word malforms the comments.
mswordHtml = mswordHtml.replace( /<!\[/g, '<!--[' ).replace( /\]>/g, ']-->' );
var fragment = CKEDITOR.htmlParser.fragment.fromHtml( mswordHtml );
filter = new CKEDITOR.htmlParser.filter( {
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 ) {
Style.createStyleStack( element, filter, editor );
},
'img': function( element ) {
var attributeStyleMap = {
width: function( value ) {
Style.setStyle( element, 'width', value + 'px' );
},
height: function( value ) {
Style.setStyle( element, 'height', value + 'px' );
}
};
// If the parent is DocumentFragment it does not have any attributes. (#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.mapStyles( element, attributeStyleMap );
if ( element.attributes.src && element.attributes.src.match( /^file:\/\// ) &&
element.attributes.alt && element.attributes.alt.match( /^https?:\/\// ) ) {
element.attributes.src = element.attributes.alt;
}
},
'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. (#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];
}
}
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*$/ ) ) {
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.objectKeys( 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. (#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. (#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| )+$/ ) ) {
// replaceWithChildren doesn't work in filters.
for ( var i = element.children.length - 1; i >= 0; i-- ) {
element.children[ i ].insertAfter( 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 );
},
'table': function( element ) {
element._tdBorders = {};
element.filterChildren( filter );
var borderStyle, occurences = 0;
for ( var border in element._tdBorders ) {
if ( element._tdBorders[ border ] > occurences ) {
occurences = element._tdBorders[ border ];
borderStyle = border;
}
}
Style.setStyle( element, 'border', borderStyle );
var parent = element.parent,
root = parent && parent.parent,
parentChildren,
i;
// In case parent div has only align attr, move it to the table element (#16811).
if ( parent.name && parent.name === 'div' && parent.attributes.align &&
tools.objectKeys( parent.attributes ).length === 1 && parent.children.length === 1 ) {
// If align is the only attribute of parent.
element.attributes.align = parent.attributes.align;
parentChildren = parent.children.splice( 0 );
element.remove();
for ( i = parentChildren.length - 1; i >= 0; i-- ) {
root.add( parentChildren[ i ], parent.getIndex() );
}
parent.remove();
}
},
'td': function( element ) {
var ascendant = element.getAscendant( 'table' ),
tdBorders = ascendant._tdBorders,
borderStyles = [ 'border', 'border-top', 'border-right', 'border-bottom', 'border-left' ],
ascendantStyle = tools.parseCssText( ascendant.attributes.style );
// Sometimes the background is set for the whole table - move it to individual cells.
var background = ascendantStyle.background || ascendantStyle.BACKGROUND;
if ( background ) {
Style.setStyle( element, 'background', background, true );
}
var backgroundColor = ascendantStyle[ 'background-color' ] || ascendantStyle[ 'BACKGROUND-COLOR' ];
if ( backgroundColor ) {
Style.setStyle( element, 'background-color', backgroundColor, true );
}
var styles = tools.parseCssText( element.attributes.style );
for ( var style in styles ) {
var temp = styles[ style ];
delete styles[ style ];
styles[ style.toLowerCase() ] = temp;
}
// Count all border styles that occur in the table.
for ( var i = 0; i < borderStyles.length; i++ ) {
if ( styles[ borderStyles[ i ] ] ) {
var key = styles[ borderStyles[ i ] ];
tdBorders[ key ] = tdBorders[ key ] ? tdBorders[ key ] + 1 : 1;
}
}
Style.createStyleStack( element, filter, editor,
/margin|text\-align|padding|list\-style\-type|width|height|border|white\-space|vertical\-align|background/i );
},
'v:imagedata': remove,
// This is how IE8 presents images.
'v:shape': function( element ) {
// In chrome a <v:shape> element may be followed by an <img> element with the same content.
var duplicate = false;
element.parent.getFirst( function( child ) {
if ( child.name == 'img' &&
child.attributes &&
child.attributes[ 'v:shapes' ] == element.attributes.id ) {
duplicate = true;
}
} );
if ( duplicate ) return false;
var src = '';
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;
},
'style': function() {
// We don't want to let any styles in. Firefox tends to add some.
return false;
}
},
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;
}
} );
var writer = new CKEDITOR.htmlParser.basicWriter();
filter.applyTo( fragment );
fragment.writeHtml( writer );
return writer.getHtml();
};
/**
* Namespace containing all the helper functions to work with styles.
*
* @private
* @since 4.6.0
* @member CKEDITOR.plugins.pastefromword
*/
CKEDITOR.plugins.pastefromword.styles = {
setStyle: function( element, key, value, dontOverwrite ) {
var styles = tools.parseCssText( element.attributes.style );
if ( dontOverwrite && styles[ key ] ) {
return;
}
if ( value === '' ) {
delete styles[ key ];
} else {
styles[ key ] = value;
}
element.attributes.style = CKEDITOR.tools.writeCssText( styles );
},
// Map attributes to styles.
mapStyles: function( element, attributeStyleMap ) {
for ( var attribute in attributeStyleMap ) {
if ( element.attributes[ attribute ] ) {
if ( typeof attributeStyleMap[ attribute ] === 'function' ) {
attributeStyleMap[ attribute ]( element.attributes[ attribute ] );
} else {
Style.setStyle( element, attributeStyleMap[ attribute ], element.attributes[ attribute ] );
}
delete element.attributes[ attribute ];
}
}
},
/**
* Filters Word-specific styles for a given element. Also might filter additional styles
* based on the `editor` configuration.
*
* @private
* @param {CKEDITOR.htmlParser.element} element
* @param {CKEDITOR.editor} editor
* @member CKEDITOR.plugins.pastefromword.styles
*/
normalizedStyles: function( element, editor ) {
// Some styles and style values are redundant, so delete them.
var resetStyles = [
'background-color:transparent',
'border-image:none',
'color:windowtext',
'direction:ltr',
'mso-',
'text-indent',
'visibility:visible',
'div:border:none' // This one stays because #6241
],
textStyles = [
'font-family',
'font',
'font-size',
'color',
'background-color',
'line-height',
'text-decoration'
],
matchStyle = function() {
var keys = [];
for ( var i = 0; i < arguments.length; i++ ) {
if ( arguments[ i ] ) {
keys.push( arguments[ i ] );
}
}
return tools.indexOf( resetStyles, keys.join( ':' ) ) !== -1;
},
removeFontStyles = editor && editor.config.pasteFromWordRemoveFontStyles === true;
var styles = tools.parseCssText( element.attributes.style );
if ( element.name == 'cke:li' ) {
// IE8 tries to emulate list indentation with a combination of
// text-indent and left margin. Normalize this. Note that IE8 styles are uppercase.
if ( styles[ 'TEXT-INDENT' ] && styles.MARGIN ) {
element.attributes[ 'cke-indentation' ] = List.getElementIndentation( element );
styles.MARGIN = styles.MARGIN.replace( /(([\w\.]+ ){3,3})[\d\.]+(\w+$)/, '$10$3' );
}
}
var keys = tools.objectKeys( styles );
for ( var i = 0; i < keys.length; i++ ) {
var styleName = keys[ i ].toLowerCase(),
styleValue = styles[ keys[ i ] ],
indexOf = CKEDITOR.tools.indexOf,
toBeRemoved = removeFontStyles && indexOf( textStyles, styleName.toLowerCase() ) !== -1;
if ( toBeRemoved || matchStyle( null, styleName, styleValue ) ||
matchStyle( null, styleName.replace( /\-.*$/, '-' ) ) ||
matchStyle( null, styleName ) ||
matchStyle( element.name, styleName, styleValue ) ||
matchStyle( element.name, styleName.replace( /\-.*$/, '-' ) ) ||
matchStyle( element.name, styleName ) ||
matchStyle( styleValue )
) {
delete styles[ keys[ i ] ];
}
}
return CKEDITOR.tools.writeCssText( styles );
},
/**
* Surrounds the element's children with a stack of spans, each one having one style
* originally belonging to the element.
*
* @private
* @param {CKEDITOR.htmlParser.element} element
* @param {CKEDITOR.htmlParser.filter} filter
* @param {CKEDITOR.editor} editor
* @param {RegExp} [skipStyles] All matching style names will not be extracted to a style stack. Defaults
* to `/margin|text\-align|width|border|padding/i`.
* @member CKEDITOR.plugins.pastefromword.styles
*/
createStyleStack: function( element, filter, editor, skipStyles ) {
var children = [],
i;
element.filterChildren( filter );
// Store element's children somewhere else.
for ( i = element.children.length - 1; i >= 0; i-- ) {
children.unshift( element.children[ i ] );
element.children[ i ].remove();
}
Style.sortStyles( element );
// Create a stack of spans with each containing one style.
var styles = tools.parseCssText( Style.normalizedStyles( element, editor ) ),
innermostElement = element,
styleTopmost = element.name === 'span'; // Ensure that the root element retains at least one style.
for ( var style in styles ) {
if ( style.match( skipStyles || /margin|text\-align|width|border|padding/i ) ) {
continue;
}
if ( styleTopmost ) {
styleTopmost = false;
continue;
}
var newElement = new CKEDITOR.htmlParser.element( 'span' );
newElement.attributes.style = style + ':' + styles[ style ];
innermostElement.add( newElement );
innermostElement = newElement;
delete styles[ style ];
}
if ( JSON.stringify( styles ) !== '{}' ) {
element.attributes.style = CKEDITOR.tools.writeCssText( styles );
} else {
delete element.attributes.style;
}
// Add the stored children to the innermost span.
for ( i = 0; i < children.length; i++ ) {
innermostElement.add( children[ i ] );
}
},
// Some styles need to be stacked in a particular order to work properly.
sortStyles: function( element ) {
var orderedStyles = [
'border',
'border-bottom',
'font-size',
'background'
],
style = tools.parseCssText( element.attributes.style ),
keys = tools.objectKeys( style ),
sortedKeys = [],
nonSortedKeys = [];
// Divide styles into sorted and non-sorted, because Array.prototype.sort()
// requires a transitive relation.
for ( var i = 0; i < keys.length; i++ ) {
if ( tools.indexOf( orderedStyles, keys[ i ].toLowerCase() ) !== -1 ) {
sortedKeys.push( keys[ i ] );
} else {
nonSortedKeys.push( keys[ i ] );
}
}
// For styles in orderedStyles[] enforce the same order as in orderedStyles[].
sortedKeys.sort( function( a, b ) {
var aIndex = tools.indexOf( orderedStyles, a.toLowerCase() );
var bIndex = tools.indexOf( orderedStyles, b.toLowerCase() );
return aIndex - bIndex;
} );
keys = [].concat( sortedKeys, nonSortedKeys );
var sortedStyles = {};
for ( i = 0; i < keys.length; i++ ) {
sortedStyles[ keys[ i ] ] = style[ keys[ i ] ];
}
element.attributes.style = CKEDITOR.tools.writeCssText( sortedStyles );
},
/**
* Moves the element's styles lower in the DOM hierarchy. If wrapText==true and the direct child of an element
* is a text node it will be wrapped in a `span` element.
*
* @param {CKEDITOR.htmlParser.element} element
* @param {Object} exceptions Object containing style names which should not be moved, e.g. `{ background: true }`.
* @param {Boolean} [wrapText=false] Whether a direct text child of an element should be wrapped into a `span` tag
* so that the styles can be moved to it.
* @returns {Boolean} Returns true if styles were successfully moved lower.
*/
pushStylesLower: function( element, exceptions, wrapText ) {
if ( !element.attributes.style ||
element.children.length === 0 ) {
return false;
}
exceptions = exceptions || {};
// Entries ending with a dash match styles that start with
// the entry name, e.g. 'border-' matches 'border-style', 'border-color' etc.
var retainedStyles = {
'list-style-type': true,
'width': true,
'height': true,
'border': true,
'border-': true
};
var styles = tools.parseCssText( element.attributes.style );
for ( var style in styles ) {
if ( style.toLowerCase() in retainedStyles ||
retainedStyles [ style.toLowerCase().replace( /\-.*$/, '-' ) ] ||
style.toLowerCase() in exceptions ) {
continue;
}
var pushed = false;
for ( var i = 0; i < element.children.length; i++ ) {
var child = element.children[ i ];
if ( child.type === CKEDITOR.NODE_TEXT && wrapText ) {
var wrapper = new CKEDITOR.htmlParser.element( 'span' );
wrapper.setHtml( child.value );
child.replaceWith( wrapper );
child = wrapper;
}
if ( child.type !== CKEDITOR.NODE_ELEMENT ) {
continue;
}
pushed = true;
Style.setStyle( child, style, styles[ style ] );
}
if ( pushed ) {
delete styles[ style ];
}
}
element.attributes.style = CKEDITOR.tools.writeCssText( styles );
return true;
},
/**
* Namespace containing styles's inliner.
*
* @since 4.7.0
* @private
* @member CKEDITOR.plugins.pastefromword.styles
*/
inliner: {
/**
*
* Styles skipped by the style inliner.
*
* @property {String[]}
* @private
* @member CKEDITOR.plugins.pastefromword.styles.inliner
*/
filtered: [
'break-before',
'break-after',
'break-inside',
'page-break',
'page-break-before',
'page-break-after',
'page-break-inside'
],
/**
* Parses content of provided `style` element.
*
* @param {CKEDITOR.dom.element/String} styles The `style` element or CSS text.
* @returns {Array} Array containing parsed styles. Each item (style) is an object containing two properties:
* selector - String representing a CSS selector.
* styles - Object containing list of styles (e.g. `{ margin: 0, text-align: 'left' }`).
* @since 4.7.0
* @private
* @member CKEDITOR.plugins.pastefromword.styles.inliner
*/
parse: function( styles ) {
var parseCssText = CKEDITOR.tools.parseCssText,
filterStyles = CKEDITOR.plugins.pastefromword.styles.inliner.filter,
sheet = styles.is ? styles.$.sheet : createIsolatedStylesheet( styles );
function createIsolatedStylesheet( styles ) {
var style = new CKEDITOR.dom.element( 'style' ),
iframe = new CKEDITOR.dom.element( 'iframe' );
iframe.hide();
CKEDITOR.document.getBody().append( iframe );
iframe.$.contentDocument.documentElement.appendChild( style.$ );
style.$.textContent = styles;
iframe.remove();
return style.$.sheet;
}
function getStyles( cssText ) {
var startIndex = cssText.indexOf( '{' ),
endIndex = cssText.indexOf( '}' );
return parseCssText( cssText.substring( startIndex + 1, endIndex ), true );
}
var parsedStyles = [],
rules,
i;
if ( sheet ) {
rules = sheet.cssRules;
for ( i = 0; i < rules.length; i++ ) {
// To detect if the rule contains styles and is not an at-rule, it's enough to check rule's type.
if ( rules[ i ].type === window.CSSRule.STYLE_RULE ) {
parsedStyles.push( {
selector: rules[ i ].selectorText,
styles: filterStyles( getStyles( rules[ i ].cssText ) )
} );
}
}
}
return parsedStyles;
},
/**
* Filters out all unnecessary styles
*
* @param {Object} stylesObj Object containing parsed CSS declarations
* as a property/value pairs (see {@link CKEDITOR.plugins.pastefromword.inline#parse}).
* @returns {Object} The stylesObj copy with a specific styles filtered out.
* @since 4.7.0
* @private
* @member CKEDITOR.plugins.pastefromword.styles.inliner
*/
filter: function( stylesObj ) {
var toRemove = CKEDITOR.plugins.pastefromword.styles.inliner.filtered,
indexOf = tools.array.indexOf,
newObj = {},
style;
for ( style in stylesObj ) {
if ( indexOf( toRemove, style ) === -1 ) {
newObj[ style ] = stylesObj[ style ];
}
}
return newObj;
},
/**
* Sorts the given styles array. All rules containing class selectors will have lower indexes than the rest
* of the rules. Selectors with the same priority will be sorted in a reverse order than in the input array.
*
* @param {Array} stylesArray Array of styles as returned from {@link CKEDITOR.plugins.pastefromword.inline#parse}.
* @returns {Array} Sorted stylesArray.
* @since 4.7.0
* @private
* @member CKEDITOR.plugins.pastefromword.styles.inliner
*/
sort: function( stylesArray ) {
// Returns comparison function which sorts all selectors in a way that class selectors are ordered
// before the rest of the selectors. The order of the selectors with the same specificity
// is reversed so that the most important will be applied first.
function getCompareFunction( styles ) {
var order = CKEDITOR.tools.array.map( styles, function( item ) {
return item.selector;
} );
return function( style1, style2 ) {
var value1 = isClassSelector( style1.selector ) ? 1 : 0,
value2 = isClassSelector( style2.selector ) ? 1 : 0,
result = value2 - value1;
// If the selectors have same specificity, the latter one should
// have higher priority (goes first).
return result !== 0 ? result :
order.indexOf( style2.selector ) - order.indexOf( style1.selector );
};
}
// True if given CSS selector contains a class selector.
function isClassSelector( selector ) {
return ( '' + selector ).indexOf( '.' ) !== -1;
}
return stylesArray.sort( getCompareFunction( stylesArray ) );
},
/**
* Finds and inlines all the `style` elements in a given `html` string and returns a document, where
* all the styles are inlined into appropriate elements.
*
* This is needed because sometimes Microsoft Word does not put style directly to the element, but in
* a generic style sheet.
*
* @param {String} html HTML string to be parsed.
* @returns {CKEDITOR.dom.document}
* @since 4.7.0
* @private
* @member CKEDITOR.plugins.pastefromword.styles.inliner
*/
inline: function( html ) {
var parseStyles = CKEDITOR.plugins.pastefromword.styles.inliner.parse,
sortStyles = CKEDITOR.plugins.pastefromword.styles.inliner.sort,
document = createTempDocument( html ),
stylesTags = document.find( 'style' ),
stylesArray = sortStyles( parseStyleTags( stylesTags ) );
function createTempDocument( html ) {
var parser = new DOMParser(),
document = parser.parseFromString( html, 'text/html' );
return new CKEDITOR.dom.document( document );
}
function parseStyleTags( stylesTags ) {
var styles = [],
i;
for ( i = 0; i < stylesTags.count(); i++ ) {
styles = styles.concat( parseStyles( stylesTags.getItem( i ) ) );
}
return styles;
}
function applyStyle( document, selector, style ) {
var elements = document.find( selector ),
element,
oldStyle,
newStyle,
i;
for ( i = 0; i < elements.count(); i++ ) {
element = elements.getItem( i );
oldStyle = CKEDITOR.tools.parseCssText( element.getAttribute( 'style' ) );
// The styles are applied with decreasing priority so we do not want
// to overwrite the existing properties.
newStyle = CKEDITOR.tools.extend( {}, oldStyle, style );
element.setAttribute( 'style', CKEDITOR.tools.writeCssText( newStyle ) );
}
}
CKEDITOR.tools.array.forEach( stylesArray, function( style ) {
applyStyle( document, style.selector, style.styles );
} );
return document;
}
}
};
Style = CKEDITOR.plugins.pastefromword.styles;
/**
* Namespace containing any list-oriented helper methods.
*
* @private
* @since 4.6.0
* @member CKEDITOR.plugins.pastefromword
*/
CKEDITOR.plugins.pastefromword.lists = {
/**
* Checks if a given element is a list item-alike.
*
* @private
* @param {CKEDITOR.editor} editor
* @param {CKEDITOR.htmlParser.element} element
* @returns {Boolean}
* @member CKEDITOR.plugins.pastefromword.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
* @param {CKEDITOR.editor} editor
* @param {CKEDITOR.htmlParser.element} element
* @member CKEDITOR.plugins.pastefromword.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.