unlinkcommand.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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( 'should remove `linkHref` attribute from selection siblings with the same attribute value ' +
  111. 'and do nothing with other attributes',
  112. () => {
  113. setData(
  114. document,
  115. '<$text linkHref="url">f</$text>' +
  116. '<$text bold="true" linkHref="url">o</$text>' +
  117. '<$text linkHref="url">o[]b</$text>' +
  118. '<$text bold="true" linkHref="url">a</$text>' +
  119. '<$text linkHref="url">r</$text>'
  120. );
  121. command.execute();
  122. expect( getData( document ) ).to.equal(
  123. 'f' +
  124. '<$text bold="true">o</$text>' +
  125. 'o[]b' +
  126. '<$text bold="true">a</$text>' +
  127. 'r'
  128. );
  129. } );
  130. it( 'should remove `linkHref` attribute from selection siblings only in the same parent as selection parent', () => {
  131. setData(
  132. document,
  133. '<p><$text linkHref="url">bar</$text></p>' +
  134. '<p><$text linkHref="url">fo[]o</$text></p>' +
  135. '<p><$text linkHref="url">bar</$text></p>'
  136. );
  137. command.execute();
  138. expect( getData( document ) ).to.equal(
  139. '<p><$text linkHref="url">bar</$text></p>' +
  140. '<p>fo[]o</p>' +
  141. '<p><$text linkHref="url">bar</$text></p>'
  142. );
  143. } );
  144. it( 'should remove `linkHref` attribute from selection siblings when selection is at the end of link', () => {
  145. setData( document, '<$text linkHref="url">foobar</$text>[]' );
  146. command.execute();
  147. expect( getData( document ) ).to.equal( 'foobar[]' );
  148. } );
  149. it( 'should remove `linkHref` attribute from selection siblings when selection is at the beginning of link', () => {
  150. setData( document, '[]<$text linkHref="url">foobar</$text>' );
  151. command.execute();
  152. expect( getData( document ) ).to.equal( '[]foobar' );
  153. } );
  154. it( 'should remove `linkHref` attribute from selection siblings on the left side when selection is between two elements with ' +
  155. 'different `linkHref` attributes',
  156. () => {
  157. setData( document, '<$text linkHref="url">foo</$text>[]<$text linkHref="other url">bar</$text>' );
  158. command.execute();
  159. expect( getData( document ) ).to.equal( 'foo[]<$text linkHref="other url">bar</$text>' );
  160. } );
  161. it( 'should remove `linkHref` attribute from selection', () => {
  162. setData( document, '<$text linkHref="url">foo[]bar</$text>' );
  163. command.execute();
  164. expect( document.selection.hasAttribute( 'linkHref' ) ).to.false;
  165. } );
  166. } );
  167. } );
  168. } );