linkcommand.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. describe( 'for images', () => {
  58. beforeEach( () => {
  59. model.schema.register( 'image', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
  60. } );
  61. it( 'should be true when an image is selected', () => {
  62. setData( model, '[<image linkHref="foo"></image>]' );
  63. expect( command.isEnabled ).to.be.true;
  64. } );
  65. it( 'should be true when an image and a text are selected', () => {
  66. setData( model, '[<image linkHref="foo"></image>Foo]' );
  67. expect( command.isEnabled ).to.be.true;
  68. } );
  69. it( 'should be true when a text and an image are selected', () => {
  70. setData( model, '[Foo<image linkHref="foo"></image>]' );
  71. expect( command.isEnabled ).to.be.true;
  72. } );
  73. it( 'should be true when two images are selected', () => {
  74. setData( model, '[<image linkHref="foo"></image><image linkHref="foo"></image>]' );
  75. expect( command.isEnabled ).to.be.true;
  76. } );
  77. it( 'should be false when a fake image is selected', () => {
  78. model.schema.register( 'fake', { isBlock: true, allowWhere: '$text' } );
  79. setData( model, '[<fake></fake>]' );
  80. expect( command.isEnabled ).to.be.false;
  81. } );
  82. it( 'should be false if an image does not accept the `linkHref` attribute in given context', () => {
  83. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  84. if ( ctx.endsWith( '$root image' ) && attributeName == 'linkHref' ) {
  85. return false;
  86. }
  87. } );
  88. setData( model, '[<image></image>]' );
  89. expect( command.isEnabled ).to.be.false;
  90. } );
  91. } );
  92. } );
  93. } );
  94. describe( 'value', () => {
  95. describe( 'collapsed selection', () => {
  96. it( 'should be equal attribute value when selection is placed inside element with `linkHref` attribute', () => {
  97. setData( model, '<$text linkHref="url">foo[]bar</$text>' );
  98. expect( command.value ).to.equal( 'url' );
  99. } );
  100. it( 'should be undefined when selection is placed inside element without `linkHref` attribute', () => {
  101. setData( model, '<$text bold="true">foo[]bar</$text>' );
  102. expect( command.value ).to.be.undefined;
  103. } );
  104. } );
  105. describe( 'non-collapsed selection', () => {
  106. it( 'should be equal attribute value when selection contains only elements with `linkHref` attribute', () => {
  107. setData( model, 'fo[<$text linkHref="url">ob</$text>]ar' );
  108. expect( command.value ).to.equal( 'url' );
  109. } );
  110. it( 'should be undefined when selection contains not only elements with `linkHref` attribute', () => {
  111. setData( model, 'f[o<$text linkHref="url">ob</$text>]ar' );
  112. expect( command.value ).to.be.undefined;
  113. } );
  114. } );
  115. describe( 'for images', () => {
  116. beforeEach( () => {
  117. model.schema.register( 'image', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
  118. } );
  119. it( 'should read the value from a selected image', () => {
  120. setData( model, '[<image linkHref="foo"></image>]' );
  121. expect( command.value ).to.be.equal( 'foo' );
  122. } );
  123. it( 'should read the value from a selected image and ignore a text node', () => {
  124. setData( model, '[<image linkHref="foo"></image><p><$text linkHref="bar">bar</$text>]</p>' );
  125. expect( command.value ).to.be.equal( 'foo' );
  126. } );
  127. it( 'should read the value from a selected text node and ignore an image', () => {
  128. setData( model, '<p>[<$text linkHref="bar">bar</$text></p><image linkHref="foo"></image>]' );
  129. expect( command.value ).to.be.equal( 'bar' );
  130. } );
  131. it( 'should be undefined when a fake image is selected', () => {
  132. model.schema.register( 'fake', { isBlock: true, allowWhere: '$text' } );
  133. setData( model, '[<fake></fake>]' );
  134. expect( command.value ).to.be.undefined;
  135. } );
  136. } );
  137. } );
  138. describe( 'execute()', () => {
  139. describe( 'non-collapsed selection', () => {
  140. it( 'should set `linkHref` attribute to selected text', () => {
  141. setData( model, 'f[ooba]r' );
  142. expect( command.value ).to.be.undefined;
  143. command.execute( 'url' );
  144. expect( getData( model ) ).to.equal( 'f[<$text linkHref="url">ooba</$text>]r' );
  145. expect( command.value ).to.equal( 'url' );
  146. } );
  147. it( 'should set `linkHref` attribute to selected text when text already has attributes', () => {
  148. setData( model, 'f[o<$text bold="true">oba]r</$text>' );
  149. expect( command.value ).to.be.undefined;
  150. command.execute( 'url' );
  151. expect( command.value ).to.equal( 'url' );
  152. expect( getData( model ) ).to.equal(
  153. 'f[<$text linkHref="url">o</$text>' +
  154. '<$text bold="true" linkHref="url">oba</$text>]' +
  155. '<$text bold="true">r</$text>'
  156. );
  157. } );
  158. it( 'should overwrite existing `linkHref` attribute when selected text wraps text with `linkHref` attribute', () => {
  159. setData( model, 'f[o<$text linkHref="other url">o</$text>ba]r' );
  160. expect( command.value ).to.be.undefined;
  161. command.execute( 'url' );
  162. expect( getData( model ) ).to.equal( 'f[<$text linkHref="url">ooba</$text>]r' );
  163. expect( command.value ).to.equal( 'url' );
  164. } );
  165. it( 'should split text and overwrite attribute value when selection is inside text with `linkHref` attribute', () => {
  166. setData( model, 'f<$text linkHref="other url">o[ob]a</$text>r' );
  167. expect( command.value ).to.equal( 'other url' );
  168. command.execute( 'url' );
  169. expect( getData( model ) ).to.equal(
  170. 'f' +
  171. '<$text linkHref="other url">o</$text>' +
  172. '[<$text linkHref="url">ob</$text>]' +
  173. '<$text linkHref="other url">a</$text>' +
  174. 'r'
  175. );
  176. expect( command.value ).to.equal( 'url' );
  177. } );
  178. it( 'should overwrite `linkHref` attribute of selected text only, ' +
  179. 'when selection start inside text with `linkHref` attribute',
  180. () => {
  181. setData( model, 'f<$text linkHref="other url">o[o</$text>ba]r' );
  182. expect( command.value ).to.equal( 'other url' );
  183. command.execute( 'url' );
  184. expect( getData( model ) ).to.equal( 'f<$text linkHref="other url">o</$text>[<$text linkHref="url">oba</$text>]r' );
  185. expect( command.value ).to.equal( 'url' );
  186. } );
  187. it( 'should overwrite `linkHref` attribute of selected text only, when selection end inside text with `linkHref` ' +
  188. 'attribute', () => {
  189. setData( model, 'f[o<$text linkHref="other url">ob]a</$text>r' );
  190. expect( command.value ).to.be.undefined;
  191. command.execute( 'url' );
  192. expect( getData( model ) ).to.equal( 'f[<$text linkHref="url">oob</$text>]<$text linkHref="other url">a</$text>r' );
  193. expect( command.value ).to.equal( 'url' );
  194. } );
  195. it( 'should set `linkHref` attribute to selected text when text is split by $block element', () => {
  196. setData( model, '<p>f[oo</p><p>ba]r</p>' );
  197. expect( command.value ).to.be.undefined;
  198. command.execute( 'url' );
  199. expect( getData( model ) )
  200. .to.equal( '<p>f[<$text linkHref="url">oo</$text></p><p><$text linkHref="url">ba</$text>]r</p>' );
  201. expect( command.value ).to.equal( 'url' );
  202. } );
  203. it( 'should set `linkHref` attribute to allowed elements', () => {
  204. model.schema.register( 'image', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
  205. setData( model, '<p>f[oo<image></image>ba]r</p>' );
  206. expect( command.value ).to.be.undefined;
  207. command.execute( 'url' );
  208. expect( getData( model ) ).to.equal(
  209. '<p>f[<$text linkHref="url">oo</$text><image linkHref="url"></image><$text linkHref="url">ba</$text>]r</p>'
  210. );
  211. expect( command.value ).to.equal( 'url' );
  212. } );
  213. it( 'should set `linkHref` attribute to nested allowed elements', () => {
  214. model.schema.register( 'image', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
  215. model.schema.register( 'blockQuote', { allowWhere: '$block', allowContentOf: '$root' } );
  216. setData( model, '<p>foo</p>[<blockQuote><image></image></blockQuote>]<p>bar</p>' );
  217. command.execute( 'url' );
  218. expect( getData( model ) )
  219. .to.equal( '<p>foo</p>[<blockQuote><image linkHref="url"></image></blockQuote>]<p>bar</p>' );
  220. } );
  221. it( 'should set `linkHref` attribute to allowed elements on multi-selection', () => {
  222. model.schema.register( 'image', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
  223. setData( model, '<p>[<image></image>][<image></image>]</p>' );
  224. command.execute( 'url' );
  225. expect( getData( model ) )
  226. .to.equal( '<p>[<image linkHref="url"></image>][<image linkHref="url"></image>]</p>' );
  227. } );
  228. it( 'should set `linkHref` attribute to allowed elements and omit disallowed', () => {
  229. model.schema.register( 'image', { isBlock: true, allowWhere: '$text' } );
  230. model.schema.register( 'caption', { allowIn: 'image' } );
  231. model.schema.extend( '$text', { allowIn: 'caption' } );
  232. setData( model, '<p>f[oo<image><caption>xxx</caption></image>ba]r</p>' );
  233. command.execute( 'url' );
  234. expect( getData( model ) ).to.equal(
  235. '<p>' +
  236. 'f[<$text linkHref="url">oo</$text>' +
  237. '<image><caption><$text linkHref="url">xxx</$text></caption></image>' +
  238. '<$text linkHref="url">ba</$text>]r' +
  239. '</p>'
  240. );
  241. } );
  242. it( 'should set `linkHref` attribute to allowed elements and omit their children even if they accept the attribute', () => {
  243. model.schema.register( 'image', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
  244. model.schema.register( 'caption', { allowIn: 'image' } );
  245. model.schema.extend( '$text', { allowIn: 'caption' } );
  246. setData( model, '<p>f[oo<image><caption>xxx</caption></image>ba]r</p>' );
  247. command.execute( 'url' );
  248. expect( getData( model ) ).to.equal(
  249. '<p>' +
  250. 'f[<$text linkHref="url">oo</$text>' +
  251. '<image linkHref="url"><caption>xxx</caption></image>' +
  252. '<$text linkHref="url">ba</$text>]r' +
  253. '</p>'
  254. );
  255. } );
  256. } );
  257. describe( 'collapsed selection', () => {
  258. it( 'should insert text with `linkHref` attribute, text data equal to href and put the selection after the new link', () => {
  259. setData( model, 'foo[]bar' );
  260. command.execute( 'url' );
  261. expect( getData( model ) ).to.equal( 'foo<$text linkHref="url">url</$text>[]bar' );
  262. } );
  263. it( 'should insert text with `linkHref` attribute, and selection attributes', () => {
  264. setData( model, '<$text bold="true">foo[]bar</$text>', {
  265. selectionAttributes: { bold: true }
  266. } );
  267. command.execute( 'url' );
  268. expect( getData( model ) ).to.equal(
  269. '<$text bold="true">foo</$text><$text bold="true" linkHref="url">url</$text><$text bold="true">[]bar</$text>'
  270. );
  271. } );
  272. it( 'should update `linkHref` attribute (text with `linkHref` attribute) and put the selection after the node', () => {
  273. setData( model, '<$text linkHref="other url">foo[]bar</$text>' );
  274. command.execute( 'url' );
  275. expect( getData( model ) ).to.equal( '<$text linkHref="url">foobar</$text>[]' );
  276. } );
  277. it( 'should not insert text with `linkHref` attribute when is not allowed in parent', () => {
  278. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  279. if ( ctx.endsWith( 'p $text' ) && attributeName == 'linkHref' ) {
  280. return false;
  281. }
  282. } );
  283. setData( model, '<p>foo[]bar</p>' );
  284. command.execute( 'url' );
  285. expect( getData( model ) ).to.equal( '<p>foo[]bar</p>' );
  286. } );
  287. it( 'should not insert text node if link is empty', () => {
  288. setData( model, '<p>foo[]bar</p>' );
  289. command.execute( '' );
  290. expect( getData( model ) ).to.equal( '<p>foo[]bar</p>' );
  291. } );
  292. } );
  293. } );
  294. describe( 'manual decorators', () => {
  295. beforeEach( () => {
  296. editor.destroy();
  297. return ModelTestEditor.create()
  298. .then( newEditor => {
  299. editor = newEditor;
  300. model = editor.model;
  301. command = new LinkCommand( editor );
  302. command.manualDecorators.add( new ManualDecorator( {
  303. id: 'linkIsFoo',
  304. label: 'Foo',
  305. attributes: {
  306. class: 'Foo'
  307. }
  308. } ) );
  309. command.manualDecorators.add( new ManualDecorator( {
  310. id: 'linkIsBar',
  311. label: 'Bar',
  312. attributes: {
  313. target: '_blank'
  314. }
  315. } ) );
  316. command.manualDecorators.add( new ManualDecorator( {
  317. id: 'linkIsSth',
  318. label: 'Sth',
  319. attributes: {
  320. class: 'sth'
  321. },
  322. defaultValue: true
  323. } ) );
  324. model.schema.extend( '$text', {
  325. allowIn: '$root',
  326. allowAttributes: [ 'linkHref', 'linkIsFoo', 'linkIsBar', 'linkIsSth' ]
  327. } );
  328. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  329. } );
  330. } );
  331. afterEach( () => {
  332. return editor.destroy();
  333. } );
  334. describe( 'collapsed selection', () => {
  335. it( 'should insert additional attributes to link when it is created', () => {
  336. setData( model, 'foo[]bar' );
  337. command.execute( 'url', { linkIsFoo: true, linkIsBar: true, linkIsSth: true } );
  338. expect( getData( model ) ).to
  339. .equal( 'foo<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="true">url</$text>[]bar' );
  340. } );
  341. it( 'should add additional attributes to link when link is modified', () => {
  342. setData( model, 'f<$text linkHref="url">o[]oba</$text>r' );
  343. command.execute( 'url', { linkIsFoo: true, linkIsBar: true, linkIsSth: true } );
  344. expect( getData( model ) ).to
  345. .equal( 'f<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="true">ooba</$text>[]r' );
  346. } );
  347. it( 'should remove additional attributes to link if those are falsy', () => {
  348. setData( model, 'foo<$text linkHref="url" linkIsBar="true" linkIsFoo="true">u[]rl</$text>bar' );
  349. command.execute( 'url', { linkIsFoo: false, linkIsBar: false } );
  350. expect( getData( model ) ).to.equal( 'foo<$text linkHref="url">url</$text>[]bar' );
  351. } );
  352. } );
  353. describe( 'range selection', () => {
  354. it( 'should insert additional attributes to link when it is created', () => {
  355. setData( model, 'f[ooba]r' );
  356. command.execute( 'url', { linkIsFoo: true, linkIsBar: true, linkIsSth: true } );
  357. expect( getData( model ) ).to
  358. .equal( 'f[<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="true">ooba</$text>]r' );
  359. } );
  360. it( 'should add additional attributes to link when link is modified', () => {
  361. setData( model, 'f[<$text linkHref="foo">ooba</$text>]r' );
  362. command.execute( 'url', { linkIsFoo: true, linkIsBar: true, linkIsSth: true } );
  363. expect( getData( model ) ).to
  364. .equal( 'f[<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="true">ooba</$text>]r' );
  365. } );
  366. it( 'should remove additional attributes to link if those are falsy', () => {
  367. setData( model, 'foo[<$text linkHref="url" linkIsBar="true" linkIsFoo="true">url</$text>]bar' );
  368. command.execute( 'url', { linkIsFoo: false, linkIsBar: false } );
  369. expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
  370. } );
  371. } );
  372. describe( 'restoreManualDecoratorStates()', () => {
  373. it( 'synchronize values with current model state', () => {
  374. setData( model, 'foo<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="true">u[]rl</$text>bar' );
  375. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  376. linkIsFoo: true,
  377. linkIsBar: true,
  378. linkIsSth: true
  379. } );
  380. command.manualDecorators.first.value = false;
  381. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  382. linkIsFoo: false,
  383. linkIsBar: true,
  384. linkIsSth: true
  385. } );
  386. command.restoreManualDecoratorStates();
  387. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  388. linkIsFoo: true,
  389. linkIsBar: true,
  390. linkIsSth: true
  391. } );
  392. } );
  393. it( 'synchronize values with current model state when the decorator that is "on" default is "off"', () => {
  394. setData( model, 'foo<$text linkHref="url" linkIsBar="true" linkIsFoo="true" linkIsSth="false">u[]rl</$text>bar' );
  395. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  396. linkIsFoo: true,
  397. linkIsBar: true,
  398. linkIsSth: false
  399. } );
  400. command.manualDecorators.last.value = true;
  401. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  402. linkIsFoo: true,
  403. linkIsBar: true,
  404. linkIsSth: true
  405. } );
  406. command.restoreManualDecoratorStates();
  407. expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
  408. linkIsFoo: true,
  409. linkIsBar: true,
  410. linkIsSth: false
  411. } );
  412. } );
  413. } );
  414. describe( '_getDecoratorStateFromModel', () => {
  415. it( 'obtain current values from the model', () => {
  416. setData( model, 'foo[<$text linkHref="url" linkIsBar="true">url</$text>]bar' );
  417. expect( command._getDecoratorStateFromModel( 'linkIsFoo' ) ).to.be.undefined;
  418. expect( command._getDecoratorStateFromModel( 'linkIsBar' ) ).to.be.true;
  419. } );
  420. } );
  421. } );
  422. } );
  423. function decoratorStates( manualDecorators ) {
  424. return Array.from( manualDecorators ).reduce( ( accumulator, currentValue ) => {
  425. accumulator[ currentValue.id ] = currentValue.value;
  426. return accumulator;
  427. }, {} );
  428. }