-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
widgetsintegration.js
1535 lines (1199 loc) · 49 KB
/
widgetsintegration.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
/* bender-tags: widgetcore, 13460 */
/* bender-include: _helpers/tools.js */
/* global widgetTestsTools */
( function() {
'use strict';
var sampleWidget = '<figure data-widget="test" foo="Value1"><span>Value2</span></figure>',
sampleWidget2 = '<h1 data-widget="test2">TEST2</h1>',
sampleWidget3ClipboardHtml = '<div data-cke-test3-widget>test3</div>';
var editorConfig = {
plugins: 'wysiwygarea,sourcearea,widget,clipboard,undo',
allowedContent: true,
language: 'en',
on: {
pluginsLoaded: function( evt ) {
var editor = evt.editor;
editor.dataProcessor.writer.sortAttributes = 1;
// Add new, "test" and "test2" widgets to this editor.
editor.widgets.add( 'test', {
parts: {
bar: 'span'
}
} );
editor.widgets.add( 'test2', {} );
editor.widgets.add( 'test3', {
getClipboardHtml: function() {
return sampleWidget3ClipboardHtml;
},
upcast: function( el ) {
if ( el.attributes[ 'data-test3-widget' ] ) {
return el.wrapWith( new CKEDITOR.htmlParser.element( 'div' ) );
}
return false;
}
} );
editor.widgets.add( 'test4', {
getClipboardHtml: function() {
return '<div data-cke-nested-widget="true">Content</div>';
},
editables: {
ned: '.ned'
}
} );
}
}
};
bender.editor = {
config: editorConfig
};
var fixHtml = widgetTestsTools.fixHtml,
objToArray = bender.tools.objToArray,
data2Attr = widgetTestsTools.data2Attribute,
getWidgetById = widgetTestsTools.getWidgetById,
widgetWrapperAttributes = widgetTestsTools.widgetWrapperAttributes,
widgetInitedWrapperAttributes = widgetTestsTools.widgetInitedWrapperAttributes;
function assertCollapsedSelectionIn( editor, node, offset, msg ) {
var sel = editor.getSelection(),
range = sel.getRanges()[ 0 ];
msg = msg ? msg + ' - ' : '';
range.optimize();
assert.isFalse( !!sel.isFake, msg + 'sel.isFake' );
assert.isTrue( range.collapsed, msg + 'collapsed' );
assert.areSame( node, range.startContainer, msg + 'startContainer' );
assert.areSame( offset, range.startOffset, msg + 'startOffset' );
}
bender.test( {
tearDown: function() {
this.editor.setReadOnly( false );
},
// (#1901)
'test modifier keystrokes upon widget focus': function() {
var editor = this.editor;
editor.widgets.add( 'testevent', {
editables: {
foo: '.foo'
}
} );
this.editorBot.setData( '<p id="p1">foo</p><div data-widget="testevent" id="w1"><p class="foo">foo</p></div>', function() {
var widget = getWidgetById( editor, 'w1' );
widget.focus();
assert.areNotEqual( false, widget.fire( 'key', { keyCode: CKEDITOR.SHIFT + 121 } ), 'Shift should not be canceled' ); // SHIFT + F10
widget.focus();
assert.areNotEqual( false, widget.fire( 'key', { keyCode: CKEDITOR.CTRL + 121 } ), 'Ctrl should not be canceled' ); // CTRL + F10
widget.focus();
assert.areNotEqual( false, widget.fire( 'key', { keyCode: CKEDITOR.ALT + 121 } ), 'Alt should not be canceled' ); // ALT + F10
} );
},
'test initializing widgets': function() {
var editor = this.editor;
this.editorBot.setData( sampleWidget + '<p>foo</p>' + sampleWidget2 + '<p id="x">foo</p>' + sampleWidget2, function() {
var editable = editor.editable(),
instances = objToArray( editor.widgets.instances ),
names = [ instances[ 0 ].name, instances[ 1 ].name, instances[ 2 ].name ].sort();
assert.areEqual( 3, instances.length, 'Three widget instances are present.' );
arrayAssert.itemsAreEqual( [ 'test', 'test2', 'test2' ], names, 'Widgets are correctly initialized' );
var wrapper = instances[ 0 ].wrapper;
assert.areSame( instances[ 0 ].element, wrapper.getFirst(), 'Wrapper contains widget element' );
assert.areEqual( '1', wrapper.data( 'cke-widget-wrapper' ), 'Wrapper has data-cke-widget-wrapper attribute' );
assert.isTrue( editable.contains( wrapper ), 'Editable contains wrapper' );
assert.isTrue( instances[ 0 ].isInited(), 'Widget is initialized' );
// Help IE making selection.
editor.getSelection().selectElement( editor.document.getById( 'x' ) );
var el = CKEDITOR.dom.element.createFromHtml( '<h2>YYY</h2>', editor.document );
editor.insertElement( el );
var widget = editor.widgets.initOn( el, editor.widgets.registered.test2 );
instances = objToArray( editor.widgets.instances );
assert.areEqual( 4, instances.length, 'Four widget instances are present.' );
assert.areEqual( 'test2', instances[ 3 ].name, 'Widget4 is correctly initialized.' );
assert.isInstanceOf( CKEDITOR.plugins.widget, widget, 'Widget4 is instance of widget' );
} );
},
'test widgets processing on toHtml': function() {
var editor = this.editor;
this.editorBot.setData( '', function() {
assert.isMatching( new RegExp(
'^<div ' + widgetWrapperAttributes + '><h1 data-cke-widget-keep-attr="1" data-widget="test2">TEST2</h1></div>' +
'<p>foo</p>' +
'<div ' + widgetWrapperAttributes + '><h1 data-cke-widget-keep-attr="1" data-widget="test2">TEST2</h1></div>$'
),
fixHtml( editor.dataProcessor.toHtml( sampleWidget2 + '<p>foo</p>' + sampleWidget2 ) )
);
} );
},
'test internal widgets stuff cleaned up on toHtml': function() {
var editor = this.editor;
this.editorBot.setData( '', function() {
assert.isMatching(
new RegExp( '^<p>X</p><div ' + widgetWrapperAttributes + '><h1 data-cke-widget-keep-attr="1" data-widget="test2">TEST2</h1></div><p>X</p>$' ),
fixHtml( editor.dataProcessor.toHtml(
'<p>X</p><div class="cke_widget_wrapper" data-cke-widget-wrapper="1" data-cke-widget-id="66" id="oldWrapper" contenteditable="false">' + sampleWidget2 + '</div><p>X</p>'
) )
);
} );
},
'test widget wrapper is skipped by data processor': function() {
var editor = this.editor,
dataProcessor = editor.dataProcessor,
enableTest = false;
dataProcessor.htmlFilter.addRules( {
elements: {
$: function( el ) {
if ( !enableTest )
return;
el.attributes.foo += '2';
}
}
} );
dataProcessor.dataFilter.addRules( {
elements: {
$: function( el ) {
if ( !enableTest )
return;
el.attributes.foo = '1';
}
}
} );
enableTest = true;
this.editorBot.setData( '<p>X</p><div><p data-widget="test" id="w1"><span>foo</span></p></div>', function() {
var data = editor.getData();
// Avoid breaking following tests in case of failure in this.
enableTest = false;
assert.areSame( '<p foo="12">X</p><div foo="12"><p data-widget="test" id="w1"><span>foo</span></p></div>', data );
assert.isFalse( getWidgetById( editor, 'w1' ).wrapper.hasAttribute( 'foo' ) );
} );
},
'test widgets cleanup on setData': function() {
var editor = this.editor,
bot = this.editorBot;
this.editorBot.setData( sampleWidget + '<p>foo</p>' + sampleWidget2 + '<p id="x">foo</p>' + sampleWidget2, function() {
var widgets = editor.widgets,
instances = objToArray( widgets.instances );
assert.areEqual( 3, instances.length, 'Three widget instances are present at the beginning' );
// Help IE making selection.
editor.getSelection().selectElement( editor.document.getById( 'x' ) );
var el = CKEDITOR.dom.element.createFromHtml( sampleWidget, editor.document );
editor.insertElement( el );
widgets.initOn( el );
instances = objToArray( widgets.instances );
assert.areEqual( 4, instances.length, 'Four widget instances are present after initializing new one' );
var destroyedInstances = 0,
createdInstances = 0,
listener1 = widgets.on( 'instanceDestroyed', function() {
destroyedInstances += 1;
} ),
listener2 = widgets.on( 'instanceCreated', function() {
createdInstances += 1;
} );
bot.setData( sampleWidget2, function() {
instances = objToArray( widgets.instances );
assert.areEqual( 1, instances.length, 'After setData only one widget instance is registered' );
assert.areSame( 'test2', instances[ 0 ].name );
assert.areSame( 4, destroyedInstances, '4 instances were destroyed' );
assert.areSame( 1, createdInstances, '1 instance was created' );
// On IE this will fire 'permission denied', because this is old doc's node.
if ( !CKEDITOR.env.ie )
assert.isTrue( el.getParent().hasAttribute( 'data-cke-widget-id' ), 'Widgets were destroyed in offline mode' );
listener1.removeListener();
listener2.removeListener();
} );
} );
},
'test checkWidgets event on contentDomInvalidated': function() {
var editor = this.editor,
widgets = editor.widgets,
checked = 0;
var listener = widgets.on( 'checkWidgets', function() {
checked += 1;
} );
editor.fire( 'contentDomInvalidated' );
listener.removeListener();
assert.areSame( 1, checked );
},
'test checkWidgets event on insertHtml': function() {
var editor = this.editor,
widgets = editor.widgets,
checked = 0,
eventData, editorData;
this.editorBot.setData( '<p data-widget="test2" id="w1">A</p><p>x</p>', function() {
var widget = getWidgetById( editor, 'w1' ),
html = widget.wrapper.getOuterHtml();
editor.widgets.del( widget );
editor.focus();
var range = editor.createRange();
range.moveToPosition( editor.editable().findOne( 'p' ), CKEDITOR.POSITION_AFTER_START );
editor.getSelection().selectRanges( [ range ] );
var listener = widgets.on( 'checkWidgets', function( evt ) {
checked += 1;
eventData = evt.data;
editorData = editor.getData();
} );
editor.insertHtml( html );
listener.removeListener();
assert.areSame( 1, checked );
assert.isTrue( eventData.initOnlyNew, 'data.initOnlyNew was passed' );
assert.isTrue( !!eventData.focusInited, 'data.focusInited was passed' );
assert.areSame( '<p data-widget="test2" id="w1">A</p><p>x</p>', editorData, 'event was fired after data was inserted' );
} );
},
'test checkWidgets event on insertHtmlIntoRange': function() {
var editor = this.editor,
editable = editor.editable(),
widgets = editor.widgets,
checked = 0,
eventData, editorData;
this.editorBot.setData( '<p data-widget="test2" id="w1">A</p><p>xx</p>', function() {
var widget = getWidgetById( editor, 'w1' ),
html = widget.wrapper.getOuterHtml();
editor.widgets.del( widget );
editor.focus();
var listener = widgets.on( 'checkWidgets', function( evt ) {
checked += 1;
eventData = evt.data;
editorData = editor.getData();
} );
var range = editor.createRange();
range.setStart( editable.getChild( [ 0, 0 ] ), 1 );
range.collapse();
editable.insertHtmlIntoRange( html, range );
listener.removeListener();
assert.areSame( 1, checked, 'checkWidgets' );
assert.isTrue( eventData.initOnlyNew, 'data.initOnlyNew was passed' );
assert.isUndefined( eventData.focusInited, 'data.initOnlyNew was not passed' );
assert.areSame( '<p>x</p><p data-widget="test2" id="w1">A</p><p>x</p>', editorData, 'event was fired after data was inserted' );
} );
},
'test checkWidgets event on insertText': function() {
var editor = this.editor,
widgets = editor.widgets,
checked = 0,
eventData, editorData;
var listener = widgets.on( 'checkWidgets', function( evt ) {
checked += 1;
eventData = evt.data;
editorData = editor.getData();
} );
this.editorBot.setHtmlWithSelection( '<p>x^x</p>' );
editor.insertText( 'foo' );
listener.removeListener();
assert.areSame( 1, checked );
assert.isTrue( eventData.initOnlyNew, 'data.initOnlyNew was passed' );
assert.areSame( '<p>xfoox</p>', editorData, 'event was fired after data was inserted' );
},
'test widgets are correctly destroyed/initialzed on mode switch': function() {
var editor = this.editor;
this.editorBot.setData( sampleWidget2 + '<p>foo</p>' + sampleWidget2, function() {
var widgets = editor.widgets;
assert.areEqual( 2, CKEDITOR.tools.object.keys( widgets.instances ).length, 'Two widgets instances are present at the beginning' );
// Ensure async.
wait( function() {
editor.setMode( 'source', function() {
var sourceWidgetsLength = CKEDITOR.tools.object.keys( widgets.instances ).length,
sourceData = editor.getData();
editor.setMode( 'wysiwyg', function() {
resume( function() {
assert.areEqual( 0, sourceWidgetsLength, 'There are no instances in source mode' );
assert.areEqual( sampleWidget2 + '<p>foo</p>' + sampleWidget2, sourceData,
'In source mode widgets are in output format' );
assert.areEqual( 2, CKEDITOR.tools.object.keys( editor.widgets.instances ).length,
'Two instances has been initialized after switching back to wysiwyg' );
assert.areEqual( 2, editor.editable().find( '.cke_widget_wrapper' ).count(),
'Two widget wrappers are present' );
} );
} );
} );
} );
} );
},
'test widget cleanup on getData': function() {
var editor = this.editor;
this.editorBot.setData( '<div>Foo' + sampleWidget + 'Bar</div>', function() {
var instances = objToArray( editor.widgets.instances ),
instance = instances[ 0 ];
assert.areEqual( '1',
instance.wrapper.getAttribute( 'data-cke-widget-wrapper' ),
'Widget wrapper is present.'
);
assert.areEqual(
'<div>Foo<figure data-widget="test" foo="Value1"><span>Value2</span></figure>Bar</div>',
fixHtml( editor.getData() ),
'Data processor cleans widgets on getData.'
);
} );
},
'test automatic data-cke-widget removing if was not present when upcasting': function() {
var editor = this.editor,
bot = this.editorBot;
this.editorBot.setData( '<p>foo</p>', function() {
editor.widgets.add( 'autodata1', {
upcast: function( el ) {
return el.name == 'i' && el.hasClass( 'autodata' );
}
} );
// Check also the variant with downcaster.
editor.widgets.add( 'autodata2', {
upcast: function( el ) {
return el.name == 'b' && el.hasClass( 'autodata' );
},
downcast: function( el ) {
el.attributes.foo = '1';
}
} );
bot.setData(
'<p><i class="autodata" data-widget="autodata1">A</i><i class="autodata">B</i>' +
'<b class="autodata" data-widget="autodata2">C</b><b class="autodata">D</b></p>',
function() {
assert.areSame( 4, CKEDITOR.tools.object.keys( editor.widgets.instances ).length, '4 widgets are upcasted' );
assert.areSame( '<p><i class="autodata" data-widget="autodata1">A</i><i class="autodata">B</i>' +
'<b class="autodata" data-widget="autodata2" foo="1">C</b><b class="autodata" foo="1">D</b></p>', editor.getData() );
}
);
} );
},
'test widgets instantiated on paste': function() {
var editor = this.editor;
this.editorBot.setData( '<p>X</p>' + sampleWidget + '<p>X</p>' + sampleWidget2 + '<p id="x">X</p>', function() {
assert.areEqual( 2, CKEDITOR.tools.object.keys( editor.widgets.instances ).length, 'instances after setData' );
// Set selection after last "X".
var p = editor.document.getById( 'x' ),
range = editor.createRange();
range.setStartAt( p, CKEDITOR.POSITION_BEFORE_END );
range.select();
p.removeAttribute( 'id' );
editor.once( 'afterPaste', function() {
resume( function() {
assert.areEqual( 4, CKEDITOR.tools.object.keys( editor.widgets.instances ).length, 'instances afterPaste' );
assert.areEqual( 4, editor.document.find( '.cke_widget_wrapper' ).count(), 'wrappers afterPaste' );
var ids = [];
map2WidgetIds( ids, editor.document.find( 'figure' ) );
map2WidgetIds( ids, editor.document.find( 'h1' ) );
ids.sort();
assert.isTrue( ids[ 0 ] != ids[ 1 ] && ids[ 1 ] != ids[ 2 ] && ids[ 2 ] != ids[ 3 ],
'Each element has its own widget instance' );
} );
} );
// Ensure async.
wait( function() {
// Paste exactly what can be copied from editable.
editor.execCommand( 'paste', editor.editable().getHtml() );
} );
} );
function map2WidgetIds( ids, collection ) {
for ( var i = 0; i < collection.count(); ++i )
ids.push( editor.widgets.getByElement( collection.getItem( i ) ).id );
}
},
'test pasting upcasted widgets': function() {
var pattern = new RegExp(
'^<p><span ' + widgetInitedWrapperAttributes + '>' +
'<span class="cke_widget_element" data-cke-widget-data="' +
data2Attr( { 'classes': null } ) +
'" data-cke-widget-keep-attr="0" data-cke-widget-upcasted="1" data-widget="test_upcasted_pasting"><i class="upcasted_pasting">foo</i></span>' +
widgetTestsTools.widgetDragHanlder +
'</span>X?(<br />)?</p>' +
'(<div [^>]+> </div>)?$' // Hidden sel container.
);
var editor = this.editor,
bot = this.editorBot;
editor.widgets.add( 'test_upcasted_pasting', {
upcast: function( el ) {
if ( el.name == 'i' && el.hasClass( 'upcasted_pasting' ) )
return el.wrapWith( new CKEDITOR.htmlParser.element( 'span' ) );
}
} );
this.editorBot.setData( '<p><i class="upcasted_pasting">foo</i></p>', function() {
editor.focus();
var html = editor.editable().getHtml();
assert.isMatching( pattern, fixHtml( html ), 'HTML after loading element to be upcasted' );
assert.areSame( 1, CKEDITOR.tools.object.keys( editor.widgets.instances ).length, '1 widget instance after setData' );
bot.setData( '<p>X</p>', function() {
editor.once( 'afterPaste', function() {
resume( function() {
assert.isMatching( pattern, fixHtml( editor.editable().getHtml() ), 'HTML after pasting element' );
assert.areSame( 1, CKEDITOR.tools.object.keys( editor.widgets.instances ).length, '1 widget instance after paste' );
} );
} );
// Ensure async.
wait( function() {
editor.execCommand( 'paste', html );
} );
} );
} );
},
'test widget\'s data is preserved after paste': function() {
var editor = this.editor;
this.editorBot.setData( sampleWidget + '<p>X</p>', function() {
objToArray( editor.widgets.instances )[ 0 ].setData( 'foo', 'bar' );
var html = editor.editable().getHtml();
editor.once( 'afterPaste', function() {
resume( function() {
assert.areSame( 'bar', objToArray( editor.widgets.instances )[ 0 ].data.foo );
} );
} );
wait( function() {
editor.setData( '', function() {
editor.execCommand( 'paste', html );
} );
} );
} );
},
'test single pasted widget is focused': function() {
var editor = this.editor;
this.editorBot.setData( '<p id="w1" data-widget="test2">X</p>', function() {
var html = editor.editable().getHtml();
// https://bugzilla.mozilla.org/show_bug.cgi?id=911201
if ( CKEDITOR.env.gecko )
html = html.replace( /^<br>/, '' );
editor.once( 'afterPaste', function() {
resume( function() {
var instances = objToArray( editor.widgets.instances ),
sel = editor.getSelection();
assert.areSame( 1, instances.length, 'one widget initialized after paste' );
assert.isTrue( !!sel.isFake, 'fake sel' );
assert.areSame( instances[ 0 ].wrapper, sel.getSelectedElement(), 'wrapper is selected' );
} );
} );
wait( function() {
editor.setData( '<p id="x">foo</p><p>bar</p>', function() {
var range = editor.createRange();
range.setStartAt( editor.document.getById( 'x' ), CKEDITOR.POSITION_BEFORE_END );
range.collapse( true );
range.select();
editor.execCommand( 'paste', html );
} );
} );
} );
},
'test no focused widget after pasting few': function() {
var editor = this.editor;
this.editorBot.setData( '<p id="w1" data-widget="test2">A</p><p>X</p><p id="w1" data-widget="test2">B</p>', function() {
var html = editor.editable().getHtml();
editor.once( 'afterPaste', function() {
resume( function() {
var instances = objToArray( editor.widgets.instances ),
sel = editor.getSelection();
assert.areSame( 2, instances.length, 'one widget initialized after paste' );
assert.isFalse( !!sel.isFake, 'selection is not fake' );
} );
} );
wait( function() {
editor.setData( '<p id="x">foo</p><p>bar</p>', function() {
var range = editor.createRange();
range.setStartAt( editor.document.getById( 'x' ), CKEDITOR.POSITION_BEFORE_END );
range.collapse( true );
range.select();
editor.execCommand( 'paste', html );
} );
} );
} );
},
'test copying single focused widget': function() {
var editor = this.editor;
this.editorBot.setData( '<p>X</p><p id="w1" data-widget="test2">A</p>', function() {
var widget = getWidgetById( editor, 'w1' ),
selectionChanged = 0;
widget.focus();
editor.on( 'selectionChange', function() {
selectionChanged += 1;
} );
var evt = new CKEDITOR.dom.event( { keyCode: CKEDITOR.CTRL + 67 } ),
prevented = 0;
evt.preventDefault = function() {
prevented += 1;
};
editor.editable().fire( 'keydown', evt );
var copybin = editor.document.getById( 'cke_copybin' ),
selContainer = editor.getSelection().getCommonAncestor();
assert.isTrue( !!copybin, 'copybin was created' );
assert.isTrue( copybin.contains( selContainer ) || copybin.equals( selContainer ), 'selection was moved to the copybin' );
wait( function() {
assert.areSame( 0, selectionChanged, 'selection has not been changed' );
assert.areSame( widget, getWidgetById( editor, 'w1' ), 'widget is ok' );
assert.isTrue( !!editor.getSelection().isFake, 'selection stayed faked' );
assert.isFalse( !!editor.document.getById( 'cke_copybin' ), 'copybin was removed' );
assert.areSame( 0, prevented, 'copy was not prevented' );
}, 150 );
} );
},
'test cutting single focused widget': function() {
var editor = this.editor;
this.editorBot.setData( '<p>X</p><p id="w1" data-widget="test2">A</p><p>X</p>', function() {
var widget = getWidgetById( editor, 'w1' ),
selectionChanged = 0;
widget.focus();
editor.on( 'selectionChange', function() {
selectionChanged += 1;
} );
editor.editable().fire( 'keydown', new CKEDITOR.dom.event( { keyCode: CKEDITOR.CTRL + 88 } ) );
var copybin = editor.document.getById( 'cke_copybin' ),
selContainer = editor.getSelection().getCommonAncestor();
assert.isTrue( !!copybin, 'copybin was created' );
assert.isTrue( copybin.contains( selContainer ) || copybin.equals( selContainer ), 'selection was moved to the copybin' );
wait( function() {
assert.isTrue( selectionChanged > 0, 'selection has been changed' );
assert.isFalse( !!getWidgetById( editor, 'w1' ), 'widget was deleted' );
assert.isFalse( !!editor.getSelection().isFake, 'selection is not faked' );
assert.isFalse( !!editor.document.getById( 'cke_copybin' ), 'copybin was removed' );
}, 150 );
} );
},
// #1570
'test cutting single focused widget with readonly mode': function() {
var editor = this.editor;
this.editorBot.setData( '<p>X</p><p id="w1" data-widget="test2">A</p><p>X</p>', function() {
var widget = getWidgetById( editor, 'w1' ),
selectionChanged = 0;
widget.focus();
editor.on( 'selectionChange', function() {
selectionChanged += 1;
} );
editor.setReadOnly( true );
editor.editable().fire( 'keydown', new CKEDITOR.dom.event( { keyCode: CKEDITOR.CTRL + 88 } ) );
var copybin = editor.document.getById( 'cke_copybin' ),
selContainer = editor.getSelection().getCommonAncestor();
assert.isTrue( !!copybin, 'copybin was created' );
assert.isTrue( copybin.contains( selContainer ) || copybin.equals( selContainer ), 'selection was moved to the copybin' );
wait( function() {
assert.isTrue( selectionChanged == 0, 'selection has not been changed' );
assert.isTrue( !!getWidgetById( editor, 'w1' ), 'widget has not been deleted' );
assert.isFalse( !!editor.getSelection().isFake, 'selection is not faked' );
assert.isFalse( !!editor.document.getById( 'cke_copybin' ), 'copybin was removed' );
}, 150 );
} );
},
'test pasting single focused widget': function() {
var editor = this.editor;
this.editorBot.setData( '<p data-widget="test2" id="w1">A</p><p id="p1">X</p>', function() {
var widget = getWidgetById( editor, 'w1' ),
html = widget.wrapper.getOuterHtml();
editor.widgets.del( widget );
editor.focus();
var range = editor.createRange();
range.setStartAt( editor.document.getById( 'p1' ), CKEDITOR.POSITION_BEFORE_END );
range.collapse( true );
range.select();
editor.once( 'afterPaste', function() {
resume( function() {
var widget = getWidgetById( editor, 'w1' );
assert.isTrue( !!widget, 'widget was pasted' );
assert.areSame( '<p id="p1">X</p><p data-widget="test2" id="w1">A</p>', editor.getData() );
assert.isTrue( !!editor.getSelection().isFake, 'widget is selected' );
assert.areSame( widget.wrapper, editor.getSelection().getSelectedElement(), 'widget is selected - element' );
} );
} );
// Ensure async.
wait( function() {
editor.execCommand( 'paste', '<span data-cke-copybin-start="1" foo="1">\u200b</span>' + html + '<span data-cke-copybin-end="1">\u200b</span>' );
} );
} );
},
// https://dev.ckeditor.com/ticket/13460
'test pasting a widget with lots of extra markup and mixed HTML case': function() {
var editor = this.editor;
this.editorBot.setData( '<p id="p1">A<span data-widget="test2" id="w1">A</span>B</p>', function() {
var widget = getWidgetById( editor, 'w1' ),
html = widget.wrapper.getOuterHtml();
editor.widgets.del( widget );
editor.focus();
var range = editor.createRange();
range.setStartAt( editor.document.getById( 'p1' ), CKEDITOR.POSITION_BEFORE_END );
range.collapse( true );
range.select();
editor.once( 'afterPaste', function() {
resume( function() {
var widget = getWidgetById( editor, 'w1' );
assert.isTrue( !!widget, 'widget was pasted' );
assert.areSame( '<p id="p1">AB<span data-widget="test2" id="w1">A</span></p>', editor.getData() );
assert.areSame( widget, editor.widgets.focused, 'widget is selected' );
} );
} );
// Ensure async.
wait( function() {
editor.execCommand( 'paste',
'<SPAN style="POSITION: absolute; WIDTH: 1px; HEIGHT: 1px; OVERFLOW: hidden; LEFT: -5000px">' +
'<SPAN data-cke-copybin-start="1" foo="1">\u200b</SPAN>' +
html +
'<SPAN data-cke-copybin-end="1">\u200b</SPAN>' +
'</SPAN>'
);
} );
} );
},
'test copying widget with context': function() {
var editor = this.editor;
this.editorBot.setData( '<p>X<b><span id="w1" data-widget="test2">A</span></b>X</p>', function() {
var widget = getWidgetById( editor, 'w1' ),
evt = new CKEDITOR.dom.event( { keyCode: CKEDITOR.CTRL + 67 } );
widget.focus();
editor.editable().fire( 'keydown', evt );
wait( function() {
var range = editor.getSelection().getRanges()[ 0 ];
assert.areSame( 'b', range.startContainer.getName(), 'startContainer' );
assert.areSame( 'b', range.endContainer.getName(), 'endContainer' );
assert.isTrue( range.startContainer.getChild( 0 ).hasClass( 'cke_widget_wrapper' ) );
}, 150 );
} );
},
// (#3138)
'test widget clipboard html can be shadowed': function() {
var editor = this.editor;
this.editorBot.setData( '<div id="w1" data-widget="test3">test3</div>', function() {
var widget = getWidgetById( editor, 'w1' );
assert.areEqual( sampleWidget3ClipboardHtml, widget.getClipboardHtml() );
} );
},
// (#3138)
'test shadowed clipboard HTML is used for copying (single widget)': createCopyCutTest( {
event: 'copy',
html: '<div id="w1" data-widget="test3">test3</div>',
init: function( editor ) {
var widget = getWidgetById( editor, 'w1' );
widget.focus();
},
assert: function( editor, clipboardHtml ) {
assert.isMatching( /.*data-cke-test3-widget.*/, clipboardHtml );
}
} ),
// (#3138)
'test shadowed clipboard HTML is used for cutting (single widget)': createCopyCutTest( {
event: 'cut',
html: '<div id="w1" data-widget="test3">test3</div>',
init: function( editor ) {
var widget = getWidgetById( editor, 'w1' );
widget.focus();
},
assert: function( editor, clipboardHtml ) {
assert.isMatching( /.*data-cke-test3-widget.*/, clipboardHtml );
}
} ),
// (#3138)
'test shadowed clipboard HTML is used for copying (multiple widgets)': createCopyCutTest( {
event: 'copy',
html: '<p>Lorem</p>' +
'<div id="w1" data-widget="test3">test3</div>' +
'<div id="w2" data-widget="test3">test3</div>' +
'<p>Ipsum</p>',
init: function( editor ) {
var range = editor.createRange(),
startNode = getWidgetById( editor, 'w1' ).wrapper,
endNode = getWidgetById( editor, 'w2' ).wrapper;
range.setStartBefore( startNode );
range.setEndAfter( endNode );
range.select();
},
assert: function( editor, clipboardHtml ) {
// Due to weird HTML in IE8 we can't use assert.isMatching here.
var match = clipboardHtml.match( /.*?data-cke-test3-widget.*?/g );
assert.areSame( 2, match.length );
}
} ),
// (#3138)
'test shadowed clipboard HTML is used for cutting (multiple widgets)': createCopyCutTest( {
event: 'cut',
html: '<p>Lorem</p>' +
'<div id="w1" data-widget="test3">test3</div>' +
'<div id="w2" data-widget="test3">test3</div>' +
'<p>Ipsum</p>',
init: function( editor ) {
var range = editor.createRange(),
startNode = getWidgetById( editor, 'w1' ).wrapper,
endNode = getWidgetById( editor, 'w2' ).wrapper;
range.setStartBefore( startNode );
range.setEndAfter( endNode );
range.select();
},
assert: function( editor, clipboardHtml ) {
// Due to weird HTML in IE8 we can't use assert.isMatching here.
var match = clipboardHtml.match( /.*?data-cke-test3-widget.*?/g );
assert.areSame( 2, match.length );
}
} ),
// (#3138)
'test shadowed clipboard HTML is used for copying (single nested widget)': createCopyCutTest( {
event: 'copy',
html: '<div data-widget="test4" id="w1">' +
'<div class="ned">' +
'<div id="w2" data-widget="test3">test3</div>' +
'</div>' +
'</div>',
init: function( editor ) {
var widget = getWidgetById( editor, 'w1' );
widget.focus();
},
assert: function( editor, clipboardHtml ) {
assert.isMatching( /<div data-cke-nested-widget="true">Content<\/div>/i, clipboardHtml );
assert.isNotMatching( /.*data-cke-test3-widget.*/, clipboardHtml );
}
} ),
// (#3138)
'test shadowed clipboard HTML is used for cutting (single nested widget)': createCopyCutTest( {
event: 'cut',
html: '<div data-widget="test4" id="w1">' +
'<div class="ned">' +
'<div id="w2" data-widget="test3">test3</div>' +
'</div>' +
'</div>',
init: function( editor ) {
var widget = getWidgetById( editor, 'w1' );
widget.focus();
},
assert: function( editor, clipboardHtml ) {
assert.isMatching( /<div data-cke-nested-widget="true">Content<\/div>/i, clipboardHtml );
assert.isNotMatching( /.*data-cke-test3-widget.*/, clipboardHtml );
}
} ),
// (#3138)
'test shadowed clipboard HTML is used for copying (multiple nested widgets)': createCopyCutTest( {
event: 'copy',
html: '<p>Lorem</p>' +
'<div data-widget="test4" id="w1">' +
'<div class="ned">' +
'<div id="w2" data-widget="test3">test3</div>' +
'</div>' +
'</div>' +
'<div data-widget="test4" id="w3">' +
'<div class="ned">' +
'<div id="w4" data-widget="test3">test3</div>' +
'</div>' +
'</div>' +
'<p>ipsum</p>',
// Safari has issues with selecting multiple nested widgets, so we must anchor
// selection in text before and after widgets.
init: function( editor ) {
var range = editor.createRange(),
paragraphs = editor.editable().find( 'p' ).toArray(),
startNode = paragraphs[ 0 ].getChild( 0 ),
endNode = paragraphs[ 1 ].getChild( 0 );
range.setStart( startNode, 4 );
range.setEnd( endNode, 3 );
range.select();
},
assert: function( editor, clipboardHtml ) {
// Due to weird HTML in IE8 we can't use assert.isMatching here.
var match = clipboardHtml.match( /<div data-cke-nested-widget="true">Content<\/div>/gi );
assert.areSame( 2, match.length );
}
} ),
// (#3138)
'test shadowed clipboard HTML is used for cutting (multiple nested widgets)': createCopyCutTest( {
event: 'cut',
html: '<p>Lorem</p>' +
'<div data-widget="test4" id="w1">' +
'<div class="ned">' +