| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- /**
- * @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 '/tests/engine/_utils/model.js';
- import Link from '/ckeditor5/link/link.js';
- import LinkEngine from '/ckeditor5/link/linkengine.js';
- import Button from '/ckeditor5/ui/button/button.js';
- import LinkBalloonPanel from '/ckeditor5/link/ui/linkballoonpanel.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, balloonPanel, editorElement;
- beforeEach( () => {
- editorElement = document.createElement( 'div' );
- document.body.appendChild( editorElement );
- return ClassicTestEditor.create( editorElement, {
- features: [ Link ]
- } )
- .then( newEditor => {
- newEditor.editing.view.attachDomRoot( editorElement );
- editor = newEditor;
- linkFeature = editor.plugins.get( Link );
- linkButton = editor.ui.featureComponents.create( 'link' );
- unlinkButton = editor.ui.featureComponents.create( 'unlink' );
- balloonPanel = linkFeature.balloonPanel;
- } );
- } );
- 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 feature component', () => {
- expect( linkButton ).to.instanceOf( Button );
- } );
- it( 'should bind linkButton#model to link command', () => {
- const model = linkButton.model;
- const command = editor.commands.get( 'link' );
- expect( model.isEnabled ).to.be.true;
- command.isEnabled = false;
- expect( model.isEnabled ).to.be.false;
- } );
- it( 'should open panel on linkButton#model execute event, when editor is focused', () => {
- editor.editing.view.isFocused = true;
- linkButton.model.fire( 'execute' );
- expect( linkFeature.balloonPanel.view.isVisible ).to.true;
- } );
- it( 'should not open panel on linkButton#model execute event, when editor is not focused', () => {
- editor.editing.view.isFocused = false;
- linkButton.model.fire( 'execute' );
- expect( linkFeature.balloonPanel.view.isVisible ).to.false;
- } );
- it( 'should open panel attached to the link element, when collapsed selection is inside link element', () => {
- const attachToSpy = testUtils.sinon.spy( balloonPanel.view, 'attachTo' );
- editor.document.schema.allow( { name: '$text', inside: '$root' } );
- setModelData( editor.document, '<$text linkHref="url">some[] url</$text>' );
- editor.editing.view.isFocused = true;
- linkButton.model.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( balloonPanel.view, 'attachTo' );
- editor.document.schema.allow( { name: '$text', inside: '$root' } );
- setModelData( editor.document, 'so[me ur]l' );
- editor.editing.view.isFocused = true;
- linkButton.model.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( balloonPanel.urlInput.view, 'select' );
- editor.editing.view.isFocused = true;
- linkButton.model.fire( 'execute' );
- expect( selectUrlInputSpy.calledOnce ).to.true;
- } );
- } );
- describe( 'unlink toolbar button', () => {
- it( 'should register unlink feature component', () => {
- expect( unlinkButton ).to.instanceOf( Button );
- } );
- it( 'should bind unlinkButton#model to unlink command', () => {
- const model = unlinkButton.model;
- const command = editor.commands.get( 'unlink' );
- expect( model.isEnabled ).to.false;
- command.hasValue = true;
- expect( model.isEnabled ).to.true;
- } );
- it( 'should execute unlink command on unlinkButton#model execute event', () => {
- const executeSpy = testUtils.sinon.spy( editor, 'execute' );
- unlinkButton.model.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( balloonPanel.view, 'hide' );
- focusEditableSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
- } );
- it( 'should be created', () => {
- expect( balloonPanel ).to.instanceOf( LinkBalloonPanel );
- } );
- it( 'should be appended to the document body', () => {
- expect( document.body.contains( balloonPanel.view.element ) );
- } );
- it( 'should open with selected url input on `CTRL+L` keystroke', () => {
- const selectUrlInputSpy = testUtils.sinon.spy( balloonPanel.urlInput.view, 'select' );
- editor.keystrokes.press( { keyCode: keyCodes.l, ctrlKey: true } );
- expect( balloonPanel.view.model.isVisible ).to.true;
- expect( selectUrlInputSpy.calledOnce ).to.true;
- } );
- describe( 'binding', () => {
- it( 'should bind balloonPanel#model to link command', () => {
- const model = balloonPanel.model;
- const command = editor.commands.get( 'link' );
- expect( model.url ).to.undefined;
- command.value = 'http://cksource.com';
- expect( model.url ).to.equal( 'http://cksource.com' );
- } );
- it( 'should execute link command on balloonPanel#model executeLink event', () => {
- const executeSpy = testUtils.sinon.spy( editor, 'execute' );
- balloonPanel.model.url = 'http://cksource.com';
- balloonPanel.model.fire( 'executeLink' );
- expect( executeSpy.calledOnce ).to.true;
- expect( executeSpy.calledWithExactly( 'link', 'http://cksource.com' ) ).to.true;
- } );
- it( 'should hide and focus editable on balloonPanel#model executeLink event', () => {
- balloonPanel.model.fire( 'executeLink' );
- expect( hidePanelSpy.calledOnce ).to.true;
- expect( focusEditableSpy.calledOnce ).to.true;
- } );
- it( 'should execute unlink command on balloonPanel#model executeUnlink event', () => {
- const executeSpy = testUtils.sinon.spy( editor, 'execute' );
- balloonPanel.model.fire( 'executeUnlink' );
- expect( executeSpy.calledOnce ).to.true;
- expect( executeSpy.calledWithExactly( 'unlink' ) ).to.true;
- } );
- it( 'should hide and focus editable on balloonPanel#model executeUnlink event', () => {
- balloonPanel.model.fire( 'executeUnlink' );
- expect( hidePanelSpy.calledOnce ).to.true;
- expect( focusEditableSpy.calledOnce ).to.true;
- } );
- it( 'should hide and focus editable on balloonPanel#model executeCancel event', () => {
- balloonPanel.model.fire( 'executeCancel' );
- expect( hidePanelSpy.calledOnce ).to.true;
- expect( focusEditableSpy.calledOnce ).to.true;
- } );
- } );
- describe( 'close listeners', () => {
- describe( 'keyboard', () => {
- it( 'should listen keyboard events when is open', () => {
- const escCloseSpy = testUtils.sinon.spy( linkFeature, '_closePanelOnClick' );
- balloonPanel.view.model.isVisible = true;
- document.dispatchEvent( new Event( 'keydown' ) );
- expect( escCloseSpy.calledOnce ).to.true;
- } );
- it( 'should not listen keyboard events when is closed', () => {
- const escCloseSpy = testUtils.sinon.spy( linkFeature, '_closePanelOnClick' );
- balloonPanel.view.model.isVisible = false;
- document.dispatchEvent( new Event( 'keydown' ) );
- expect( escCloseSpy.calledOnce ).to.false;
- } );
- it( 'should stop listening keyboard events after close', () => {
- const escCloseSpy = testUtils.sinon.spy( linkFeature, '_closePanelOnClick' );
- balloonPanel.view.model.isVisible = true;
- balloonPanel.view.model.isVisible = false;
- document.dispatchEvent( new Event( 'keydown' ) );
- expect( escCloseSpy.notCalled ).to.true;
- } );
- } );
- describe( 'mouse', () => {
- it( 'should not close on click inside the panel', () => {
- balloonPanel.view.model.isVisible = true;
- balloonPanel.view.element.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
- expect( hidePanelSpy.notCalled ).to.true;
- } );
- it( 'should close and not focus editable on click outside the panel', () => {
- balloonPanel.view.model.isVisible = true;
- document.dispatchEvent( new Event( 'mouseup' ) );
- expect( hidePanelSpy.calledOnce ).to.true;
- expect( focusEditableSpy.notCalled ).to.true;
- } );
- it( 'should not listen mouse events before open', () => {
- balloonPanel.view.model.isVisible = false;
- document.dispatchEvent( new Event( 'mouseup' ) );
- expect( hidePanelSpy.notCalled ).to.true;
- } );
- it( 'should stop listening mouse events after closed', () => {
- balloonPanel.view.model.isVisible = true;
- balloonPanel.view.model.isVisible = false;
- document.dispatchEvent( new Event( 'mouseup' ) );
- 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( balloonPanel.urlInput.view, '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( balloonPanel.view.model.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( balloonPanel.view.model.isVisible ).to.true;
- const attachToSpy = testUtils.sinon.spy( balloonPanel.view, 'attachTo' );
- editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
- editor.editing.view.render();
- expect( balloonPanel.view.model.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( balloonPanel.view.model.isVisible ).to.true;
- editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 3, text, 3 ) ], true );
- editor.editing.view.render();
- expect( balloonPanel.view.model.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( balloonPanel.view.model.isVisible ).to.true;
- editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 1 ) ], true );
- editor.editing.view.render();
- expect( balloonPanel.view.model.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( balloonPanel.view.model.isVisible ).to.true;
- editor.editing.view.selection.setRanges( [ Range.createFromParentsAndOffsets( text, 1, text, 2 ) ] );
- editor.editing.view.render();
- expect( balloonPanel.view.model.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( balloonPanel.view.model.isVisible ).to.true;
- balloonPanel.view.model.isVisible = false;
- const attachToSpy = testUtils.sinon.spy( balloonPanel.view, '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( balloonPanel.view.model.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( balloonPanel.view.model.isVisible ).to.false;
- } );
- } );
- describe( '_closePanelOnClick', () => {
- it( 'should hide panel and focus editable on ESC press', () => {
- const eventInfo = {};
- const domEvent = { keyCode: 27 };
- linkFeature._closePanelOnClick( eventInfo, domEvent );
- expect( hidePanelSpy.calledOnce ).to.true;
- expect( focusEditableSpy.calledOnce ).to.true;
- } );
- } );
- } );
- } );
|