unlinkcommand.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from 'ckeditor5-core/tests/_utils/modeltesteditor';
  6. import UnlinkCommand from 'ckeditor5-link/src/unlinkcommand';
  7. import { setData, getData } from 'ckeditor5-engine/src/dev-utils/model';
  8. import testUtils from '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. command.destroy();
  27. } );
  28. describe( 'constructor()', () => {
  29. it( 'should listen on selection attribute change and refresh state', () => {
  30. const refreshStateSpy = testUtils.sinon.spy( command, 'refreshState' );
  31. document.selection.fire( 'change:attribute' );
  32. expect( refreshStateSpy.calledOnce ).to.true;
  33. } );
  34. } );
  35. describe( '_doExecute', () => {
  36. describe( 'non-collapsed selection', () => {
  37. it( 'should remove `linkHref` attribute from selected text', () => {
  38. setData( document, '<$text linkHref="url">f[ooba]r</$text>' );
  39. command._doExecute();
  40. expect( getData( document ) ).to.equal( '<$text linkHref="url">f</$text>[ooba]<$text linkHref="url">r</$text>' );
  41. } );
  42. it( 'should remove `linkHref` attribute from selected text and do not modified other attributes', () => {
  43. setData( document, '<$text bold="true" linkHref="url">f[ooba]r</$text>' );
  44. command._doExecute();
  45. expect( getData( document ) ).to.equal(
  46. '<$text bold="true" linkHref="url">f</$text>' +
  47. '[<$text bold="true">ooba</$text>]' +
  48. '<$text bold="true" linkHref="url">r</$text>'
  49. );
  50. } );
  51. it( 'should remove `linkHref` attribute from selected text when attributes have different value', () => {
  52. setData( document, '[<$text linkHref="url">foo</$text><$text linkHref="other url">bar</$text>]' );
  53. command._doExecute();
  54. expect( getData( document ) ).to.equal( '[foobar]' );
  55. } );
  56. it( 'should remove `linkHref` attribute from selection', () => {
  57. setData( document, '<$text linkHref="url">f[ooba]r</$text>' );
  58. command._doExecute();
  59. expect( document.selection.hasAttribute( 'linkHref' ) ).to.false;
  60. } );
  61. } );
  62. describe( 'collapsed selection', () => {
  63. it( 'should remove `linkHref` attribute from selection siblings with the same attribute value', () => {
  64. setData( document, '<$text linkHref="url">foo[]bar</$text>' );
  65. command._doExecute();
  66. expect( getData( document ) ).to.equal( 'foo[]bar' );
  67. } );
  68. it(
  69. 'should remove `linkHref` attribute from selection siblings with the same attribute value and do not modify other attributes',
  70. () => {
  71. setData(
  72. document,
  73. '<$text linkHref="other url">fo</$text>' +
  74. '<$text linkHref="url">o[]b</$text>' +
  75. '<$text linkHref="other url">ar</$text>'
  76. );
  77. command._doExecute();
  78. expect( getData( document ) ).to.equal(
  79. '<$text linkHref="other url">fo</$text>' +
  80. 'o[]b' +
  81. '<$text linkHref="other url">ar</$text>'
  82. );
  83. } );
  84. it( 'should do nothing with nodes with the same `linkHref` value when there is a node with different value `linkHref` ' +
  85. 'attribute between', () => {
  86. setData(
  87. document,
  88. '<$text linkHref="same url">f</$text>' +
  89. '<$text linkHref="other url">o</$text>' +
  90. '<$text linkHref="same url">o[]b</$text>' +
  91. '<$text linkHref="other url">a</$text>' +
  92. '<$text linkHref="same url">r</$text>'
  93. );
  94. command._doExecute();
  95. expect( getData( document ) )
  96. .to.equal(
  97. '<$text linkHref="same url">f</$text>' +
  98. '<$text linkHref="other url">o</$text>' +
  99. 'o[]b' +
  100. '<$text linkHref="other url">a</$text>' +
  101. '<$text linkHref="same url">r</$text>'
  102. );
  103. } );
  104. it(
  105. 'should remove `linkHref` attribute from selection siblings with the same attribute value and do nothing with other ' +
  106. 'attributes',
  107. () => {
  108. setData(
  109. document,
  110. '<$text linkHref="url">f</$text>' +
  111. '<$text bold="true" linkHref="url">o</$text>' +
  112. '<$text linkHref="url">o[]b</$text>' +
  113. '<$text bold="true" linkHref="url">a</$text>' +
  114. '<$text linkHref="url">r</$text>'
  115. );
  116. command._doExecute();
  117. expect( getData( document ) ).to.equal(
  118. 'f' +
  119. '<$text bold="true">o</$text>' +
  120. 'o[]b' +
  121. '<$text bold="true">a</$text>' +
  122. 'r'
  123. );
  124. } );
  125. it( 'should remove `linkHref` attribute from selection siblings only in the same parent as selection parent', () => {
  126. setData(
  127. document,
  128. '<p><$text linkHref="url">bar</$text></p>' +
  129. '<p><$text linkHref="url">fo[]o</$text></p>' +
  130. '<p><$text linkHref="url">bar</$text></p>'
  131. );
  132. command._doExecute();
  133. expect( getData( document ) ).to.equal(
  134. '<p><$text linkHref="url">bar</$text></p>' +
  135. '<p>fo[]o</p>' +
  136. '<p><$text linkHref="url">bar</$text></p>'
  137. );
  138. } );
  139. it( 'should remove `linkHref` attribute from selection siblings when selection is at the end of link', () => {
  140. setData( document, '<$text linkHref="url">foobar</$text>[]' );
  141. command._doExecute();
  142. expect( getData( document ) ).to.equal( 'foobar[]' );
  143. } );
  144. it( 'should remove `linkHref` attribute from selection siblings when selection is at the beginning of link', () => {
  145. setData( document, '[]<$text linkHref="url">foobar</$text>' );
  146. command._doExecute();
  147. expect( getData( document ) ).to.equal( '[]foobar' );
  148. } );
  149. it( 'should remove `linkHref` attribute from selection siblings on the left side when selection is between two elements with ' +
  150. 'different `linkHref` attributes',
  151. () => {
  152. setData( document, '<$text linkHref="url">foo</$text>[]<$text linkHref="other url">bar</$text>' );
  153. command._doExecute();
  154. expect( getData( document ) ).to.equal( 'foo[]<$text linkHref="other url">bar</$text>' );
  155. } );
  156. it( 'should remove `linkHref` attribute from selection', () => {
  157. setData( document, '<$text linkHref="url">foo[]bar</$text>' );
  158. command._doExecute();
  159. expect( document.selection.hasAttribute( 'linkHref' ) ).to.false;
  160. } );
  161. } );
  162. } );
  163. describe( '_checkEnabled', () => {
  164. it( 'should return false when selection has `linkHref` attribute', () => {
  165. document.selection.setAttribute( 'linkHref', 'value' );
  166. expect( command._checkEnabled() ).to.true;
  167. } );
  168. it( 'should return false when selection doesn\'t have `linkHref` attribute', () => {
  169. document.selection.removeAttribute( 'linkHref' );
  170. expect( command._checkEnabled() ).to.false;
  171. } );
  172. } );
  173. } );