| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- /**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import ModelTestEditor from 'ckeditor5-core/tests/_utils/modeltesteditor';
- import UnlinkCommand from 'ckeditor5-link/src/unlinkcommand';
- import { setData, getData } from 'ckeditor5-engine/src/dev-utils/model';
- import testUtils from 'ckeditor5-core/tests/_utils/utils';
- testUtils.createSinonSandbox();
- describe( 'UnlinkCommand', () => {
- let editor, document, command;
- beforeEach( () => {
- return ModelTestEditor.create()
- .then( newEditor => {
- editor = newEditor;
- document = editor.document;
- command = new UnlinkCommand( editor );
- // Allow text in $root.
- document.schema.allow( { name: '$text', inside: '$root' } );
- // Allow text with `linkHref` attribute in paragraph.
- document.schema.registerItem( 'p', '$block' );
- document.schema.allow( { name: '$text', attributes: 'linkHref', inside: '$root' } );
- } );
- } );
- afterEach( () => {
- command.destroy();
- } );
- describe( 'constructor()', () => {
- it( 'should listen on selection attribute change and refresh state', () => {
- const refreshStateSpy = testUtils.sinon.spy( command, 'refreshState' );
- document.selection.fire( 'change:attribute' );
- expect( refreshStateSpy.calledOnce ).to.true;
- } );
- } );
- describe( '_doExecute', () => {
- describe( 'non-collapsed selection', () => {
- it( 'should remove `linkHref` attribute from selected text', () => {
- setData( document, '<$text linkHref="url">f[ooba]r</$text>' );
- command._doExecute();
- expect( getData( document ) ).to.equal( '<$text linkHref="url">f</$text>[ooba]<$text linkHref="url">r</$text>' );
- } );
- it( 'should remove `linkHref` attribute from selected text and do not modified other attributes', () => {
- setData( document, '<$text bold="true" linkHref="url">f[ooba]r</$text>' );
- command._doExecute();
- expect( getData( document ) ).to.equal(
- '<$text bold="true" linkHref="url">f</$text>' +
- '[<$text bold="true">ooba</$text>]' +
- '<$text bold="true" linkHref="url">r</$text>'
- );
- } );
- it( 'should remove `linkHref` attribute from selected text when attributes have different value', () => {
- setData( document, '[<$text linkHref="url">foo</$text><$text linkHref="other url">bar</$text>]' );
- command._doExecute();
- expect( getData( document ) ).to.equal( '[foobar]' );
- } );
- it( 'should remove `linkHref` attribute from selection', () => {
- setData( document, '<$text linkHref="url">f[ooba]r</$text>' );
- command._doExecute();
- expect( document.selection.hasAttribute( 'linkHref' ) ).to.false;
- } );
- } );
- describe( 'collapsed selection', () => {
- it( 'should remove `linkHref` attribute from selection siblings with the same attribute value', () => {
- setData( document, '<$text linkHref="url">foo[]bar</$text>' );
- command._doExecute();
- expect( getData( document ) ).to.equal( 'foo[]bar' );
- } );
- it(
- 'should remove `linkHref` attribute from selection siblings with the same attribute value and do not modify other attributes',
- () => {
- setData(
- document,
- '<$text linkHref="other url">fo</$text>' +
- '<$text linkHref="url">o[]b</$text>' +
- '<$text linkHref="other url">ar</$text>'
- );
- command._doExecute();
- expect( getData( document ) ).to.equal(
- '<$text linkHref="other url">fo</$text>' +
- 'o[]b' +
- '<$text linkHref="other url">ar</$text>'
- );
- } );
- it( 'should do nothing with nodes with the same `linkHref` value when there is a node with different value `linkHref` ' +
- 'attribute between', () => {
- setData(
- document,
- '<$text linkHref="same url">f</$text>' +
- '<$text linkHref="other url">o</$text>' +
- '<$text linkHref="same url">o[]b</$text>' +
- '<$text linkHref="other url">a</$text>' +
- '<$text linkHref="same url">r</$text>'
- );
- command._doExecute();
- expect( getData( document ) )
- .to.equal(
- '<$text linkHref="same url">f</$text>' +
- '<$text linkHref="other url">o</$text>' +
- 'o[]b' +
- '<$text linkHref="other url">a</$text>' +
- '<$text linkHref="same url">r</$text>'
- );
- } );
- it(
- 'should remove `linkHref` attribute from selection siblings with the same attribute value and do nothing with other ' +
- 'attributes',
- () => {
- setData(
- document,
- '<$text linkHref="url">f</$text>' +
- '<$text bold="true" linkHref="url">o</$text>' +
- '<$text linkHref="url">o[]b</$text>' +
- '<$text bold="true" linkHref="url">a</$text>' +
- '<$text linkHref="url">r</$text>'
- );
- command._doExecute();
- expect( getData( document ) ).to.equal(
- 'f' +
- '<$text bold="true">o</$text>' +
- 'o[]b' +
- '<$text bold="true">a</$text>' +
- 'r'
- );
- } );
- it( 'should remove `linkHref` attribute from selection siblings only in the same parent as selection parent', () => {
- setData(
- document,
- '<p><$text linkHref="url">bar</$text></p>' +
- '<p><$text linkHref="url">fo[]o</$text></p>' +
- '<p><$text linkHref="url">bar</$text></p>'
- );
- command._doExecute();
- expect( getData( document ) ).to.equal(
- '<p><$text linkHref="url">bar</$text></p>' +
- '<p>fo[]o</p>' +
- '<p><$text linkHref="url">bar</$text></p>'
- );
- } );
- it( 'should remove `linkHref` attribute from selection siblings when selection is at the end of link', () => {
- setData( document, '<$text linkHref="url">foobar</$text>[]' );
- command._doExecute();
- expect( getData( document ) ).to.equal( 'foobar[]' );
- } );
- it( 'should remove `linkHref` attribute from selection siblings when selection is at the beginning of link', () => {
- setData( document, '[]<$text linkHref="url">foobar</$text>' );
- command._doExecute();
- expect( getData( document ) ).to.equal( '[]foobar' );
- } );
- it( 'should remove `linkHref` attribute from selection siblings on the left side when selection is between two elements with ' +
- 'different `linkHref` attributes',
- () => {
- setData( document, '<$text linkHref="url">foo</$text>[]<$text linkHref="other url">bar</$text>' );
- command._doExecute();
- expect( getData( document ) ).to.equal( 'foo[]<$text linkHref="other url">bar</$text>' );
- } );
- it( 'should remove `linkHref` attribute from selection', () => {
- setData( document, '<$text linkHref="url">foo[]bar</$text>' );
- command._doExecute();
- expect( document.selection.hasAttribute( 'linkHref' ) ).to.false;
- } );
- } );
- } );
- describe( '_checkEnabled', () => {
- it( 'should return false when selection has `linkHref` attribute', () => {
- document.selection.setAttribute( 'linkHref', 'value' );
- expect( command._checkEnabled() ).to.true;
- } );
- it( 'should return false when selection doesn\'t have `linkHref` attribute', () => {
- document.selection.removeAttribute( 'linkHref' );
- expect( command._checkEnabled() ).to.false;
- } );
- } );
- } );
|