unlinkcommand.js 6.9 KB

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