linkengine.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import LinkEngine from '../src/linkengine';
  6. import LinkCommand from '../src/linkcommand';
  7. import LinkElement from '../src/linkelement';
  8. import UnlinkCommand from '../src/unlinkcommand';
  9. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  13. describe( 'LinkEngine', () => {
  14. let editor, doc;
  15. beforeEach( () => {
  16. return VirtualTestEditor.create( {
  17. plugins: [ Paragraph, LinkEngine ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. doc = editor.document;
  22. } );
  23. } );
  24. it( 'should be loaded', () => {
  25. expect( editor.plugins.get( LinkEngine ) ).to.be.instanceOf( LinkEngine );
  26. } );
  27. it( 'should set proper schema rules', () => {
  28. expect( doc.schema.check( { name: '$inline', attributes: [ 'linkHref' ], inside: '$block' } ) ).to.be.true;
  29. } );
  30. describe( 'command', () => {
  31. it( 'should register link command', () => {
  32. expect( editor.commands.has( 'link' ) ).to.be.true;
  33. const command = editor.commands.get( 'link' );
  34. expect( command ).to.be.instanceOf( LinkCommand );
  35. } );
  36. it( 'should register unlink command', () => {
  37. expect( editor.commands.has( 'unlink' ) ).to.be.true;
  38. const command = editor.commands.get( 'unlink' );
  39. expect( command ).to.be.instanceOf( UnlinkCommand );
  40. } );
  41. } );
  42. describe( 'data pipeline conversions', () => {
  43. it( 'should convert `<a href="url">` to `linkHref="url"` attribute', () => {
  44. editor.setData( '<p><a href="url">foo</a>bar</p>' );
  45. expect( getModelData( doc, { withoutSelection: true } ) )
  46. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  47. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  48. } );
  49. } );
  50. describe( 'editing pipeline conversion', () => {
  51. it( 'should convert attribute', () => {
  52. setModelData( doc, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  53. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  54. } );
  55. it( 'should convert to `LinkElement` instance', () => {
  56. setModelData( doc, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  57. expect( editor.editing.view.getRoot().getChild( 0 ).getChild( 0 ) ).to.be.instanceof( LinkElement );
  58. } );
  59. } );
  60. } );