unlinkcommand.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. expect( getData( document ) ).to.equal(
  53. '<$text bold="true" linkHref="url">f</$text>' +
  54. '[<$text bold="true">ooba</$text>]' +
  55. '<$text bold="true" linkHref="url">r</$text>'
  56. );
  57. } );
  58. it( 'should remove `linkHref` attribute from selected text when attributes have different value', () => {
  59. setData( document, '[<$text linkHref="url">foo</$text><$text linkHref="other url">bar</$text>]' );
  60. command.execute();
  61. expect( getData( document ) ).to.equal( '[foobar]' );
  62. } );
  63. it( 'should remove `linkHref` attribute from selection', () => {
  64. setData( document, '<$text linkHref="url">f[ooba]r</$text>' );
  65. command.execute();
  66. expect( document.selection.hasAttribute( 'linkHref' ) ).to.false;
  67. } );
  68. } );
  69. describe( 'collapsed selection', () => {
  70. it( 'should remove `linkHref` attribute from selection siblings with the same attribute value', () => {
  71. setData( document, '<$text linkHref="url">foo[]bar</$text>' );
  72. command.execute();
  73. expect( getData( document ) ).to.equal( 'foo[]bar' );
  74. } );
  75. it( 'should remove `linkHref` attribute from selection siblings with the same attribute value and do not modify ' +
  76. 'other attributes', () => {
  77. setData(
  78. document,
  79. '<$text linkHref="other url">fo</$text>' +
  80. '<$text linkHref="url">o[]b</$text>' +
  81. '<$text linkHref="other url">ar</$text>'
  82. );
  83. command.execute();
  84. expect( getData( document ) ).to.equal(
  85. '<$text linkHref="other url">fo</$text>' +
  86. 'o[]b' +
  87. '<$text linkHref="other url">ar</$text>'
  88. );
  89. } );
  90. it( 'should do nothing with nodes with the same `linkHref` value when there is a node with different value `linkHref` ' +
  91. 'attribute between', () => {
  92. setData(
  93. document,
  94. '<$text linkHref="same url">f</$text>' +
  95. '<$text linkHref="other url">o</$text>' +
  96. '<$text linkHref="same url">o[]b</$text>' +
  97. '<$text linkHref="other url">a</$text>' +
  98. '<$text linkHref="same url">r</$text>'
  99. );
  100. command.execute();
  101. expect( getData( document ) )
  102. .to.equal(
  103. '<$text linkHref="same url">f</$text>' +
  104. '<$text linkHref="other url">o</$text>' +
  105. 'o[]b' +
  106. '<$text linkHref="other url">a</$text>' +
  107. '<$text linkHref="same url">r</$text>'
  108. );
  109. } );
  110. it(
  111. 'should remove `linkHref` attribute from selection siblings with the same attribute value and do nothing with other ' +
  112. 'attributes',
  113. () => {
  114. setData(
  115. document,
  116. '<$text linkHref="url">f</$text>' +
  117. '<$text bold="true" linkHref="url">o</$text>' +
  118. '<$text linkHref="url">o[]b</$text>' +
  119. '<$text bold="true" linkHref="url">a</$text>' +
  120. '<$text linkHref="url">r</$text>'
  121. );
  122. command.execute();
  123. expect( getData( document ) ).to.equal(
  124. 'f' +
  125. '<$text bold="true">o</$text>' +
  126. 'o[]b' +
  127. '<$text bold="true">a</$text>' +
  128. 'r'
  129. );
  130. } );
  131. it( 'should remove `linkHref` attribute from selection siblings only in the same parent as selection parent', () => {
  132. setData(
  133. document,
  134. '<p><$text linkHref="url">bar</$text></p>' +
  135. '<p><$text linkHref="url">fo[]o</$text></p>' +
  136. '<p><$text linkHref="url">bar</$text></p>'
  137. );
  138. command.execute();
  139. expect( getData( document ) ).to.equal(
  140. '<p><$text linkHref="url">bar</$text></p>' +
  141. '<p>fo[]o</p>' +
  142. '<p><$text linkHref="url">bar</$text></p>'
  143. );
  144. } );
  145. it( 'should remove `linkHref` attribute from selection siblings when selection is at the end of link', () => {
  146. setData( document, '<$text linkHref="url">foobar</$text>[]' );
  147. command.execute();
  148. expect( getData( document ) ).to.equal( 'foobar[]' );
  149. } );
  150. it( 'should remove `linkHref` attribute from selection siblings when selection is at the beginning of link', () => {
  151. setData( document, '[]<$text linkHref="url">foobar</$text>' );
  152. command.execute();
  153. expect( getData( document ) ).to.equal( '[]foobar' );
  154. } );
  155. it( 'should remove `linkHref` attribute from selection siblings on the left side when selection is between two elements with ' +
  156. 'different `linkHref` attributes',
  157. () => {
  158. setData( document, '<$text linkHref="url">foo</$text>[]<$text linkHref="other url">bar</$text>' );
  159. command.execute();
  160. expect( getData( document ) ).to.equal( 'foo[]<$text linkHref="other url">bar</$text>' );
  161. } );
  162. it( 'should remove `linkHref` attribute from selection', () => {
  163. setData( document, '<$text linkHref="url">foo[]bar</$text>' );
  164. command.execute();
  165. expect( document.selection.hasAttribute( 'linkHref' ) ).to.false;
  166. } );
  167. } );
  168. } );
  169. } );