8
0

unlinkcommand.js 6.1 KB

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