8
0

unlinkcommand.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. command.destroy();
  27. } );
  28. describe( 'constructor()', () => {
  29. it( 'should listen on document#changesDone and refresh the state', () => {
  30. const refreshStateSpy = testUtils.sinon.spy( command, 'refreshState' );
  31. document.fire( 'changesDone' );
  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( 'should remove `linkHref` attribute from selection siblings with the same attribute value and do not modify ' +
  69. 'other attributes', () => {
  70. setData(
  71. document,
  72. '<$text linkHref="other url">fo</$text>' +
  73. '<$text linkHref="url">o[]b</$text>' +
  74. '<$text linkHref="other url">ar</$text>'
  75. );
  76. command._doExecute();
  77. expect( getData( document ) ).to.equal(
  78. '<$text linkHref="other url">fo</$text>' +
  79. 'o[]b' +
  80. '<$text linkHref="other url">ar</$text>'
  81. );
  82. } );
  83. it( 'should do nothing with nodes with the same `linkHref` value when there is a node with different value `linkHref` ' +
  84. 'attribute between', () => {
  85. setData(
  86. document,
  87. '<$text linkHref="same url">f</$text>' +
  88. '<$text linkHref="other url">o</$text>' +
  89. '<$text linkHref="same url">o[]b</$text>' +
  90. '<$text linkHref="other url">a</$text>' +
  91. '<$text linkHref="same url">r</$text>'
  92. );
  93. command._doExecute();
  94. expect( getData( document ) )
  95. .to.equal(
  96. '<$text linkHref="same url">f</$text>' +
  97. '<$text linkHref="other url">o</$text>' +
  98. 'o[]b' +
  99. '<$text linkHref="other url">a</$text>' +
  100. '<$text linkHref="same url">r</$text>'
  101. );
  102. } );
  103. it(
  104. 'should remove `linkHref` attribute from selection siblings with the same attribute value and do nothing with other ' +
  105. 'attributes',
  106. () => {
  107. setData(
  108. document,
  109. '<$text linkHref="url">f</$text>' +
  110. '<$text bold="true" linkHref="url">o</$text>' +
  111. '<$text linkHref="url">o[]b</$text>' +
  112. '<$text bold="true" linkHref="url">a</$text>' +
  113. '<$text linkHref="url">r</$text>'
  114. );
  115. command._doExecute();
  116. expect( getData( document ) ).to.equal(
  117. 'f' +
  118. '<$text bold="true">o</$text>' +
  119. 'o[]b' +
  120. '<$text bold="true">a</$text>' +
  121. 'r'
  122. );
  123. } );
  124. it( 'should remove `linkHref` attribute from selection siblings only in the same parent as selection parent', () => {
  125. setData(
  126. document,
  127. '<p><$text linkHref="url">bar</$text></p>' +
  128. '<p><$text linkHref="url">fo[]o</$text></p>' +
  129. '<p><$text linkHref="url">bar</$text></p>'
  130. );
  131. command._doExecute();
  132. expect( getData( document ) ).to.equal(
  133. '<p><$text linkHref="url">bar</$text></p>' +
  134. '<p>fo[]o</p>' +
  135. '<p><$text linkHref="url">bar</$text></p>'
  136. );
  137. } );
  138. it( 'should remove `linkHref` attribute from selection siblings when selection is at the end of link', () => {
  139. setData( document, '<$text linkHref="url">foobar</$text>[]' );
  140. command._doExecute();
  141. expect( getData( document ) ).to.equal( 'foobar[]' );
  142. } );
  143. it( 'should remove `linkHref` attribute from selection siblings when selection is at the beginning of link', () => {
  144. setData( document, '[]<$text linkHref="url">foobar</$text>' );
  145. command._doExecute();
  146. expect( getData( document ) ).to.equal( '[]foobar' );
  147. } );
  148. it( 'should remove `linkHref` attribute from selection siblings on the left side when selection is between two elements with ' +
  149. 'different `linkHref` attributes',
  150. () => {
  151. setData( document, '<$text linkHref="url">foo</$text>[]<$text linkHref="other url">bar</$text>' );
  152. command._doExecute();
  153. expect( getData( document ) ).to.equal( 'foo[]<$text linkHref="other url">bar</$text>' );
  154. } );
  155. it( 'should remove `linkHref` attribute from selection', () => {
  156. setData( document, '<$text linkHref="url">foo[]bar</$text>' );
  157. command._doExecute();
  158. expect( document.selection.hasAttribute( 'linkHref' ) ).to.false;
  159. } );
  160. } );
  161. } );
  162. describe( '_checkEnabled', () => {
  163. it( 'should return false when selection has `linkHref` attribute', () => {
  164. document.selection.setAttribute( 'linkHref', 'value' );
  165. expect( command._checkEnabled() ).to.true;
  166. } );
  167. it( 'should return false when selection doesn\'t have `linkHref` attribute', () => {
  168. document.selection.removeAttribute( 'linkHref' );
  169. expect( command._checkEnabled() ).to.false;
  170. } );
  171. } );
  172. } );