| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import LinkEditing from '../src/linkediting';
- import LinkCommand from '../src/linkcommand';
- import UnlinkCommand from '../src/unlinkcommand';
- import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- import Enter from '@ckeditor/ckeditor5-enter/src/enter';
- import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
- import { isLinkElement } from '../src/utils';
- import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
- import { downcastAttributeToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
- /* global document */
- describe( 'LinkEditing', () => {
- let editor, model, view;
- beforeEach( () => {
- return VirtualTestEditor
- .create( {
- plugins: [ Paragraph, LinkEditing, Enter ]
- } )
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- view = editor.editing.view;
- } );
- } );
- it( 'should be loaded', () => {
- expect( editor.plugins.get( LinkEditing ) ).to.be.instanceOf( LinkEditing );
- } );
- it( 'should set proper schema rules', () => {
- expect( model.schema.checkAttribute( [ '$block', '$text' ], 'linkHref' ) ).to.be.true;
- expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'linkHref' ) ).to.be.true;
- expect( model.schema.checkAttribute( [ '$block' ], 'linkHref' ) ).to.be.false;
- } );
- it( 'should bind two-step caret movement to `linkHref` attribute', () => {
- // Let's check only the minimum to not duplicated `bindTwoStepCaretToAttribute()` tests.
- // Testing minimum is better then testing using spies that might give false positive results.
- // Put selection before the link element.
- setModelData( editor.model, '<paragraph>foo[]<$text linkHref="url">b</$text>ar</paragraph>' );
- // The selection's gravity is not overridden because selection land here not as a result of `keydown`.
- expect( editor.model.document.selection.isGravityOverridden ).to.false;
- // So let's simulate `keydown` event.
- editor.editing.view.document.fire( 'keydown', {
- keyCode: keyCodes.arrowright,
- preventDefault: () => {},
- domTarget: document.body
- } );
- expect( editor.model.document.selection.isGravityOverridden ).to.true;
- } );
- describe( 'command', () => {
- it( 'should register link command', () => {
- const command = editor.commands.get( 'link' );
- expect( command ).to.be.instanceOf( LinkCommand );
- } );
- it( 'should register unlink command', () => {
- const command = editor.commands.get( 'unlink' );
- expect( command ).to.be.instanceOf( UnlinkCommand );
- } );
- } );
- describe( 'data pipeline conversions', () => {
- it( 'should convert `<a href="url">` to `linkHref="url"` attribute', () => {
- editor.setData( '<p><a href="url">foo</a>bar</p>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
- expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
- } );
- it( 'should be integrated with autoparagraphing', () => {
- editor.setData( '<a href="url">foo</a>bar' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
- expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
- } );
- // https://github.com/ckeditor/ckeditor5/issues/500
- it( 'should not pick up `<a name="foo">`', () => {
- editor.setData( '<p><a name="foo">foo</a>bar</p>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '<paragraph>foobar</paragraph>' );
- } );
- // CKEditor 4 does. And CKEditor 5's balloon allows creating such links.
- it( 'should pick up `<a href="">`', () => {
- editor.setData( '<p><a href="">foo</a>bar</p>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '<paragraph><$text linkHref="">foo</$text>bar</paragraph>' );
- expect( editor.getData() ).to.equal( '<p><a href="">foo</a>bar</p>' );
- } );
- } );
- describe( 'editing pipeline conversion', () => {
- it( 'should convert attribute', () => {
- setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
- expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><a href="url">foo</a>bar</p>' );
- } );
- it( 'should convert to link element instance', () => {
- setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
- const element = editor.editing.view.document.getRoot().getChild( 0 ).getChild( 0 );
- expect( isLinkElement( element ) ).to.be.true;
- } );
- // https://github.com/ckeditor/ckeditor5-link/issues/121
- it( 'should should set priority for `linkHref` higher than all other attribute elements', () => {
- model.schema.extend( '$text', { allowAttributes: 'foo' } );
- editor.conversion.for( 'downcast' ).add( downcastAttributeToElement( { model: 'foo', view: 'f' } ) );
- setModelData( model,
- '<paragraph>' +
- '<$text linkHref="url">a</$text><$text foo="true" linkHref="url">b</$text><$text linkHref="url">c</$text>' +
- '</paragraph>' );
- expect( editor.getData() ).to.equal( '<p><a href="url">a<f>b</f>c</a></p>' );
- } );
- } );
- describe( 'highlight link boundaries', () => {
- it( 'should create marker in model when selection is inside a link', () => {
- expect( model.markers.has( 'linkBoundaries' ) ).to.be.false;
- setModelData( model,
- '<paragraph>foo <$text linkHref="url">b{}ar</$text> baz</paragraph>'
- );
- expect( model.markers.has( 'linkBoundaries' ) ).to.be.true;
- const marker = model.markers.get( 'linkBoundaries' );
- expect( marker.getStart().path ).to.deep.equal( [ 0, 4 ] );
- expect( marker.getEnd().path ).to.deep.equal( [ 0, 7 ] );
- } );
- it( 'should convert link boundaries marker to proper view', () => {
- setModelData( model,
- '<paragraph>foo <$text linkHref="url">b{}ar</$text> baz</paragraph>'
- );
- expect( getViewData( view ) ).to.equal(
- '<p>foo <span class="ck-link_selected"><a href="url">b{}ar</a></span> baz</p>'
- );
- } );
- it( 'should work whenever selection has linkHref attribute', () => {
- setModelData( model,
- '<paragraph>foo {}<$text linkHref="url">bar</$text> baz</paragraph>'
- );
- expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.false;
- model.change( writer => {
- writer.setSelectionAttribute( 'linkHref', 'url' );
- } );
- expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
- expect( model.markers.has( 'linkBoundaries' ) ).to.be.true;
- const marker = model.markers.get( 'linkBoundaries' );
- expect( marker.getStart().path ).to.deep.equal( [ 0, 4 ] );
- expect( marker.getEnd().path ).to.deep.equal( [ 0, 7 ] );
- } );
- it( 'should render highlight correctly after splitting the link', () => {
- setModelData( model,
- '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
- );
- editor.execute( 'enter' );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo <$text linkHref="url">li</$text></paragraph>' +
- '<paragraph><$text linkHref="url">[]nk</$text> baz</paragraph>'
- );
- expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
- expect( model.markers.has( 'linkBoundaries' ) ).to.be.true;
- const marker = model.markers.get( 'linkBoundaries' );
- expect( marker.getStart().path ).to.deep.equal( [ 1, 0 ] );
- expect( marker.getEnd().path ).to.deep.equal( [ 1, 2 ] );
- } );
- } );
- } );
|