unlinkcommand.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 UnlinkCommand from '../src/unlinkcommand';
  7. import LinkEditing from '../src/linkediting';
  8. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  10. describe( 'UnlinkCommand', () => {
  11. let editor, model, document, command;
  12. testUtils.createSinonSandbox();
  13. beforeEach( () => {
  14. return ModelTestEditor.create()
  15. .then( newEditor => {
  16. editor = newEditor;
  17. model = editor.model;
  18. document = model.document;
  19. command = new UnlinkCommand( editor );
  20. model.schema.extend( '$text', {
  21. allowIn: '$root',
  22. allowAttributes: 'linkHref'
  23. } );
  24. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  25. } );
  26. } );
  27. afterEach( () => {
  28. return editor.destroy();
  29. } );
  30. describe( 'isEnabled', () => {
  31. it( 'should be true when selection has `linkHref` attribute', () => {
  32. model.change( writer => {
  33. writer.setSelectionAttribute( 'linkHref', 'value' );
  34. } );
  35. expect( command.isEnabled ).to.true;
  36. } );
  37. it( 'should be false when selection doesn\'t have `linkHref` attribute', () => {
  38. model.change( writer => {
  39. writer.removeSelectionAttribute( 'linkHref' );
  40. } );
  41. expect( command.isEnabled ).to.false;
  42. } );
  43. describe( 'for images', () => {
  44. beforeEach( () => {
  45. model.schema.register( 'image', { isBlock: true, allowWhere: '$text', allowAttributes: [ 'linkHref' ] } );
  46. } );
  47. it( 'should be true when an image is selected', () => {
  48. setData( model, '[<image linkHref="foo"></image>]' );
  49. expect( command.isEnabled ).to.be.true;
  50. } );
  51. it( 'should be true when an image and a text are selected', () => {
  52. setData( model, '[<image linkHref="foo"></image>Foo]' );
  53. expect( command.isEnabled ).to.be.true;
  54. } );
  55. it( 'should be true when a text and an image are selected', () => {
  56. setData( model, '[Foo<image linkHref="foo"></image>]' );
  57. expect( command.isEnabled ).to.be.true;
  58. } );
  59. it( 'should be true when two images are selected', () => {
  60. setData( model, '[<image linkHref="foo"></image><image linkHref="foo"></image>]' );
  61. expect( command.isEnabled ).to.be.true;
  62. } );
  63. it( 'should be false when a fake image is selected', () => {
  64. model.schema.register( 'fake', { isBlock: true, allowWhere: '$text' } );
  65. setData( model, '[<fake></fake>]' );
  66. expect( command.isEnabled ).to.be.false;
  67. } );
  68. it( 'should be false if an image does not accept the `linkHref` attribute in given context', () => {
  69. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  70. if ( ctx.endsWith( '$root image' ) && attributeName == 'linkHref' ) {
  71. return false;
  72. }
  73. } );
  74. setData( model, '[<image></image>]' );
  75. expect( command.isEnabled ).to.be.false;
  76. } );
  77. } );
  78. } );
  79. describe( 'execute()', () => {
  80. describe( 'non-collapsed selection', () => {
  81. it( 'should remove `linkHref` attribute from selected text', () => {
  82. setData( model, '<$text linkHref="url">f[ooba]r</$text>' );
  83. command.execute();
  84. expect( getData( model ) ).to.equal( '<$text linkHref="url">f</$text>[ooba]<$text linkHref="url">r</$text>' );
  85. } );
  86. it( 'should remove `linkHref` attribute from selected text and do not modified other attributes', () => {
  87. setData( model, '<$text bold="true" linkHref="url">f[ooba]r</$text>' );
  88. command.execute();
  89. const assertAll = () => {
  90. expect( getData( model ) ).to.equal(
  91. '<$text bold="true" linkHref="url">f</$text>' +
  92. '[<$text bold="true">ooba</$text>]' +
  93. '<$text bold="true" linkHref="url">r</$text>'
  94. );
  95. };
  96. const assertEdge = () => {
  97. expect( getData( model ) ).to.equal(
  98. '<$text bold="true" linkHref="url">f</$text>' +
  99. '[<$text bold="true">ooba]<$text linkHref="url">r</$text></$text>'
  100. );
  101. };
  102. testUtils.checkAssertions( assertAll, assertEdge );
  103. } );
  104. it( 'should remove `linkHref` attribute from selected text when attributes have different value', () => {
  105. setData( model, '[<$text linkHref="url">foo</$text><$text linkHref="other url">bar</$text>]' );
  106. command.execute();
  107. expect( getData( model ) ).to.equal( '[foobar]' );
  108. } );
  109. it( 'should remove `linkHref` attribute from selection', () => {
  110. setData( model, '<$text linkHref="url">f[ooba]r</$text>' );
  111. command.execute();
  112. expect( document.selection.hasAttribute( 'linkHref' ) ).to.false;
  113. } );
  114. } );
  115. describe( 'collapsed selection', () => {
  116. it( 'should remove `linkHref` attribute from selection siblings with the same attribute value', () => {
  117. setData( model, '<$text linkHref="url">foo[]bar</$text>' );
  118. command.execute();
  119. expect( getData( model ) ).to.equal( 'foo[]bar' );
  120. } );
  121. it( 'should remove `linkHref` attribute from selection siblings with the same attribute value and do not modify ' +
  122. 'other attributes', () => {
  123. setData(
  124. model,
  125. '<$text linkHref="other url">fo</$text>' +
  126. '<$text linkHref="url">o[]b</$text>' +
  127. '<$text linkHref="other url">ar</$text>'
  128. );
  129. command.execute();
  130. expect( getData( model ) ).to.equal(
  131. '<$text linkHref="other url">fo</$text>' +
  132. 'o[]b' +
  133. '<$text linkHref="other url">ar</$text>'
  134. );
  135. } );
  136. it( 'should do nothing with nodes with the same `linkHref` value when there is a node with different value `linkHref` ' +
  137. 'attribute between', () => {
  138. setData(
  139. model,
  140. '<$text linkHref="same url">f</$text>' +
  141. '<$text linkHref="other url">o</$text>' +
  142. '<$text linkHref="same url">o[]b</$text>' +
  143. '<$text linkHref="other url">a</$text>' +
  144. '<$text linkHref="same url">r</$text>'
  145. );
  146. command.execute();
  147. expect( getData( model ) )
  148. .to.equal(
  149. '<$text linkHref="same url">f</$text>' +
  150. '<$text linkHref="other url">o</$text>' +
  151. 'o[]b' +
  152. '<$text linkHref="other url">a</$text>' +
  153. '<$text linkHref="same url">r</$text>'
  154. );
  155. } );
  156. it( 'should remove `linkHref` attribute from selection siblings with the same attribute value ' +
  157. 'and do nothing with other attributes',
  158. () => {
  159. setData(
  160. model,
  161. '<$text linkHref="url">f</$text>' +
  162. '<$text bold="true" linkHref="url">o</$text>' +
  163. '<$text linkHref="url">o[]b</$text>' +
  164. '<$text bold="true" linkHref="url">a</$text>' +
  165. '<$text linkHref="url">r</$text>'
  166. );
  167. command.execute();
  168. expect( getData( model ) ).to.equal(
  169. 'f' +
  170. '<$text bold="true">o</$text>' +
  171. 'o[]b' +
  172. '<$text bold="true">a</$text>' +
  173. 'r'
  174. );
  175. } );
  176. it( 'should remove `linkHref` attribute from selection siblings only in the same parent as selection parent', () => {
  177. setData(
  178. model,
  179. '<p><$text linkHref="url">bar</$text></p>' +
  180. '<p><$text linkHref="url">fo[]o</$text></p>' +
  181. '<p><$text linkHref="url">bar</$text></p>'
  182. );
  183. command.execute();
  184. expect( getData( model ) ).to.equal(
  185. '<p><$text linkHref="url">bar</$text></p>' +
  186. '<p>fo[]o</p>' +
  187. '<p><$text linkHref="url">bar</$text></p>'
  188. );
  189. } );
  190. it( 'should remove `linkHref` attribute from selection siblings when selection is at the end of link', () => {
  191. setData( model, '<$text linkHref="url">foobar</$text>[]' );
  192. command.execute();
  193. expect( getData( model ) ).to.equal( 'foobar[]' );
  194. } );
  195. it( 'should remove `linkHref` attribute from selection siblings when selection is at the beginning of link', () => {
  196. setData( model, '[]<$text linkHref="url">foobar</$text>' );
  197. command.execute();
  198. expect( getData( model ) ).to.equal( '[]foobar' );
  199. } );
  200. it( 'should remove `linkHref` attribute from selection siblings on the left side when selection is between two elements with ' +
  201. 'different `linkHref` attributes',
  202. () => {
  203. setData( model, '<$text linkHref="url">foo</$text>[]<$text linkHref="other url">bar</$text>' );
  204. command.execute();
  205. expect( getData( model ) ).to.equal( 'foo[]<$text linkHref="other url">bar</$text>' );
  206. } );
  207. it( 'should remove `linkHref` attribute from selection', () => {
  208. setData( model, '<$text linkHref="url">foo[]bar</$text>' );
  209. command.execute();
  210. expect( document.selection.hasAttribute( 'linkHref' ) ).to.false;
  211. } );
  212. } );
  213. } );
  214. describe( 'manual decorators', () => {
  215. beforeEach( () => {
  216. editor.destroy();
  217. return ModelTestEditor.create( {
  218. extraPlugins: [ LinkEditing ],
  219. link: {
  220. decorators: {
  221. isFoo: {
  222. mode: 'manual',
  223. label: 'Foo',
  224. attributes: {
  225. class: 'foo'
  226. }
  227. },
  228. isBar: {
  229. mode: 'manual',
  230. label: 'Bar',
  231. attributes: {
  232. target: '_blank'
  233. }
  234. }
  235. }
  236. }
  237. } )
  238. .then( newEditor => {
  239. editor = newEditor;
  240. model = editor.model;
  241. document = model.document;
  242. command = new UnlinkCommand( editor );
  243. model.schema.extend( '$text', {
  244. allowIn: '$root',
  245. allowAttributes: [ 'linkHref', 'linkIsFoo', 'linkIsBar' ]
  246. } );
  247. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  248. } );
  249. } );
  250. afterEach( () => {
  251. return editor.destroy();
  252. } );
  253. it( 'should remove manual decorators from links together with linkHref', () => {
  254. setData( model, '<$text linkIsFoo="true" linkIsBar="true" linkHref="url">f[]oobar</$text>' );
  255. command.execute();
  256. expect( getData( model ) ).to.equal( 'f[]oobar' );
  257. } );
  258. } );
  259. } );