unlinkcommand.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. describe( 'UnlinkCommand', () => {
  10. let editor, model, document, command;
  11. testUtils.createSinonSandbox();
  12. beforeEach( () => {
  13. return ModelTestEditor.create()
  14. .then( newEditor => {
  15. editor = newEditor;
  16. model = editor.model;
  17. document = model.document;
  18. command = new UnlinkCommand( editor );
  19. model.schema.extend( '$text', {
  20. allowIn: '$root',
  21. allowAttributes: 'linkHref'
  22. } );
  23. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  24. } );
  25. } );
  26. afterEach( () => {
  27. return editor.destroy();
  28. } );
  29. describe( 'isEnabled', () => {
  30. it( 'should be true when selection has `linkHref` attribute', () => {
  31. model.change( writer => {
  32. writer.setSelectionAttribute( 'linkHref', 'value' );
  33. } );
  34. expect( command.isEnabled ).to.true;
  35. } );
  36. it( 'should be false when selection doesn\'t have `linkHref` attribute', () => {
  37. model.change( writer => {
  38. writer.removeSelectionAttribute( 'linkHref' );
  39. } );
  40. expect( command.isEnabled ).to.false;
  41. } );
  42. } );
  43. describe( 'execute()', () => {
  44. describe( 'non-collapsed selection', () => {
  45. it( 'should remove `linkHref` attribute from selected text', () => {
  46. setData( model, '<$text linkHref="url">f[ooba]r</$text>' );
  47. command.execute();
  48. expect( getData( model ) ).to.equal( '<$text linkHref="url">f</$text>[ooba]<$text linkHref="url">r</$text>' );
  49. } );
  50. it( 'should remove `linkHref` attribute from selected text and do not modified other attributes', () => {
  51. setData( model, '<$text bold="true" linkHref="url">f[ooba]r</$text>' );
  52. command.execute();
  53. const assertAll = () => {
  54. expect( getData( model ) ).to.equal(
  55. '<$text bold="true" linkHref="url">f</$text>' +
  56. '[<$text bold="true">ooba</$text>]' +
  57. '<$text bold="true" linkHref="url">r</$text>'
  58. );
  59. };
  60. const assertEdge = () => {
  61. expect( getData( model ) ).to.equal(
  62. '<$text bold="true" linkHref="url">f</$text>' +
  63. '[<$text bold="true">ooba]<$text linkHref="url">r</$text></$text>'
  64. );
  65. };
  66. testUtils.checkAssertions( assertAll, assertEdge );
  67. } );
  68. it( 'should remove `linkHref` attribute from selected text when attributes have different value', () => {
  69. setData( model, '[<$text linkHref="url">foo</$text><$text linkHref="other url">bar</$text>]' );
  70. command.execute();
  71. expect( getData( model ) ).to.equal( '[foobar]' );
  72. } );
  73. it( 'should remove `linkHref` attribute from selection', () => {
  74. setData( model, '<$text linkHref="url">f[ooba]r</$text>' );
  75. command.execute();
  76. expect( document.selection.hasAttribute( 'linkHref' ) ).to.false;
  77. } );
  78. } );
  79. describe( 'collapsed selection', () => {
  80. it( 'should remove `linkHref` attribute from selection siblings with the same attribute value', () => {
  81. setData( model, '<$text linkHref="url">foo[]bar</$text>' );
  82. command.execute();
  83. expect( getData( model ) ).to.equal( 'foo[]bar' );
  84. } );
  85. it( 'should remove `linkHref` attribute from selection siblings with the same attribute value and do not modify ' +
  86. 'other attributes', () => {
  87. setData(
  88. model,
  89. '<$text linkHref="other url">fo</$text>' +
  90. '<$text linkHref="url">o[]b</$text>' +
  91. '<$text linkHref="other url">ar</$text>'
  92. );
  93. command.execute();
  94. expect( getData( model ) ).to.equal(
  95. '<$text linkHref="other url">fo</$text>' +
  96. 'o[]b' +
  97. '<$text linkHref="other url">ar</$text>'
  98. );
  99. } );
  100. it( 'should do nothing with nodes with the same `linkHref` value when there is a node with different value `linkHref` ' +
  101. 'attribute between', () => {
  102. setData(
  103. model,
  104. '<$text linkHref="same url">f</$text>' +
  105. '<$text linkHref="other url">o</$text>' +
  106. '<$text linkHref="same url">o[]b</$text>' +
  107. '<$text linkHref="other url">a</$text>' +
  108. '<$text linkHref="same url">r</$text>'
  109. );
  110. command.execute();
  111. expect( getData( model ) )
  112. .to.equal(
  113. '<$text linkHref="same url">f</$text>' +
  114. '<$text linkHref="other url">o</$text>' +
  115. 'o[]b' +
  116. '<$text linkHref="other url">a</$text>' +
  117. '<$text linkHref="same url">r</$text>'
  118. );
  119. } );
  120. it( 'should remove `linkHref` attribute from selection siblings with the same attribute value ' +
  121. 'and do nothing with other attributes',
  122. () => {
  123. setData(
  124. model,
  125. '<$text linkHref="url">f</$text>' +
  126. '<$text bold="true" linkHref="url">o</$text>' +
  127. '<$text linkHref="url">o[]b</$text>' +
  128. '<$text bold="true" linkHref="url">a</$text>' +
  129. '<$text linkHref="url">r</$text>'
  130. );
  131. command.execute();
  132. expect( getData( model ) ).to.equal(
  133. 'f' +
  134. '<$text bold="true">o</$text>' +
  135. 'o[]b' +
  136. '<$text bold="true">a</$text>' +
  137. 'r'
  138. );
  139. } );
  140. it( 'should remove `linkHref` attribute from selection siblings only in the same parent as selection parent', () => {
  141. setData(
  142. model,
  143. '<p><$text linkHref="url">bar</$text></p>' +
  144. '<p><$text linkHref="url">fo[]o</$text></p>' +
  145. '<p><$text linkHref="url">bar</$text></p>'
  146. );
  147. command.execute();
  148. expect( getData( model ) ).to.equal(
  149. '<p><$text linkHref="url">bar</$text></p>' +
  150. '<p>fo[]o</p>' +
  151. '<p><$text linkHref="url">bar</$text></p>'
  152. );
  153. } );
  154. it( 'should remove `linkHref` attribute from selection siblings when selection is at the end of link', () => {
  155. setData( model, '<$text linkHref="url">foobar</$text>[]' );
  156. command.execute();
  157. expect( getData( model ) ).to.equal( 'foobar[]' );
  158. } );
  159. it( 'should remove `linkHref` attribute from selection siblings when selection is at the beginning of link', () => {
  160. setData( model, '[]<$text linkHref="url">foobar</$text>' );
  161. command.execute();
  162. expect( getData( model ) ).to.equal( '[]foobar' );
  163. } );
  164. it( 'should remove `linkHref` attribute from selection siblings on the left side when selection is between two elements with ' +
  165. 'different `linkHref` attributes',
  166. () => {
  167. setData( model, '<$text linkHref="url">foo</$text>[]<$text linkHref="other url">bar</$text>' );
  168. command.execute();
  169. expect( getData( model ) ).to.equal( 'foo[]<$text linkHref="other url">bar</$text>' );
  170. } );
  171. it( 'should remove `linkHref` attribute from selection', () => {
  172. setData( model, '<$text linkHref="url">foo[]bar</$text>' );
  173. command.execute();
  174. expect( document.selection.hasAttribute( 'linkHref' ) ).to.false;
  175. } );
  176. } );
  177. } );
  178. } );