linkengine.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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( { plugins: [ Paragraph, LinkEngine ] } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. doc = editor.document;
  20. } );
  21. } );
  22. it( 'should be loaded', () => {
  23. expect( editor.plugins.get( LinkEngine ) ).to.be.instanceOf( LinkEngine );
  24. } );
  25. it( 'should set proper schema rules', () => {
  26. expect( doc.schema.check( { name: '$inline', attributes: [ 'linkHref' ], inside: '$root' } ) ).to.be.false;
  27. expect( doc.schema.check( { name: '$inline', attributes: [ 'linkHref' ], inside: '$block' } ) ).to.be.true;
  28. } );
  29. describe( 'command', () => {
  30. it( 'should register link command', () => {
  31. const command = editor.commands.get( 'link' );
  32. expect( command ).to.be.instanceOf( LinkCommand );
  33. } );
  34. it( 'should register unlink command', () => {
  35. const command = editor.commands.get( 'unlink' );
  36. expect( command ).to.be.instanceOf( UnlinkCommand );
  37. } );
  38. } );
  39. describe( 'data pipeline conversions', () => {
  40. it( 'should convert `<a href="url">` to `linkHref="url"` attribute', () => {
  41. editor.setData( '<p><a href="url">foo</a>bar</p>' );
  42. expect( getModelData( doc, { withoutSelection: true } ) )
  43. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  44. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  45. } );
  46. it( 'should be integrated with autoparagraphing', () => {
  47. // Incorrect results because autoparagraphing works incorrectly (issue in paragraph).
  48. // https://github.com/ckeditor/ckeditor5-paragraph/issues/10
  49. editor.setData( '<a href="url">foo</a>bar' );
  50. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobar</paragraph>' );
  51. expect( editor.getData() ).to.equal( '<p>foobar</p>' );
  52. } );
  53. } );
  54. describe( 'editing pipeline conversion', () => {
  55. it( 'should convert attribute', () => {
  56. setModelData( doc, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  57. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  58. } );
  59. it( 'should convert to `LinkElement` instance', () => {
  60. setModelData( doc, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  61. expect( editor.editing.view.getRoot().getChild( 0 ).getChild( 0 ) ).to.be.instanceof( LinkElement );
  62. } );
  63. } );
  64. } );