This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
link.js
786 lines (588 loc) · 25.1 KB
/
link.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
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/* globals document, Event */
import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Link from '../src/link';
import LinkEngine from '../src/linkengine';
import LinkFormView from '../src/ui/linkformview';
import LinkActionsView from '../src/ui/linkactionsview';
import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
import Range from '@ckeditor/ckeditor5-engine/src/view/range';
import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
import priorities from '@ckeditor/ckeditor5-utils/src/priorities';
testUtils.createSinonSandbox();
describe( 'Link', () => {
let editor, linkFeature, linkButton, balloon, formView, actionsView, editorElement;
beforeEach( () => {
editorElement = document.createElement( 'div' );
document.body.appendChild( editorElement );
return ClassicTestEditor
.create( editorElement, {
plugins: [ Link, Paragraph ]
} )
.then( newEditor => {
editor = newEditor;
linkFeature = editor.plugins.get( Link );
linkButton = editor.ui.componentFactory.create( 'link' );
balloon = editor.plugins.get( ContextualBalloon );
formView = linkFeature.formView;
actionsView = linkFeature.actionsView;
// There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
testUtils.sinon.stub( balloon.view, 'attachTo' ).returns( {} );
testUtils.sinon.stub( balloon.view, 'pin' ).returns( {} );
formView.render();
} );
} );
afterEach( () => {
editorElement.remove();
return editor.destroy();
} );
it( 'should be loaded', () => {
expect( linkFeature ).to.be.instanceOf( Link );
} );
it( 'should load LinkEngine', () => {
expect( editor.plugins.get( LinkEngine ) ).to.be.instanceOf( LinkEngine );
} );
it( 'should load ContextualBalloon', () => {
expect( editor.plugins.get( ContextualBalloon ) ).to.be.instanceOf( ContextualBalloon );
} );
describe( 'init', () => {
it( 'should register click observer', () => {
expect( editor.editing.view.getObserver( ClickObserver ) ).to.be.instanceOf( ClickObserver );
} );
it( 'should create #actionsView', () => {
expect( actionsView ).to.be.instanceOf( LinkActionsView );
} );
it( 'should create #formView', () => {
expect( formView ).to.be.instanceOf( LinkFormView );
} );
describe( 'link toolbar button', () => {
it( 'should be registered', () => {
expect( linkButton ).to.be.instanceOf( ButtonView );
} );
it( 'should be bound to the link command', () => {
const command = editor.commands.get( 'link' );
command.isEnabled = true;
expect( linkButton.isEnabled ).to.be.true;
command.isEnabled = false;
expect( linkButton.isEnabled ).to.be.false;
} );
it( 'should call #_showUI upon #execute', () => {
const spy = testUtils.sinon.stub( linkFeature, '_showUI' ).returns( {} );
linkButton.fire( 'execute' );
sinon.assert.calledWithExactly( spy );
} );
} );
} );
describe( '_showUI()', () => {
let balloonAddSpy;
beforeEach( () => {
balloonAddSpy = testUtils.sinon.spy( balloon, 'add' );
editor.editing.view.document.isFocused = true;
} );
it( 'should not work if the link command is disabled', () => {
setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
editor.commands.get( 'link' ).isEnabled = false;
linkFeature._showUI();
expect( balloon.visibleView ).to.be.null;
} );
it( 'should not throw if the UI is already visible', () => {
setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
linkFeature._showUI();
expect( () => {
linkFeature._showUI();
} ).to.not.throw();
} );
it( 'should add #formView to the balloon and attach the balloon to the selection when text fragment is selected', () => {
setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
linkFeature._showUI();
expect( balloon.visibleView ).to.equal( formView );
sinon.assert.calledWithExactly( balloonAddSpy, {
view: formView,
position: {
target: selectedRange
}
} );
} );
it( 'should add #formView to the balloon and attach the balloon to the selection when selection is collapsed', () => {
setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
linkFeature._showUI();
expect( balloon.visibleView ).to.equal( formView );
sinon.assert.calledWithExactly( balloonAddSpy, {
view: formView,
position: {
target: selectedRange
}
} );
} );
it( 'should add #actionsView to the balloon and attach the balloon to the link element when collapsed selection is inside ' +
'that link',
() => {
setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
const linkElement = editor.editing.view.getDomRoot().querySelector( 'a' );
linkFeature._showUI();
expect( balloon.visibleView ).to.equal( actionsView );
sinon.assert.calledWithExactly( balloonAddSpy, {
view: actionsView,
position: {
target: linkElement
}
} );
} );
it( 'should disable #formView and #actionsView elements when link and unlink commands are disabled', () => {
setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
linkFeature._showUI();
editor.commands.get( 'link' ).isEnabled = true;
editor.commands.get( 'unlink' ).isEnabled = true;
expect( formView.urlInputView.isReadOnly ).to.be.false;
expect( formView.saveButtonView.isEnabled ).to.be.true;
expect( formView.cancelButtonView.isEnabled ).to.be.true;
expect( actionsView.unlinkButtonView.isEnabled ).to.be.true;
expect( actionsView.editButtonView.isEnabled ).to.be.true;
editor.commands.get( 'link' ).isEnabled = false;
editor.commands.get( 'unlink' ).isEnabled = false;
expect( formView.urlInputView.isReadOnly ).to.be.true;
expect( formView.saveButtonView.isEnabled ).to.be.false;
expect( formView.cancelButtonView.isEnabled ).to.be.true;
expect( actionsView.unlinkButtonView.isEnabled ).to.be.false;
expect( actionsView.editButtonView.isEnabled ).to.be.false;
} );
// https://github.com/ckeditor/ckeditor5-link/issues/78
it( 'should make sure the URL input in the #formView always stays in sync with the value of the command (selected link)', () => {
setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
// Mock some leftover value **in DOM**, e.g. after previous editing.
formView.urlInputView.inputView.element.value = 'leftover';
linkFeature._showUI();
actionsView.fire( 'edit' );
expect( formView.urlInputView.inputView.element.value ).to.equal( 'url' );
} );
// https://github.com/ckeditor/ckeditor5-link/issues/123
it( 'should make sure the URL input in the #formView always stays in sync with the value of the command (no link selected)', () => {
setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
linkFeature._showUI();
expect( formView.urlInputView.inputView.element.value ).to.equal( '' );
} );
describe( 'response to view#change', () => {
let view, viewDocument;
beforeEach( () => {
view = editor.editing.view;
viewDocument = view.document;
} );
it( 'should not duplicate #render listeners', () => {
setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
linkFeature._showUI();
view.change( () => {} );
linkFeature._hideUI();
linkFeature._showUI();
view.change( () => {} );
sinon.assert.calledTwice( spy );
} );
it( 'should update position on #render with low priority', () => {
setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
linkFeature._showUI();
const spyBefore = sinon.spy();
const spy = sinon.spy( balloon, 'updatePosition' );
const spyAfter = sinon.spy();
view.on( 'render', spyBefore, { priority: priorities.get( 'low' ) + 1 } );
view.on( 'render', spyAfter, { priority: 'low' } );
view.fire( 'render' );
sinon.assert.callOrder( spyBefore, spy, spyAfter );
} );
// https://github.com/ckeditor/ckeditor5-link/issues/113
it( 'updates the position of the panel – editing a link, then the selection remains in the link', () => {
setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
linkFeature._showUI();
const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
const root = viewDocument.getRoot();
const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
// Move selection to foo[].
view.change( writer => {
writer.setSelection( Range.createFromParentsAndOffsets( text, 3, text, 3 ), true );
} );
sinon.assert.calledOnce( spy );
sinon.assert.calledWithExactly( spy, {
target: view.domConverter.mapViewToDom( root.getChild( 0 ).getChild( 0 ) )
} );
} );
// https://github.com/ckeditor/ckeditor5-link/issues/113
it( 'updates the position of the panel – creating a new link, then the selection moved', () => {
setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
linkFeature._showUI();
const spy = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
const root = viewDocument.getRoot();
const text = root.getChild( 0 ).getChild( 0 );
view.change( writer => {
writer.setSelection( Range.createFromParentsAndOffsets( text, 3, text, 3 ), true );
} );
sinon.assert.calledOnce( spy );
sinon.assert.calledWithExactly( spy, {
target: editorElement.ownerDocument.getSelection().getRangeAt( 0 )
} );
} );
// https://github.com/ckeditor/ckeditor5-link/issues/113
it( 'hides of the panel – editing a link, then the selection moved out of the link', () => {
setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text>bar</paragraph>' );
linkFeature._showUI();
const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
const spyHide = testUtils.sinon.spy( linkFeature, '_hideUI' );
const root = viewDocument.getRoot();
const text = root.getChild( 0 ).getChild( 1 );
// Move selection to b[]ar.
view.change( writer => {
writer.setSelection( Range.createFromParentsAndOffsets( text, 1, text, 1 ), true );
} );
sinon.assert.calledOnce( spyHide );
sinon.assert.notCalled( spyUpdate );
} );
// https://github.com/ckeditor/ckeditor5-link/issues/113
it( 'hides the panel – editing a link, then the selection expands', () => {
setModelData( editor.model, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );
linkFeature._showUI();
const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
const spyHide = testUtils.sinon.spy( linkFeature, '_hideUI' );
const root = viewDocument.getRoot();
const text = root.getChild( 0 ).getChild( 0 ).getChild( 0 );
// Move selection to f[o]o.
view.change( writer => {
writer.setSelection( Range.createFromParentsAndOffsets( text, 1, text, 2 ), true );
} );
sinon.assert.calledOnce( spyHide );
sinon.assert.notCalled( spyUpdate );
} );
// https://github.com/ckeditor/ckeditor5-link/issues/113
it( 'hides the panel – creating a new link, then the selection moved to another parent', () => {
setModelData( editor.model, '<paragraph>f[]oo</paragraph><paragraph>bar</paragraph>' );
linkFeature._showUI();
const spyUpdate = testUtils.sinon.stub( balloon, 'updatePosition' ).returns( {} );
const spyHide = testUtils.sinon.spy( linkFeature, '_hideUI' );
const root = viewDocument.getRoot();
const text = root.getChild( 1 ).getChild( 0 );
// Move selection to f[o]o.
view.change( writer => {
writer.setSelection( Range.createFromParentsAndOffsets( text, 1, text, 2 ), true );
} );
sinon.assert.calledOnce( spyHide );
sinon.assert.notCalled( spyUpdate );
} );
} );
} );
describe( '_hideUI()', () => {
beforeEach( () => {
linkFeature._showUI();
} );
it( 'should remove the UI from the balloon', () => {
expect( balloon.hasView( formView ) ).to.be.true;
expect( balloon.hasView( actionsView ) ).to.be.true;
linkFeature._hideUI();
expect( balloon.hasView( formView ) ).to.be.false;
expect( balloon.hasView( actionsView ) ).to.be.false;
} );
it( 'should focus the `editable` by default', () => {
const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
linkFeature._hideUI();
// First call is from _removeFormView.
sinon.assert.calledTwice( spy );
} );
it( 'should not throw an error when views are not in the `balloon`', () => {
linkFeature._hideUI();
expect( () => {
linkFeature._hideUI();
} ).to.not.throw();
} );
it( 'should clear #render listener from the ViewDocument', () => {
const spy = sinon.spy();
linkFeature.listenTo( editor.editing.view, 'render', spy );
linkFeature._hideUI();
editor.editing.view.change( () => {} );
sinon.assert.notCalled( spy );
} );
} );
describe( 'keyboard support', () => {
it( 'should show the UI on Ctrl+K keystroke', () => {
const spy = testUtils.sinon.stub( linkFeature, '_showUI' ).returns( {} );
const command = editor.commands.get( 'link' );
command.isEnabled = false;
editor.keystrokes.press( {
keyCode: keyCodes.k,
ctrlKey: true,
preventDefault: sinon.spy(),
stopPropagation: sinon.spy()
} );
sinon.assert.notCalled( spy );
command.isEnabled = true;
editor.keystrokes.press( {
keyCode: keyCodes.k,
ctrlKey: true,
preventDefault: sinon.spy(),
stopPropagation: sinon.spy()
} );
sinon.assert.calledWithExactly( spy );
} );
it( 'should prevent default action on Ctrl+K keystroke', () => {
const preventDefaultSpy = sinon.spy();
const stopPropagationSpy = sinon.spy();
editor.keystrokes.press( {
keyCode: keyCodes.k,
ctrlKey: true,
preventDefault: preventDefaultSpy,
stopPropagation: stopPropagationSpy
} );
sinon.assert.calledOnce( preventDefaultSpy );
sinon.assert.calledOnce( stopPropagationSpy );
} );
it( 'should focus the the #actionsView on `Tab` key press when #actionsView is visible', () => {
const keyEvtData = {
keyCode: keyCodes.tab,
preventDefault: sinon.spy(),
stopPropagation: sinon.spy()
};
const normalPriorityTabCallbackSpy = sinon.spy();
const highestPriorityTabCallbackSpy = sinon.spy();
editor.keystrokes.set( 'Tab', normalPriorityTabCallbackSpy );
editor.keystrokes.set( 'Tab', highestPriorityTabCallbackSpy, { priority: 'highest' } );
// Balloon is invisible, form not focused.
actionsView.focusTracker.isFocused = false;
const spy = sinon.spy( actionsView, 'focus' );
editor.keystrokes.press( keyEvtData );
sinon.assert.notCalled( keyEvtData.preventDefault );
sinon.assert.notCalled( keyEvtData.stopPropagation );
sinon.assert.notCalled( spy );
sinon.assert.calledOnce( normalPriorityTabCallbackSpy );
sinon.assert.calledOnce( highestPriorityTabCallbackSpy );
// Balloon is visible, form focused.
linkFeature._showUI();
testUtils.sinon.stub( linkFeature, '_areActionsVisible' ).value( true );
actionsView.focusTracker.isFocused = true;
editor.keystrokes.press( keyEvtData );
sinon.assert.notCalled( keyEvtData.preventDefault );
sinon.assert.notCalled( keyEvtData.stopPropagation );
sinon.assert.notCalled( spy );
sinon.assert.calledTwice( normalPriorityTabCallbackSpy );
sinon.assert.calledTwice( highestPriorityTabCallbackSpy );
// Balloon is still visible, form not focused.
actionsView.focusTracker.isFocused = false;
editor.keystrokes.press( keyEvtData );
sinon.assert.calledOnce( keyEvtData.preventDefault );
sinon.assert.calledOnce( keyEvtData.stopPropagation );
sinon.assert.calledOnce( spy );
sinon.assert.calledTwice( normalPriorityTabCallbackSpy );
sinon.assert.calledThrice( highestPriorityTabCallbackSpy );
} );
it( 'should hide the UI after Esc key press (from editor) and not focus the editable', () => {
const spy = testUtils.sinon.spy( linkFeature, '_hideUI' );
const keyEvtData = {
keyCode: keyCodes.esc,
preventDefault: sinon.spy(),
stopPropagation: sinon.spy()
};
// Balloon is visible.
linkFeature._showUI();
editor.keystrokes.press( keyEvtData );
sinon.assert.calledWithExactly( spy );
} );
it( 'should not hide the UI after Esc key press (from editor) when UI is open but is not visible', () => {
const spy = testUtils.sinon.spy( linkFeature, '_hideUI' );
const keyEvtData = {
keyCode: keyCodes.esc,
preventDefault: () => {},
stopPropagation: () => {}
};
const viewMock = {
ready: true,
render: () => {},
destroy: () => {}
};
linkFeature._showUI();
// Some view precedes the link UI in the balloon.
balloon.add( { view: viewMock } );
editor.keystrokes.press( keyEvtData );
sinon.assert.notCalled( spy );
} );
} );
describe( 'mouse support', () => {
it( 'should hide the UI and not focus editable upon clicking outside the UI', () => {
const spy = testUtils.sinon.spy( linkFeature, '_hideUI' );
linkFeature._showUI( true );
document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
sinon.assert.calledWithExactly( spy );
} );
it( 'should not hide the UI upon clicking inside the the UI', () => {
const spy = testUtils.sinon.spy( linkFeature, '_hideUI' );
linkFeature._showUI( true );
balloon.view.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
sinon.assert.notCalled( spy );
} );
describe( 'clicking on editable', () => {
let observer, spy;
beforeEach( () => {
observer = editor.editing.view.getObserver( ClickObserver );
editor.model.schema.extend( '$text', { allowIn: '$root' } );
spy = testUtils.sinon.stub( linkFeature, '_showUI' ).returns( {} );
} );
it( 'should show the UI when collapsed selection is inside link element', () => {
setModelData( editor.model, '<$text linkHref="url">fo[]o</$text>' );
observer.fire( 'click', { target: document.body } );
sinon.assert.calledWithExactly( spy );
} );
it( 'should show the UI when selection exclusively encloses a link element (#1)', () => {
setModelData( editor.model, '[<$text linkHref="url">foo</$text>]' );
observer.fire( 'click', { target: {} } );
sinon.assert.calledWithExactly( spy );
} );
it( 'should show the UI when selection exclusively encloses a link element (#2)', () => {
setModelData( editor.model, '<$text linkHref="url">[foo]</$text>' );
observer.fire( 'click', { target: {} } );
sinon.assert.calledWithExactly( spy );
} );
it( 'should do nothing when selection is not inside link element', () => {
setModelData( editor.model, '[]' );
observer.fire( 'click', { target: {} } );
sinon.assert.notCalled( spy );
} );
it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#1)', () => {
setModelData( editor.model, '<$text linkHref="url">f[o]o</$text>' );
observer.fire( 'click', { target: {} } );
sinon.assert.notCalled( spy );
} );
it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#2)', () => {
setModelData( editor.model, '<$text linkHref="url">[fo]o</$text>' );
observer.fire( 'click', { target: {} } );
sinon.assert.notCalled( spy );
} );
it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#3)', () => {
setModelData( editor.model, '<$text linkHref="url">f[oo]</$text>' );
observer.fire( 'click', { target: {} } );
sinon.assert.notCalled( spy );
} );
it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#4)', () => {
setModelData( editor.model, 'ba[r<$text linkHref="url">foo]</$text>' );
observer.fire( 'click', { target: {} } );
sinon.assert.notCalled( spy );
} );
it( 'should do nothing when selection is non-collapsed and doesn\'t enclose a link element (#5)', () => {
setModelData( editor.model, 'ba[r<$text linkHref="url">foo</$text>]' );
observer.fire( 'click', { target: {} } );
sinon.assert.notCalled( spy );
} );
} );
} );
describe( 'actions view', () => {
let focusEditableSpy;
beforeEach( () => {
focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
} );
it( 'should mark the editor UI as focused when the #actionsView is focused', () => {
linkFeature._showUI();
linkFeature._removeFormView();
expect( balloon.visibleView ).to.equal( actionsView );
editor.ui.focusTracker.isFocused = false;
actionsView.element.dispatchEvent( new Event( 'focus' ) );
expect( editor.ui.focusTracker.isFocused ).to.be.true;
} );
describe( 'binding', () => {
it( 'should show the #formView on #edit event and select the URL input field', () => {
linkFeature._showUI();
linkFeature._removeFormView();
const selectSpy = testUtils.sinon.spy( formView.urlInputView, 'select' );
actionsView.fire( 'edit' );
expect( balloon.visibleView ).to.equal( formView );
sinon.assert.calledOnce( selectSpy );
} );
it( 'should execute unlink command on actionsView#unlink event', () => {
const executeSpy = testUtils.sinon.spy( editor, 'execute' );
actionsView.fire( 'unlink' );
expect( executeSpy.calledOnce ).to.be.true;
expect( executeSpy.calledWithExactly( 'unlink' ) ).to.be.true;
} );
it( 'should hide and focus editable on actionsView#unlink event', () => {
linkFeature._showUI();
linkFeature._removeFormView();
// Removing the form would call the focus spy.
focusEditableSpy.resetHistory();
actionsView.fire( 'unlink' );
expect( balloon.visibleView ).to.be.null;
expect( focusEditableSpy.calledOnce ).to.be.true;
} );
it( 'should hide after Esc key press', () => {
const keyEvtData = {
keyCode: keyCodes.esc,
preventDefault: sinon.spy(),
stopPropagation: sinon.spy()
};
linkFeature._showUI();
linkFeature._removeFormView();
// Removing the form would call the focus spy.
focusEditableSpy.resetHistory();
actionsView.keystrokes.press( keyEvtData );
expect( balloon.visibleView ).to.equal( null );
expect( focusEditableSpy.calledOnce ).to.be.true;
} );
} );
} );
describe( 'link form view', () => {
let focusEditableSpy;
beforeEach( () => {
focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
} );
it( 'should mark the editor UI as focused when the #formView is focused', () => {
linkFeature._showUI();
expect( balloon.visibleView ).to.equal( formView );
editor.ui.focusTracker.isFocused = false;
formView.element.dispatchEvent( new Event( 'focus' ) );
expect( editor.ui.focusTracker.isFocused ).to.be.true;
} );
describe( 'binding', () => {
beforeEach( () => {
setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
} );
it( 'should bind formView.urlInputView#value to link command value', () => {
const command = editor.commands.get( 'link' );
expect( formView.urlInputView.value ).to.undefined;
command.value = 'http://cksource.com';
expect( formView.urlInputView.value ).to.equal( 'http://cksource.com' );
} );
it( 'should execute link command on formView#submit event', () => {
const executeSpy = testUtils.sinon.spy( editor, 'execute' );
formView.urlInputView.value = 'http://ckeditor.com';
expect( formView.urlInputView.inputView.element.value ).to.equal( 'http://ckeditor.com' );
formView.urlInputView.inputView.element.value = 'http://cksource.com';
formView.fire( 'submit' );
expect( executeSpy.calledOnce ).to.be.true;
expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.be.true;
} );
it( 'should hide and reveal the #actionsView on formView#submit event', () => {
linkFeature._showUI();
formView.fire( 'submit' );
expect( balloon.visibleView ).to.equal( actionsView );
expect( focusEditableSpy.calledOnce ).to.be.true;
} );
it( 'should hide and reveal the #actionsView on formView#cancel event', () => {
linkFeature._showUI();
formView.fire( 'cancel' );
expect( balloon.visibleView ).to.equal( actionsView );
expect( focusEditableSpy.calledOnce ).to.be.true;
} );
it( 'should hide after Esc key press', () => {
const keyEvtData = {
keyCode: keyCodes.esc,
preventDefault: sinon.spy(),
stopPropagation: sinon.spy()
};
linkFeature._showUI();
formView.keystrokes.press( keyEvtData );
expect( balloon.visibleView ).to.equal( actionsView );
expect( focusEditableSpy.calledOnce ).to.be.true;
} );
} );
} );
} );