linkcommand.js 8.5 KB

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