8
0

linkengine.js 3.2 KB

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