linkengine.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import LinkEngine from 'ckeditor5/link/linkengine.js';
  6. import LinkCommand from 'ckeditor5/link/linkcommand.js';
  7. import LinkElement from 'ckeditor5/link/linkelement.js';
  8. import UnlinkCommand from 'ckeditor5/link/unlinkcommand.js';
  9. import VirtualTestEditor from 'tests/core/_utils/virtualtesteditor.js';
  10. import { getData as getModelData, setData as setModelData } from 'ckeditor5/engine/dev-utils/model.js';
  11. import { getData as getViewData } from 'ckeditor5/engine/dev-utils/view.js';
  12. describe( 'LinkEngine', () => {
  13. let editor, doc;
  14. beforeEach( () => {
  15. return VirtualTestEditor.create( {
  16. plugins: [ LinkEngine ]
  17. } )
  18. .then( newEditor => {
  19. editor = newEditor;
  20. doc = editor.document;
  21. doc.schema.allow( { name: '$text', inside: '$root' } );
  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' ] } ) ).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( '<a href="url">foo</a>bar' );
  45. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<$text linkHref="url">foo</$text>bar' );
  46. expect( editor.getData() ).to.equal( '<a href="url">foo</a>bar' );
  47. } );
  48. } );
  49. describe( 'editing pipeline conversion', () => {
  50. it( 'should convert attribute', () => {
  51. setModelData( doc, '<$text linkHref="url">foo</$text>bar' );
  52. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<a href="url">foo</a>bar' );
  53. } );
  54. it( 'should convert to `LinkElement` instance', () => {
  55. setModelData( doc, '<$text linkHref="url">foo</$text>bar' );
  56. expect( editor.editing.view.getRoot().getChild( 0 ) ).to.be.instanceof( LinkElement );
  57. } );
  58. } );
  59. } );