linkcommand.js 20 KB

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