linkengine.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  14. describe( 'LinkEngine', () => {
  15. let editor, doc;
  16. beforeEach( () => {
  17. return VirtualTestEditor
  18. .create( {
  19. plugins: [ Paragraph, LinkEngine ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. doc = editor.document;
  24. } );
  25. } );
  26. it( 'should be loaded', () => {
  27. expect( editor.plugins.get( LinkEngine ) ).to.be.instanceOf( LinkEngine );
  28. } );
  29. it( 'should set proper schema rules', () => {
  30. expect( doc.schema.check( { name: '$inline', attributes: 'linkHref', inside: '$root' } ) ).to.be.false;
  31. expect( doc.schema.check( { name: '$inline', attributes: 'linkHref', inside: '$block' } ) ).to.be.true;
  32. expect( doc.schema.check( { name: '$inline', attributes: 'linkHref', inside: '$clipboardHolder' } ) ).to.be.true;
  33. } );
  34. describe( 'command', () => {
  35. it( 'should register link command', () => {
  36. const command = editor.commands.get( 'link' );
  37. expect( command ).to.be.instanceOf( LinkCommand );
  38. } );
  39. it( 'should register unlink command', () => {
  40. const command = editor.commands.get( 'unlink' );
  41. expect( command ).to.be.instanceOf( UnlinkCommand );
  42. } );
  43. } );
  44. describe( 'data pipeline conversions', () => {
  45. it( 'should convert `<a href="url">` to `linkHref="url"` attribute', () => {
  46. editor.setData( '<p><a href="url">foo</a>bar</p>' );
  47. expect( getModelData( doc, { withoutSelection: true } ) )
  48. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  49. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  50. } );
  51. it( 'should be integrated with autoparagraphing', () => {
  52. // Incorrect results because autoparagraphing works incorrectly (issue in paragraph).
  53. // https://github.com/ckeditor/ckeditor5-paragraph/issues/10
  54. editor.setData( '<a href="url">foo</a>bar' );
  55. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobar</paragraph>' );
  56. expect( editor.getData() ).to.equal( '<p>foobar</p>' );
  57. } );
  58. // https://github.com/ckeditor/ckeditor5/issues/500
  59. it( 'should not pick up `<a name="foo">`', () => {
  60. editor.setData( '<p><a name="foo">foo</a>bar</p>' );
  61. expect( getModelData( doc, { withoutSelection: true } ) )
  62. .to.equal( '<paragraph>foobar</paragraph>' );
  63. } );
  64. // CKEditor 4 does. And CKEditor 5's balloon allows creating such links.
  65. it( 'should pick up `<a href="">`', () => {
  66. editor.setData( '<p><a href="">foo</a>bar</p>' );
  67. expect( getModelData( doc, { withoutSelection: true } ) )
  68. .to.equal( '<paragraph><$text linkHref="">foo</$text>bar</paragraph>' );
  69. expect( editor.getData() ).to.equal( '<p><a href="">foo</a>bar</p>' );
  70. } );
  71. } );
  72. describe( 'editing pipeline conversion', () => {
  73. it( 'should convert attribute', () => {
  74. setModelData( doc, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  75. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  76. } );
  77. it( 'should convert to `LinkElement` instance', () => {
  78. setModelData( doc, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  79. expect( editor.editing.view.getRoot().getChild( 0 ).getChild( 0 ) ).to.be.instanceof( LinkElement );
  80. } );
  81. // https://github.com/ckeditor/ckeditor5-link/issues/121
  82. it( 'should should set priority for `linkHref` higher than all other attribute elements', () => {
  83. editor.document.schema.allow( { name: '$inline', attributes: [ 'foo' ], inside: '$block' } );
  84. buildModelConverter().for( editor.data.modelToView )
  85. .fromAttribute( 'foo' )
  86. .toElement( 'f' );
  87. setModelData( doc,
  88. '<paragraph>' +
  89. '<$text linkHref="url">a</$text><$text foo="true" linkHref="url">b</$text><$text linkHref="url">c</$text>' +
  90. '</paragraph>' );
  91. expect( editor.getData() ).to.equal( '<p><a href="url">a<f>b</f>c</a></p>' );
  92. } );
  93. } );
  94. } );