linkengine.js 3.1 KB

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