unlinkcommand.js 6.1 KB

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