linkcommand.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 to allowed elements', () => {
  147. model.schema.register( 'img', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
  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 linkHref="url"></img><$text linkHref="url">ba</$text>]r</p>' );
  153. expect( command.value ).to.equal( 'url' );
  154. } );
  155. it( 'should set `linkHref` attribute to nested allowed elements', () => {
  156. model.schema.register( 'img', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
  157. model.schema.register( 'blockQuote', { allowWhere: '$block', allowContentOf: '$root' } );
  158. setData( model, '<p>foo</p>[<blockQuote><img></img></blockQuote>]<p>bar</p>' );
  159. command.execute( 'url' );
  160. expect( getData( model ) )
  161. .to.equal( '<p>foo</p>[<blockQuote><img linkHref="url"></img></blockQuote>]<p>bar</p>' );
  162. } );
  163. it( 'should set `linkHref` attribute to allowed elements on multi-selection', () => {
  164. model.schema.register( 'img', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
  165. setData( model, '<p>[<img></img>][<img></img>]</p>' );
  166. command.execute( 'url' );
  167. expect( getData( model ) )
  168. .to.equal( '<p>[<img linkHref="url"></img>][<img linkHref="url"></img>]</p>' );
  169. } );
  170. it( 'should set `linkHref` attribute to allowed elements and omit disallowed', () => {
  171. model.schema.register( 'img', { isBlock: true, allowWhere: '$text' } );
  172. model.schema.register( 'caption', { allowIn: 'img' } );
  173. model.schema.extend( '$text', { allowIn: 'caption' } );
  174. setData( model, '<p>f[oo<img><caption>xxx</caption></img>ba]r</p>' );
  175. command.execute( 'url' );
  176. expect( getData( model ) ).to.equal(
  177. '<p>' +
  178. 'f[<$text linkHref="url">oo</$text>' +
  179. '<img><caption><$text linkHref="url">xxx</$text></caption></img>' +
  180. '<$text linkHref="url">ba</$text>]r' +
  181. '</p>'
  182. );
  183. } );
  184. it( 'should set `linkHref` attribute to allowed elements and omit their children even if they accept the attribute', () => {
  185. model.schema.register( 'img', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
  186. model.schema.register( 'caption', { allowIn: 'img' } );
  187. model.schema.extend( '$text', { allowIn: 'caption' } );
  188. setData( model, '<p>f[oo<img><caption>xxx</caption></img>ba]r</p>' );
  189. command.execute( 'url' );
  190. expect( getData( model ) ).to.equal(
  191. '<p>' +
  192. 'f[<$text linkHref="url">oo</$text>' +
  193. '<img linkHref="url"><caption>xxx</caption></img>' +
  194. '<$text linkHref="url">ba</$text>]r' +
  195. '</p>'
  196. );
  197. } );
  198. } );
  199. describe( 'collapsed selection', () => {
  200. it( 'should insert text with `linkHref` attribute, text data equal to href and select new link', () => {
  201. setData( model, 'foo[]bar' );
  202. command.execute( 'url' );
  203. expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
  204. } );
  205. it( 'should insert text with `linkHref` attribute, and selection attributes', () => {
  206. setData( model, '<$text bold="true">foo[]bar</$text>', {
  207. selectionAttributes: { bold: true }
  208. } );
  209. command.execute( 'url' );
  210. expect( getData( model ) ).to.equal(
  211. '<$text bold="true">foo</$text>[<$text bold="true" linkHref="url">url</$text>]<$text bold="true">bar</$text>'
  212. );
  213. } );
  214. it( 'should update `linkHref` attribute and select whole link when selection is inside text with `linkHref` attribute', () => {
  215. setData( model, '<$text linkHref="other url">foo[]bar</$text>' );
  216. command.execute( 'url' );
  217. expect( getData( model ) ).to.equal( '[<$text linkHref="url">foobar</$text>]' );
  218. } );
  219. it( 'should not insert text with `linkHref` attribute when is not allowed in parent', () => {
  220. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  221. if ( ctx.endsWith( 'p $text' ) && attributeName == 'linkHref' ) {
  222. return false;
  223. }
  224. } );
  225. setData( model, '<p>foo[]bar</p>' );
  226. command.execute( 'url' );
  227. expect( getData( model ) ).to.equal( '<p>foo[]bar</p>' );
  228. } );
  229. it( 'should not insert text node if link is empty', () => {
  230. setData( model, '<p>foo[]bar</p>' );
  231. command.execute( '' );
  232. expect( getData( model ) ).to.equal( '<p>foo[]bar</p>' );
  233. } );
  234. } );
  235. } );
  236. describe( 'manual decorators', () => {
  237. beforeEach( () => {
  238. editor.destroy();
  239. return ModelTestEditor.create()
  240. .then( newEditor => {
  241. editor = newEditor;
  242. model = editor.model;
  243. command = new LinkCommand( editor );
  244. command.manualDecorators.add( new ManualDecorator( {
  245. id: 'linkIsFoo',
  246. label: 'Foo',
  247. attributes: {
  248. class: 'Foo'
  249. }
  250. } ) );
  251. command.manualDecorators.add( new ManualDecorator( {
  252. id: 'linkIsBar',
  253. label: 'Bar',
  254. attributes: {
  255. target: '_blank'
  256. }
  257. } ) );
  258. command.manualDecorators.add( new ManualDecorator( {
  259. id: 'linkIsSth',
  260. label: 'Sth',
  261. attributes: {
  262. class: 'sth'
  263. },
  264. defaultValue: true
  265. } ) );
  266. model.schema.extend( '$text', {
  267. allowIn: '$root',
  268. allowAttributes: [ 'linkHref', 'linkIsFoo', 'linkIsBar', 'linkIsSth' ]
  269. } );
  270. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  271. } );
  272. } );
  273. afterEach( () => {
  274. return editor.destroy();
  275. } );
  276. describe( 'collapsed selection', () => {
  277. it( 'should insert additional attributes to link when it is created', () => {
  278. setData( model, 'foo[]bar' );
  279. command.execute( 'url', { linkIsFoo: true, linkIsBar: true, linkIsSth: true } );
  280. expect( getData( model ) ).to
  281. .equal( 'foo[<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="true">url</$text>]bar' );
  282. } );
  283. it( 'should add additional attributes to link when link is modified', () => {
  284. setData( model, 'f<$text linkHref="url">o[]oba</$text>r' );
  285. command.execute( 'url', { linkIsFoo: true, linkIsBar: true, linkIsSth: true } );
  286. expect( getData( model ) ).to
  287. .equal( 'f[<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="true">ooba</$text>]r' );
  288. } );
  289. it( 'should remove additional attributes to link if those are falsy', () => {
  290. setData( model, 'foo<$text linkHref="url" linkIsBar="true" linkIsFoo="true">u[]rl</$text>bar' );
  291. command.execute( 'url', { linkIsFoo: false, linkIsBar: false } );
  292. expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
  293. } );
  294. } );
  295. describe( 'range selection', () => {
  296. it( 'should insert additional attributes to link when it is created', () => {
  297. setData( model, 'f[ooba]r' );
  298. command.execute( 'url', { linkIsFoo: true, linkIsBar: true, linkIsSth: true } );
  299. expect( getData( model ) ).to
  300. .equal( 'f[<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="true">ooba</$text>]r' );
  301. } );
  302. it( 'should add additional attributes to link when link is modified', () => {
  303. setData( model, 'f[<$text linkHref="foo">ooba</$text>]r' );
  304. command.execute( 'url', { linkIsFoo: true, linkIsBar: true, linkIsSth: true } );
  305. expect( getData( model ) ).to
  306. .equal( 'f[<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="true">ooba</$text>]r' );
  307. } );
  308. it( 'should remove additional attributes to link if those are falsy', () => {
  309. setData( model, 'foo[<$text linkHref="url" linkIsBar="true" linkIsFoo="true">url</$text>]bar' );
  310. command.execute( 'url', { linkIsFoo: false, linkIsBar: false } );
  311. expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
  312. } );
  313. } );
  314. describe( 'restoreManualDecoratorStates()', () => {
  315. it( 'synchronize values with current model state', () => {
  316. setData( model, 'foo<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="true">u[]rl</$text>bar' );
  317. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  318. linkIsFoo: true,
  319. linkIsBar: true,
  320. linkIsSth: true
  321. } );
  322. command.manualDecorators.first.value = false;
  323. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  324. linkIsFoo: false,
  325. linkIsBar: true,
  326. linkIsSth: true
  327. } );
  328. command.restoreManualDecoratorStates();
  329. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  330. linkIsFoo: true,
  331. linkIsBar: true,
  332. linkIsSth: true
  333. } );
  334. } );
  335. it( 'synchronize values with current model state when the decorator that is "on" default is "off"', () => {
  336. setData( model, 'foo<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="false">u[]rl</$text>bar' );
  337. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  338. linkIsFoo: true,
  339. linkIsBar: true,
  340. linkIsSth: false
  341. } );
  342. command.manualDecorators.last.value = true;
  343. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  344. linkIsFoo: true,
  345. linkIsBar: true,
  346. linkIsSth: true
  347. } );
  348. command.restoreManualDecoratorStates();
  349. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  350. linkIsFoo: true,
  351. linkIsBar: true,
  352. linkIsSth: false
  353. } );
  354. } );
  355. } );
  356. describe( '_getDecoratorStateFromModel', () => {
  357. it( 'obtain current values from the model', () => {
  358. setData( model, 'foo[<$text linkHref="url" linkIsBar="true">url</$text>]bar' );
  359. expect( command._getDecoratorStateFromModel( 'linkIsFoo' ) ).to.be.undefined;
  360. expect( command._getDecoratorStateFromModel( 'linkIsBar' ) ).to.be.true;
  361. } );
  362. } );
  363. } );
  364. } );
  365. function decoratorStates( manualDecorators ) {
  366. return Array.from( manualDecorators ).reduce( ( accumulator, currentValue ) => {
  367. accumulator[ currentValue.id ] = currentValue.value;
  368. return accumulator;
  369. }, {} );
  370. }