linkcommand.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /**
  2. * @license Copyright (c) 2003-2020, 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: 'linkIsFoo',
  203. label: 'Foo',
  204. attributes: {
  205. class: 'Foo'
  206. }
  207. } ) );
  208. command.manualDecorators.add( new ManualDecorator( {
  209. id: 'linkIsBar',
  210. label: 'Bar',
  211. attributes: {
  212. target: '_blank'
  213. }
  214. } ) );
  215. command.manualDecorators.add( new ManualDecorator( {
  216. id: 'linkIsSth',
  217. label: 'Sth',
  218. attributes: {
  219. class: 'sth'
  220. },
  221. defaultValue: true
  222. } ) );
  223. model.schema.extend( '$text', {
  224. allowIn: '$root',
  225. allowAttributes: [ 'linkHref', 'linkIsFoo', 'linkIsBar', 'linkIsSth' ]
  226. } );
  227. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  228. } );
  229. } );
  230. afterEach( () => {
  231. return editor.destroy();
  232. } );
  233. describe( 'collapsed selection', () => {
  234. it( 'should insert additional attributes to link when it is created', () => {
  235. setData( model, 'foo[]bar' );
  236. command.execute( 'url', { linkIsFoo: true, linkIsBar: true, linkIsSth: true } );
  237. expect( getData( model ) ).to
  238. .equal( 'foo[<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="true">url</$text>]bar' );
  239. } );
  240. it( 'should add additional attributes to link when link is modified', () => {
  241. setData( model, 'f<$text linkHref="url">o[]oba</$text>r' );
  242. command.execute( 'url', { linkIsFoo: true, linkIsBar: true, linkIsSth: true } );
  243. expect( getData( model ) ).to
  244. .equal( 'f[<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="true">ooba</$text>]r' );
  245. } );
  246. it( 'should remove additional attributes to link if those are falsy', () => {
  247. setData( model, 'foo<$text linkHref="url" linkIsBar="true" linkIsFoo="true">u[]rl</$text>bar' );
  248. command.execute( 'url', { linkIsFoo: false, linkIsBar: false } );
  249. expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
  250. } );
  251. } );
  252. describe( 'range selection', () => {
  253. it( 'should insert additional attributes to link when it is created', () => {
  254. setData( model, 'f[ooba]r' );
  255. command.execute( 'url', { linkIsFoo: true, linkIsBar: true, linkIsSth: true } );
  256. expect( getData( model ) ).to
  257. .equal( 'f[<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="true">ooba</$text>]r' );
  258. } );
  259. it( 'should add additional attributes to link when link is modified', () => {
  260. setData( model, 'f[<$text linkHref="foo">ooba</$text>]r' );
  261. command.execute( 'url', { linkIsFoo: true, linkIsBar: true, linkIsSth: true } );
  262. expect( getData( model ) ).to
  263. .equal( 'f[<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="true">ooba</$text>]r' );
  264. } );
  265. it( 'should remove additional attributes to link if those are falsy', () => {
  266. setData( model, 'foo[<$text linkHref="url" linkIsBar="true" linkIsFoo="true">url</$text>]bar' );
  267. command.execute( 'url', { linkIsFoo: false, linkIsBar: false } );
  268. expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
  269. } );
  270. } );
  271. describe( 'restoreManualDecoratorStates()', () => {
  272. it( 'synchronize values with current model state', () => {
  273. setData( model, 'foo<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="true">u[]rl</$text>bar' );
  274. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  275. linkIsFoo: true,
  276. linkIsBar: true,
  277. linkIsSth: true
  278. } );
  279. command.manualDecorators.first.value = false;
  280. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  281. linkIsFoo: false,
  282. linkIsBar: true,
  283. linkIsSth: true
  284. } );
  285. command.restoreManualDecoratorStates();
  286. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  287. linkIsFoo: true,
  288. linkIsBar: true,
  289. linkIsSth: true
  290. } );
  291. } );
  292. it( 'synchronize values with current model state when the decorator that is "on" default is "off"', () => {
  293. setData( model, 'foo<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="false">u[]rl</$text>bar' );
  294. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  295. linkIsFoo: true,
  296. linkIsBar: true,
  297. linkIsSth: false
  298. } );
  299. command.manualDecorators.last.value = true;
  300. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  301. linkIsFoo: true,
  302. linkIsBar: true,
  303. linkIsSth: true
  304. } );
  305. command.restoreManualDecoratorStates();
  306. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  307. linkIsFoo: true,
  308. linkIsBar: true,
  309. linkIsSth: false
  310. } );
  311. } );
  312. } );
  313. describe( '_getDecoratorStateFromModel', () => {
  314. it( 'obtain current values from the model', () => {
  315. setData( model, 'foo[<$text linkHref="url" linkIsBar="true">url</$text>]bar' );
  316. expect( command._getDecoratorStateFromModel( 'linkIsFoo' ) ).to.be.undefined;
  317. expect( command._getDecoratorStateFromModel( 'linkIsBar' ) ).to.be.true;
  318. } );
  319. } );
  320. } );
  321. } );
  322. function decoratorStates( manualDecorators ) {
  323. return Array.from( manualDecorators ).reduce( ( accumulator, currentValue ) => {
  324. accumulator[ currentValue.id ] = currentValue.value;
  325. return accumulator;
  326. }, {} );
  327. }