| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* globals document */
- import ClassicTestEditor from 'tests/core/_utils/classictesteditor.js';
- import testUtils from 'tests/core/_utils/utils.js';
- import { keyCodes } from 'ckeditor5/utils/keyboard.js';
- import { setData as setModelData } from 'ckeditor5/engine/dev-utils/model.js';
- import Link from 'ckeditor5/link/link.js';
- import LinkEngine from 'ckeditor5/link/linkengine.js';
- import ButtonView from 'ckeditor5/ui/button/buttonview.js';
- import BalloonPanelView from 'ckeditor5/ui/balloonpanel/balloonpanelview.js';
- import Range from 'ckeditor5/engine/view/range.js';
- import ClickObserver from 'ckeditor5/engine/view/observer/clickobserver.js';
- testUtils.createSinonSandbox();
- describe( 'Link', () => {
- let editor, linkFeature, linkButton, unlinkButton, balloonPanelView, formView, editorElement;
- beforeEach( () => {
- editorElement = document.createElement( 'div' );
- document.body.appendChild( editorElement );
- return ClassicTestEditor.create( editorElement, {
- plugins: [ Link ]
- } )
- .then( newEditor => {
- newEditor.editing.view.attachDomRoot( editorElement );
- editor = newEditor;
- linkFeature = editor.plugins.get( Link );
- linkButton = editor.ui.componentFactory.create( 'link' );
- unlinkButton = editor.ui.componentFactory.create( 'unlink' );
- balloonPanelView = linkFeature.balloonPanelView;
- formView = linkFeature.formView;
- } );
- } );
- afterEach( () => {
- return editor.destroy();
- } );
- it( 'should be loaded', () => {
- expect( linkFeature ).to.instanceOf( Link );
- } );
- it( 'should load LinkEngine', () => {
- expect( editor.plugins.get( LinkEngine ) ).to.instanceOf( LinkEngine );
- } );
- it( 'should register click observer', () => {
- expect( editor.editing.view.getObserver( ClickObserver ) ).to.instanceOf( ClickObserver );
- } );
- describe( 'link toolbar button', () => {
- it( 'should register link button', () => {
- expect( linkButton ).to.instanceOf( ButtonView );
- } );
- it( 'should bind linkButtonView to 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 open panel on linkButtonView execute event', () => {
- linkButton.fire( 'execute' );
- expect( linkFeature.balloonPanelView.isVisible ).to.true;
- } );
- it( 'should open panel attached to the link element, when collapsed selection is inside link element', () => {
- const attachToSpy = testUtils.sinon.spy( balloonPanelView, 'attachTo' );
- editor.document.schema.allow( { name: '$text', inside: '$root' } );
- setModelData( editor.document, '<$text linkHref="url">some[] url</$text>' );
- editor.editing.view.isFocused = true;
- linkButton.fire( 'execute' );
- const linkElement = editorElement.querySelector( 'a' );
- expect( attachToSpy.calledWithExactly( linkElement, editorElement ) ).to.true;
- } );
- it( 'should open panel attached to the selection, when there is non-collapsed selection', () => {
- const attachToSpy = testUtils.sinon.spy( balloonPanelView, 'attachTo' );
- editor.document.schema.allow( { name: '$text', inside: '$root' } );
- setModelData( editor.document, 'so[me ur]l' );
- editor.editing.view.isFocused = true;
- linkButton.fire( 'execute' );
- const selectedRange = editorElement.ownerDocument.getSelection().getRangeAt( 0 );
- expect( attachToSpy.calledWithExactly( selectedRange, editorElement ) ).to.true;
- } );
- it( 'should select panel input value when panel is opened', () => {
- const selectUrlInputSpy = testUtils.sinon.spy( linkFeature.formView.urlInputView, 'select' );
- editor.editing.view.isFocused = true;
- linkButton.fire( 'execute' );
- expect( selectUrlInputSpy.calledOnce ).to.true;
- } );
- } );
- describe( 'unlink toolbar button', () => {
- it( 'should register unlink button', () => {
- expect( unlinkButton ).to.instanceOf( ButtonView );
- } );
- it( 'should bind unlinkButtonView to unlink command', () => {
- const command = editor.commands.get( 'unlink' );
- command.isEnabled = true;
- expect( unlinkButton.isEnabled ).to.be.true;
- command.isEnabled = false;
- expect( unlinkButton.isEnabled ).to.be.false;
- } );
- it( 'should execute unlink command on unlinkButtonView execute event', () => {
- const executeSpy = testUtils.sinon.spy( editor, 'execute' );
- unlinkButton.fire( 'execute' );
- expect( executeSpy.calledOnce ).to.true;
- expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
- } );
- } );
- describe( 'link balloon panel', () => {
- let hidePanelSpy, focusEditableSpy;
- beforeEach( () => {
- hidePanelSpy = testUtils.sinon.spy( balloonPanelView, 'hide' );
- focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
- } );
- it( 'should be created', () => {
- expect( balloonPanelView ).to.instanceOf( BalloonPanelView );
- } );
- it( 'should be appended to the document body', () => {
- expect( document.body.contains( balloonPanelView.element ) );
- } );
- it( 'should open with selected url input on `CTRL+K` keystroke', () => {
- const selectUrlInputSpy = testUtils.sinon.spy( linkFeature.formView.urlInputView, 'select' );
- editor.keystrokes.press( { keyCode: keyCodes.k, ctrlKey: true } );
- expect( balloonPanelView.isVisible ).to.true;
- expect( selectUrlInputSpy.calledOnce ).to.true;
- } );
- it( 'should add balloon panel element to focus tracker', () => {
- editor.ui.focusTracker.isFocused = false;
- balloonPanelView.element.dispatchEvent( new Event( 'focus' ) );
- expect( editor.ui.focusTracker.isFocused ).to.true;
- } );
- describe( 'close listeners', () => {
- describe( 'keyboard', () => {
- it( 'should close after `ESC` press', () => {
- balloonPanelView.isVisible = true;
- dispatchKeyboardEvent( document, 'keydown', keyCodes.esc );
- expect( hidePanelSpy.calledOnce ).to.true;
- expect( focusEditableSpy.calledOnce ).to.true;
- } );
- } );
- describe( 'mouse', () => {
- it( 'should close and not focus editable on click outside the panel', () => {
- balloonPanelView.isVisible = true;
- document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
- expect( hidePanelSpy.calledOnce ).to.true;
- expect( focusEditableSpy.notCalled ).to.true;
- } );
- it( 'should not close on click inside the panel', () => {
- balloonPanelView.isVisible = true;
- balloonPanelView.element.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
- expect( hidePanelSpy.notCalled ).to.true;
- } );
- } );
- } );
- describe( 'click on editable', () => {
- it( 'should open with not selected url input when collapsed selection is inside link element', () => {
- const selectUrlInputSpy = testUtils.sinon.spy( linkFeature.formView.urlInputView, 'select' );
- const observer = editor.editing.view.getObserver( ClickObserver );
- editor.document.schema.allow( { name: '$text', inside: '$root' } );
- setModelData( editor.document, '<$text linkHref="url">fo[]o</$text>' );
- observer.fire( 'click', { target: document.body } );
- expect( balloonPanelView.isVisible ).to.true;
- expect( selectUrlInputSpy.notCalled ).to.true;
- } );
- it( 'should keep open and update position until collapsed selection stay inside the same link element', () => {
- const observer = editor.editing.view.getObserver( ClickObserver );
- editor.document.schema.allow( { name: '$text', inside: '$root' } );
- setModelData( editor.document, '<$text linkHref="url">b[]ar</$text>' );
- const root = editor.editing.view.getRoot();
- const text = root.getChild( 0 ).getChild( 0 );
- observer.fire( 'click', { target: document.body } );
- expect( balloonPanelView.isVisible ).to.true;
- const attachToSpy = testUtils.sinon.spy( balloonPanelView, 'attachTo' );
- editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
- editor.editing.view.render();
- expect( balloonPanelView.isVisible ).to.true;
- expect( attachToSpy.calledOnce ).to.true;
- } );
- it( 'should close when selection goes outside the link element', () => {
- const observer = editor.editing.view.getObserver( ClickObserver );
- editor.document.schema.allow( { name: '$text', inside: '$root' } );
- setModelData( editor.document, 'foo <$text linkHref="url">b[]ar</$text>' );
- const root = editor.editing.view.getRoot();
- const text = root.getChild( 0 );
- observer.fire( 'click', { target: document.body } );
- expect( balloonPanelView.isVisible ).to.true;
- editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 3, text, 3 ) ], true );
- editor.editing.view.render();
- expect( balloonPanelView.isVisible ).to.false;
- } );
- it( 'should close when selection goes to the other link element with the same href', () => {
- const observer = editor.editing.view.getObserver( ClickObserver );
- editor.document.schema.allow( { name: '$text', inside: '$root' } );
- setModelData( editor.document, '<$text linkHref="url">f[]oo</$text> bar <$text linkHref="url">biz</$text>' );
- const root = editor.editing.view.getRoot();
- const text = root.getChild( 2 ).getChild( 0 );
- observer.fire( 'click', { target: document.body } );
- expect( balloonPanelView.isVisible ).to.true;
- editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
- editor.editing.view.render();
- expect( balloonPanelView.isVisible ).to.false;
- } );
- it( 'should close when selection becomes non-collapsed', () => {
- const observer = editor.editing.view.getObserver( ClickObserver );
- editor.document.schema.allow( { name: '$text', inside: '$root' } );
- setModelData( editor.document, '<$text linkHref="url">f[]oo</$text>' );
- const root = editor.editing.view.getRoot();
- const text = root.getChild( 0 ).getChild( 0 );
- observer.fire( 'click', { target: {} } );
- expect( balloonPanelView.isVisible ).to.true;
- editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 2 ) ] );
- editor.editing.view.render();
- expect( balloonPanelView.isVisible ).to.false;
- } );
- it( 'should stop updating position after close', () => {
- const observer = editor.editing.view.getObserver( ClickObserver );
- editor.document.schema.allow( { name: '$text', inside: '$root' } );
- setModelData( editor.document, '<$text linkHref="url">b[]ar</$text>' );
- const root = editor.editing.view.getRoot();
- const text = root.getChild( 0 ).getChild( 0 );
- observer.fire( 'click', { target: {} } );
- expect( balloonPanelView.isVisible ).to.true;
- balloonPanelView.isVisible = false;
- const attachToSpy = testUtils.sinon.spy( balloonPanelView, 'attachTo' );
- editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 2, text, 2 ) ], true );
- editor.editing.view.render();
- expect( attachToSpy.notCalled ).to.true;
- } );
- it( 'should not open when selection is not inside link element', () => {
- const observer = editor.editing.view.getObserver( ClickObserver );
- setModelData( editor.document, '[]' );
- observer.fire( 'click', { target: {} } );
- expect( balloonPanelView.isVisible ).to.false;
- } );
- it( 'should not open when selection is non-collapsed', () => {
- const observer = editor.editing.view.getObserver( ClickObserver );
- editor.document.schema.allow( { name: '$text', inside: '$root' } );
- setModelData( editor.document, '<$text linkHref="url">f[o]o</$text>' );
- observer.fire( 'click', { target: document.body } );
- expect( balloonPanelView.isVisible ).to.false;
- } );
- } );
- } );
- describe( 'link form', () => {
- let hidePanelSpy, focusEditableSpy;
- beforeEach( () => {
- hidePanelSpy = testUtils.sinon.spy( balloonPanelView, 'hide' );
- focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
- } );
- describe( 'binding', () => {
- 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.true;
- expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.true;
- } );
- it( 'should hide and focus editable on formView#submit event', () => {
- formView.fire( 'submit' );
- expect( hidePanelSpy.calledOnce ).to.true;
- expect( focusEditableSpy.calledOnce ).to.true;
- } );
- it( 'should execute unlink command on formView#unlink event', () => {
- const executeSpy = testUtils.sinon.spy( editor, 'execute' );
- formView.fire( 'unlink' );
- expect( executeSpy.calledOnce ).to.true;
- expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
- } );
- it( 'should hide and focus editable on formView#unlink event', () => {
- formView.fire( 'unlink' );
- expect( hidePanelSpy.calledOnce ).to.true;
- expect( focusEditableSpy.calledOnce ).to.true;
- } );
- it( 'should hide and focus editable on formView#cancel event', () => {
- formView.fire( 'cancel' );
- expect( hidePanelSpy.calledOnce ).to.true;
- expect( focusEditableSpy.calledOnce ).to.true;
- } );
- } );
- } );
- } );
- // Creates and dispatches keyboard event with specified keyCode.
- //
- // @private
- // @param {EventTarget} eventTarget
- // @param {String} eventName
- // @param {Number} keyCode
- function dispatchKeyboardEvent( element, eventName, keyCode ) {
- const event = document.createEvent( 'Events' );
- event.initEvent( eventName, true, true );
- event.which = keyCode;
- event.keyCode = keyCode;
- element.dispatchEvent( event );
- }
|