linkcommand.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 ManualDecorator from '../src/utils/manualdecorator';
  8. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. describe( 'LinkCommand', () => {
  10. let editor, model, command;
  11. beforeEach( () => {
  12. return ModelTestEditor.create()
  13. .then( newEditor => {
  14. editor = newEditor;
  15. model = editor.model;
  16. command = new LinkCommand( editor );
  17. model.schema.extend( '$text', {
  18. allowIn: '$root',
  19. allowAttributes: [ 'linkHref', 'bold' ]
  20. } );
  21. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  22. } );
  23. } );
  24. afterEach( () => {
  25. return editor.destroy();
  26. } );
  27. describe( 'isEnabled', () => {
  28. // This test doesn't tests every possible case.
  29. // refresh() uses `isAttributeAllowedInSelection` helper which is fully tested in his own test.
  30. beforeEach( () => {
  31. model.schema.register( 'x', { inheritAllFrom: '$block' } );
  32. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  33. if ( ctx.endsWith( 'x $text' ) && attributeName == 'linkHref' ) {
  34. return false;
  35. }
  36. } );
  37. } );
  38. describe( 'when selection is collapsed', () => {
  39. it( 'should be true if characters with the attribute can be placed at caret position', () => {
  40. setData( model, '<p>f[]oo</p>' );
  41. expect( command.isEnabled ).to.be.true;
  42. } );
  43. it( 'should be false if characters with the attribute cannot be placed at caret position', () => {
  44. setData( model, '<x>fo[]o</x>' );
  45. expect( command.isEnabled ).to.be.false;
  46. } );
  47. } );
  48. describe( 'when selection is not collapsed', () => {
  49. it( 'should be true if there is at least one node in selection that can have the attribute', () => {
  50. setData( model, '<p>[foo]</p>' );
  51. expect( command.isEnabled ).to.be.true;
  52. } );
  53. it( 'should be false if there are no nodes in selection that can have the attribute', () => {
  54. setData( model, '<x>[foo]</x>' );
  55. expect( command.isEnabled ).to.be.false;
  56. } );
  57. } );
  58. } );
  59. describe( 'value', () => {
  60. describe( 'collapsed selection', () => {
  61. it( 'should be equal attribute value when selection is placed inside element with `linkHref` attribute', () => {
  62. setData( model, '<$text linkHref="url">foo[]bar</$text>' );
  63. expect( command.value ).to.equal( 'url' );
  64. } );
  65. it( 'should be undefined when selection is placed inside element without `linkHref` attribute', () => {
  66. setData( model, '<$text bold="true">foo[]bar</$text>' );
  67. expect( command.value ).to.be.undefined;
  68. } );
  69. } );
  70. describe( 'non-collapsed selection', () => {
  71. it( 'should be equal attribute value when selection contains only elements with `linkHref` attribute', () => {
  72. setData( model, 'fo[<$text linkHref="url">ob</$text>]ar' );
  73. expect( command.value ).to.equal( 'url' );
  74. } );
  75. it( 'should be undefined when selection contains not only elements with `linkHref` attribute', () => {
  76. setData( model, 'f[o<$text linkHref="url">ob</$text>]ar' );
  77. expect( command.value ).to.be.undefined;
  78. } );
  79. } );
  80. } );
  81. describe( 'execute()', () => {
  82. describe( 'non-collapsed selection', () => {
  83. it( 'should set `linkHref` attribute to selected text', () => {
  84. setData( model, 'f[ooba]r' );
  85. expect( command.value ).to.be.undefined;
  86. command.execute( 'url' );
  87. expect( getData( model ) ).to.equal( 'f[<$text linkHref="url">ooba</$text>]r' );
  88. expect( command.value ).to.equal( 'url' );
  89. } );
  90. it( 'should set `linkHref` attribute to selected text when text already has attributes', () => {
  91. setData( model, 'f[o<$text bold="true">oba]r</$text>' );
  92. expect( command.value ).to.be.undefined;
  93. command.execute( 'url' );
  94. expect( command.value ).to.equal( 'url' );
  95. expect( getData( model ) ).to.equal(
  96. 'f[<$text linkHref="url">o</$text>' +
  97. '<$text bold="true" linkHref="url">oba</$text>]' +
  98. '<$text bold="true">r</$text>'
  99. );
  100. } );
  101. it( 'should overwrite existing `linkHref` attribute when selected text wraps text with `linkHref` attribute', () => {
  102. setData( model, 'f[o<$text linkHref="other url">o</$text>ba]r' );
  103. expect( command.value ).to.be.undefined;
  104. command.execute( 'url' );
  105. expect( getData( model ) ).to.equal( 'f[<$text linkHref="url">ooba</$text>]r' );
  106. expect( command.value ).to.equal( 'url' );
  107. } );
  108. it( 'should split text and overwrite attribute value when selection is inside text with `linkHref` attribute', () => {
  109. setData( model, 'f<$text linkHref="other url">o[ob]a</$text>r' );
  110. expect( command.value ).to.equal( 'other url' );
  111. command.execute( 'url' );
  112. expect( getData( model ) ).to.equal(
  113. 'f' +
  114. '<$text linkHref="other url">o</$text>' +
  115. '[<$text linkHref="url">ob</$text>]' +
  116. '<$text linkHref="other url">a</$text>' +
  117. 'r'
  118. );
  119. expect( command.value ).to.equal( 'url' );
  120. } );
  121. it( 'should overwrite `linkHref` attribute of selected text only, ' +
  122. 'when selection start inside text with `linkHref` attribute',
  123. () => {
  124. setData( model, 'f<$text linkHref="other url">o[o</$text>ba]r' );
  125. expect( command.value ).to.equal( 'other url' );
  126. command.execute( 'url' );
  127. expect( getData( model ) ).to.equal( 'f<$text linkHref="other url">o</$text>[<$text linkHref="url">oba</$text>]r' );
  128. expect( command.value ).to.equal( 'url' );
  129. } );
  130. it( 'should overwrite `linkHref` attribute of selected text only, when selection end inside text with `linkHref` ' +
  131. 'attribute', () => {
  132. setData( model, 'f[o<$text linkHref="other url">ob]a</$text>r' );
  133. expect( command.value ).to.be.undefined;
  134. command.execute( 'url' );
  135. expect( getData( model ) ).to.equal( 'f[<$text linkHref="url">oob</$text>]<$text linkHref="other url">a</$text>r' );
  136. expect( command.value ).to.equal( 'url' );
  137. } );
  138. it( 'should set `linkHref` attribute to selected text when text is split by $block element', () => {
  139. setData( model, '<p>f[oo</p><p>ba]r</p>' );
  140. expect( command.value ).to.be.undefined;
  141. command.execute( 'url' );
  142. expect( getData( model ) )
  143. .to.equal( '<p>f[<$text linkHref="url">oo</$text></p><p><$text linkHref="url">ba</$text>]r</p>' );
  144. expect( command.value ).to.equal( 'url' );
  145. } );
  146. it( 'should set `linkHref` attribute only to allowed elements and omit disallowed', () => {
  147. model.schema.register( 'img', { allowWhere: '$text' } );
  148. setData( model, '<p>f[oo<img></img>ba]r</p>' );
  149. expect( command.value ).to.be.undefined;
  150. command.execute( 'url' );
  151. expect( getData( model ) )
  152. .to.equal( '<p>f[<$text linkHref="url">oo</$text><img></img><$text linkHref="url">ba</$text>]r</p>' );
  153. expect( command.value ).to.equal( 'url' );
  154. } );
  155. } );
  156. describe( 'collapsed selection', () => {
  157. it( 'should insert text with `linkHref` attribute, text data equal to href and select new link', () => {
  158. setData( model, 'foo[]bar' );
  159. command.execute( 'url' );
  160. expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
  161. } );
  162. it( 'should insert text with `linkHref` attribute, and selection attributes', () => {
  163. setData( model, '<$text bold="true">foo[]bar</$text>', {
  164. selectionAttributes: { bold: true }
  165. } );
  166. command.execute( 'url' );
  167. expect( getData( model ) ).to.equal(
  168. '<$text bold="true">foo</$text>[<$text bold="true" linkHref="url">url</$text>]<$text bold="true">bar</$text>'
  169. );
  170. } );
  171. it( 'should update `linkHref` attribute and select whole link when selection is inside text with `linkHref` attribute', () => {
  172. setData( model, '<$text linkHref="other url">foo[]bar</$text>' );
  173. command.execute( 'url' );
  174. expect( getData( model ) ).to.equal( '[<$text linkHref="url">foobar</$text>]' );
  175. } );
  176. it( 'should not insert text with `linkHref` attribute when is not allowed in parent', () => {
  177. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  178. if ( ctx.endsWith( 'p $text' ) && attributeName == 'linkHref' ) {
  179. return false;
  180. }
  181. } );
  182. setData( model, '<p>foo[]bar</p>' );
  183. command.execute( 'url' );
  184. expect( getData( model ) ).to.equal( '<p>foo[]bar</p>' );
  185. } );
  186. it( 'should not insert text node if link is empty', () => {
  187. setData( model, '<p>foo[]bar</p>' );
  188. command.execute( '' );
  189. expect( getData( model ) ).to.equal( '<p>foo[]bar</p>' );
  190. } );
  191. } );
  192. } );
  193. describe( 'manual decorators', () => {
  194. beforeEach( () => {
  195. editor.destroy();
  196. return ModelTestEditor.create()
  197. .then( newEditor => {
  198. editor = newEditor;
  199. model = editor.model;
  200. command = new LinkCommand( editor );
  201. command.manualDecorators.add( new ManualDecorator( {
  202. id: 'linkManualDecorator0',
  203. label: 'Foo',
  204. attributes: {
  205. class: 'Foo'
  206. }
  207. } ) );
  208. command.manualDecorators.add( new ManualDecorator( {
  209. id: 'linkManualDecorator1',
  210. label: 'Bar',
  211. attributes: {
  212. target: '_blank'
  213. }
  214. } ) );
  215. model.schema.extend( '$text', {
  216. allowIn: '$root',
  217. allowAttributes: [ 'linkHref', 'linkManualDecorator0', 'linkManualDecorator1' ]
  218. } );
  219. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  220. } );
  221. } );
  222. afterEach( () => {
  223. return editor.destroy();
  224. } );
  225. describe( 'collapsed selection', () => {
  226. it( 'should insert additional attributes to link when it is created', () => {
  227. setData( model, 'foo[]bar' );
  228. command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } );
  229. expect( getData( model ) ).to
  230. .equal( 'foo[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">url</$text>]bar' );
  231. } );
  232. it( 'should add additional attributes to link when link is modified', () => {
  233. setData( model, 'f<$text linkHref="url">o[]oba</$text>r' );
  234. command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } );
  235. expect( getData( model ) ).to
  236. .equal( 'f[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">ooba</$text>]r' );
  237. } );
  238. it( 'should remove additional attributes to link if those are falsy', () => {
  239. setData( model, 'foo<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true" >u[]rl</$text>bar' );
  240. command.execute( 'url', { linkManualDecorator0: false, linkManualDecorator1: false } );
  241. expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
  242. } );
  243. } );
  244. describe( 'range selection', () => {
  245. it( 'should insert additional attributes to link when it is created', () => {
  246. setData( model, 'f[ooba]r' );
  247. command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } );
  248. expect( getData( model ) ).to
  249. .equal( 'f[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">ooba</$text>]r' );
  250. } );
  251. it( 'should add additional attributes to link when link is modified', () => {
  252. setData( model, 'f[<$text linkHref="foo">ooba</$text>]r' );
  253. command.execute( 'url', { linkManualDecorator0: true, linkManualDecorator1: true } );
  254. expect( getData( model ) ).to
  255. .equal( 'f[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true">ooba</$text>]r' );
  256. } );
  257. it( 'should remove additional attributes to link if those are falsy', () => {
  258. setData( model, 'foo[<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true" >url</$text>]bar' );
  259. command.execute( 'url', { linkManualDecorator0: false, linkManualDecorator1: false } );
  260. expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
  261. } );
  262. } );
  263. } );
  264. } );