linkcommand.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import LinkCommand from '../src/linkcommand';
  7. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. describe( 'LinkCommand', () => {
  9. let editor, model, command;
  10. beforeEach( () => {
  11. return ModelTestEditor.create()
  12. .then( newEditor => {
  13. editor = newEditor;
  14. model = editor.model;
  15. command = new LinkCommand( editor );
  16. model.schema.extend( '$text', {
  17. allowIn: '$root',
  18. allowAttributes: 'linkHref'
  19. } );
  20. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  21. } );
  22. } );
  23. afterEach( () => {
  24. return editor.destroy();
  25. } );
  26. describe( 'isEnabled', () => {
  27. // This test doesn't tests every possible case.
  28. // refresh() uses `isAttributeAllowedInSelection` helper which is fully tested in his own test.
  29. beforeEach( () => {
  30. model.schema.register( 'x', { inheritAllFrom: '$block' } );
  31. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  32. if ( ctx.endsWith( 'x $text' ) && attributeName == 'linkHref' ) {
  33. return false;
  34. }
  35. } );
  36. } );
  37. describe( 'when selection is collapsed', () => {
  38. it( 'should be true if characters with the attribute can be placed at caret position', () => {
  39. setData( model, '<p>f[]oo</p>' );
  40. expect( command.isEnabled ).to.be.true;
  41. } );
  42. it( 'should be false if characters with the attribute cannot be placed at caret position', () => {
  43. setData( model, '<x>fo[]o</x>' );
  44. expect( command.isEnabled ).to.be.false;
  45. } );
  46. } );
  47. describe( 'when selection is not collapsed', () => {
  48. it( 'should be true if there is at least one node in selection that can have the attribute', () => {
  49. setData( model, '<p>[foo]</p>' );
  50. expect( command.isEnabled ).to.be.true;
  51. } );
  52. it( 'should be false if there are no nodes in selection that can have the attribute', () => {
  53. setData( model, '<x>[foo]</x>' );
  54. expect( command.isEnabled ).to.be.false;
  55. } );
  56. } );
  57. } );
  58. describe( 'value', () => {
  59. describe( 'collapsed selection', () => {
  60. it( 'should be equal attribute value when selection is placed inside element with `linkHref` attribute', () => {
  61. setData( model, '<$text linkHref="url">foo[]bar</$text>' );
  62. expect( command.value ).to.equal( 'url' );
  63. } );
  64. it( 'should be undefined when selection is placed inside element without `linkHref` attribute', () => {
  65. setData( model, '<$text bold="true">foo[]bar</$text>' );
  66. expect( command.value ).to.be.undefined;
  67. } );
  68. } );
  69. describe( 'non-collapsed selection', () => {
  70. it( 'should be equal attribute value when selection contains only elements with `linkHref` attribute', () => {
  71. setData( model, 'fo[<$text linkHref="url">ob</$text>]ar' );
  72. expect( command.value ).to.equal( 'url' );
  73. } );
  74. it( 'should be undefined when selection contains not only elements with `linkHref` attribute', () => {
  75. setData( model, 'f[o<$text linkHref="url">ob</$text>]ar' );
  76. expect( command.value ).to.be.undefined;
  77. } );
  78. } );
  79. } );
  80. describe( 'execute()', () => {
  81. describe( 'non-collapsed selection', () => {
  82. it( 'should set `linkHref` attribute to selected text', () => {
  83. setData( model, 'f[ooba]r' );
  84. expect( command.value ).to.be.undefined;
  85. command.execute( 'url' );
  86. expect( getData( model ) ).to.equal( 'f[<$text linkHref="url">ooba</$text>]r' );
  87. expect( command.value ).to.equal( 'url' );
  88. } );
  89. it( 'should set `linkHref` attribute to selected text when text already has attributes', () => {
  90. setData( model, 'f[o<$text bold="true">oba]r</$text>' );
  91. expect( command.value ).to.be.undefined;
  92. command.execute( 'url' );
  93. expect( command.value ).to.equal( 'url' );
  94. expect( getData( model ) ).to.equal(
  95. 'f[<$text linkHref="url">o</$text>' +
  96. '<$text bold="true" linkHref="url">oba</$text>]' +
  97. '<$text bold="true">r</$text>'
  98. );
  99. } );
  100. it( 'should overwrite existing `linkHref` attribute when selected text wraps text with `linkHref` attribute', () => {
  101. setData( model, 'f[o<$text linkHref="other url">o</$text>ba]r' );
  102. expect( command.value ).to.be.undefined;
  103. command.execute( 'url' );
  104. expect( getData( model ) ).to.equal( 'f[<$text linkHref="url">ooba</$text>]r' );
  105. expect( command.value ).to.equal( 'url' );
  106. } );
  107. it( 'should split text and overwrite attribute value when selection is inside text with `linkHref` attribute', () => {
  108. setData( model, 'f<$text linkHref="other url">o[ob]a</$text>r' );
  109. expect( command.value ).to.equal( 'other url' );
  110. command.execute( 'url' );
  111. expect( getData( model ) ).to.equal(
  112. 'f' +
  113. '<$text linkHref="other url">o</$text>' +
  114. '[<$text linkHref="url">ob</$text>]' +
  115. '<$text linkHref="other url">a</$text>' +
  116. 'r'
  117. );
  118. expect( command.value ).to.equal( 'url' );
  119. } );
  120. it( 'should overwrite `linkHref` attribute of selected text only, ' +
  121. 'when selection start inside text with `linkHref` attribute',
  122. () => {
  123. setData( model, 'f<$text linkHref="other url">o[o</$text>ba]r' );
  124. expect( command.value ).to.equal( 'other url' );
  125. command.execute( 'url' );
  126. expect( getData( model ) ).to.equal( 'f<$text linkHref="other url">o</$text>[<$text linkHref="url">oba</$text>]r' );
  127. expect( command.value ).to.equal( 'url' );
  128. } );
  129. it( 'should overwrite `linkHref` attribute of selected text only, when selection end inside text with `linkHref` ' +
  130. 'attribute', () => {
  131. setData( model, 'f[o<$text linkHref="other url">ob]a</$text>r' );
  132. expect( command.value ).to.be.undefined;
  133. command.execute( 'url' );
  134. expect( getData( model ) ).to.equal( 'f[<$text linkHref="url">oob</$text>]<$text linkHref="other url">a</$text>r' );
  135. expect( command.value ).to.equal( 'url' );
  136. } );
  137. it( 'should set `linkHref` attribute to selected text when text is split by $block element', () => {
  138. setData( model, '<p>f[oo</p><p>ba]r</p>' );
  139. expect( command.value ).to.be.undefined;
  140. command.execute( 'url' );
  141. expect( getData( model ) )
  142. .to.equal( '<p>f[<$text linkHref="url">oo</$text></p><p><$text linkHref="url">ba</$text>]r</p>' );
  143. expect( command.value ).to.equal( 'url' );
  144. } );
  145. it( 'should set `linkHref` attribute only to allowed elements and omit disallowed', () => {
  146. model.schema.register( 'img', { allowWhere: '$text' } );
  147. setData( model, '<p>f[oo<img></img>ba]r</p>' );
  148. expect( command.value ).to.be.undefined;
  149. command.execute( 'url' );
  150. expect( getData( model ) )
  151. .to.equal( '<p>f[<$text linkHref="url">oo</$text><img></img><$text linkHref="url">ba</$text>]r</p>' );
  152. expect( command.value ).to.equal( 'url' );
  153. } );
  154. } );
  155. describe( 'collapsed selection', () => {
  156. it( 'should insert text with `linkHref` attribute, text data equal to href and select new link', () => {
  157. setData( model, 'foo[]bar' );
  158. command.execute( 'url' );
  159. expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
  160. } );
  161. it( 'should insert text with `linkHref` attribute, and selection attributes', () => {
  162. setData( model, '<$text bold="true">foo[]bar</$text>', {
  163. selectionAttributes: { bold: true }
  164. } );
  165. command.execute( 'url' );
  166. expect( getData( model ) ).to.equal(
  167. '<$text bold="true">foo</$text>[<$text bold="true" linkHref="url">url</$text>]<$text bold="true">bar</$text>'
  168. );
  169. } );
  170. it( 'should update `linkHref` attribute and select whole link when selection is inside text with `linkHref` attribute', () => {
  171. setData( model, '<$text linkHref="other url">foo[]bar</$text>' );
  172. command.execute( 'url' );
  173. expect( getData( model ) ).to.equal( '[<$text linkHref="url">foobar</$text>]' );
  174. } );
  175. it( 'should not insert text with `linkHref` attribute when is not allowed in parent', () => {
  176. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  177. if ( ctx.endsWith( 'p $text' ) && attributeName == 'linkHref' ) {
  178. return false;
  179. }
  180. } );
  181. setData( model, '<p>foo[]bar</p>' );
  182. command.execute( 'url' );
  183. expect( getData( model ) ).to.equal( '<p>foo[]bar</p>' );
  184. } );
  185. it( 'should not insert text node if link is empty', () => {
  186. setData( model, '<p>foo[]bar</p>' );
  187. command.execute( '' );
  188. expect( getData( model ) ).to.equal( '<p>foo[]bar</p>' );
  189. } );
  190. } );
  191. } );
  192. } );