8
0

remove.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import DowncastWriter from '../../../src/view/downcastwriter';
  6. import ContainerElement from '../../../src/view/containerelement';
  7. import Range from '../../../src/view/range';
  8. import DocumentFragment from '../../../src/view/documentfragment';
  9. import { stringify, parse } from '../../../src/dev-utils/view';
  10. import AttributeElement from '../../../src/view/attributeelement';
  11. import EmptyElement from '../../../src/view/emptyelement';
  12. import UIElement from '../../../src/view/uielement';
  13. import Document from '../../../src/view/document';
  14. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  15. describe( 'DowncastWriter', () => {
  16. describe( 'remove()', () => {
  17. let writer, document;
  18. // Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
  19. // test ranges.
  20. //
  21. // @param {String} input
  22. // @param {String} expectedResult
  23. // @param {String} expectedRemoved
  24. // @param {Boolean} asItem
  25. function test( input, expectedResult, expectedRemoved, asItem = false ) {
  26. const { view, selection } = parse( input );
  27. const range = selection.getFirstRange();
  28. const rangeOrItem = asItem ? Array.from( range.getItems() )[ 0 ] : range;
  29. const removed = writer.remove( rangeOrItem );
  30. expect( stringify( view, asItem ? null : range, { showType: true, showPriority: true } ) ).to.equal( expectedResult );
  31. expect( stringify( removed, null, { showType: true, showPriority: true } ) ).to.equal( expectedRemoved );
  32. }
  33. beforeEach( () => {
  34. document = new Document();
  35. writer = new DowncastWriter( document );
  36. } );
  37. it( 'should throw when range placed in two containers', () => {
  38. const p1 = new ContainerElement( 'p' );
  39. const p2 = new ContainerElement( 'p' );
  40. expectToThrowCKEditorError( () => {
  41. writer.remove( Range._createFromParentsAndOffsets( p1, 0, p2, 0 ) );
  42. }, 'view-writer-invalid-range-container', document );
  43. } );
  44. it( 'should throw when range has no parent container', () => {
  45. const el = new AttributeElement( 'b' );
  46. expectToThrowCKEditorError( () => {
  47. writer.remove( Range._createFromParentsAndOffsets( el, 0, el, 0 ) );
  48. }, 'view-writer-invalid-range-container', document );
  49. } );
  50. it( 'should return empty DocumentFragment when range is collapsed', () => {
  51. const p = new ContainerElement( 'p' );
  52. const range = Range._createFromParentsAndOffsets( p, 0, p, 0 );
  53. const fragment = writer.remove( range );
  54. expect( fragment ).to.be.instanceof( DocumentFragment );
  55. expect( fragment.childCount ).to.equal( 0 );
  56. expect( range.isCollapsed ).to.be.true;
  57. } );
  58. it( 'should remove single text node', () => {
  59. test( '<container:p>[foobar]</container:p>', '<container:p>[]</container:p>', 'foobar' );
  60. } );
  61. it( 'should not leave empty text nodes', () => {
  62. test( '<container:p>{foobar}</container:p>', '<container:p>[]</container:p>', 'foobar' );
  63. } );
  64. it( 'should remove part of the text node', () => {
  65. test( '<container:p>f{oob}ar</container:p>', '<container:p>f{}ar</container:p>', 'oob' );
  66. } );
  67. it( 'should remove parts of nodes #1', () => {
  68. test(
  69. '<container:p>f{oo<attribute:b view-priority="10">ba}r</attribute:b></container:p>',
  70. '<container:p>f[]<attribute:b view-priority="10">r</attribute:b></container:p>',
  71. 'oo<attribute:b view-priority="10">ba</attribute:b>'
  72. );
  73. } );
  74. it( 'should support unicode', () => {
  75. test(
  76. '<container:p>நி{லை<attribute:b view-priority="10">க்}கு</attribute:b></container:p>',
  77. '<container:p>நி[]<attribute:b view-priority="10">கு</attribute:b></container:p>',
  78. 'லை<attribute:b view-priority="10">க்</attribute:b>'
  79. );
  80. } );
  81. it( 'should merge after removing #1', () => {
  82. test(
  83. '<container:p>' +
  84. '<attribute:b view-priority="1">foo</attribute:b>[bar]<attribute:b view-priority="1">bazqux</attribute:b>' +
  85. '</container:p>',
  86. '<container:p><attribute:b view-priority="1">foo{}bazqux</attribute:b></container:p>',
  87. 'bar'
  88. );
  89. } );
  90. it( 'should merge after removing #2', () => {
  91. test(
  92. '<container:p>' +
  93. '<attribute:b view-priority="1">fo{o</attribute:b>bar<attribute:b view-priority="1">ba}zqux</attribute:b>' +
  94. '</container:p>',
  95. '<container:p><attribute:b view-priority="1">fo{}zqux</attribute:b></container:p>',
  96. '<attribute:b view-priority="1">o</attribute:b>bar<attribute:b view-priority="1">ba</attribute:b>'
  97. );
  98. } );
  99. it( 'should remove part of the text node in document fragment', () => {
  100. test( 'fo{ob}ar', 'fo{}ar', 'ob' );
  101. } );
  102. it( 'should remove EmptyElement', () => {
  103. test(
  104. '<container:p>foo[<empty:img></empty:img>]bar</container:p>',
  105. '<container:p>foo{}bar</container:p>',
  106. '<empty:img></empty:img>'
  107. );
  108. } );
  109. it( 'should throw if range is placed inside EmptyElement', () => {
  110. const emptyElement = new EmptyElement( 'img' );
  111. const attributeElement = new AttributeElement( 'b' );
  112. new ContainerElement( 'p', null, [ emptyElement, attributeElement ] ); // eslint-disable-line no-new
  113. const range = Range._createFromParentsAndOffsets( emptyElement, 0, attributeElement, 0 );
  114. expectToThrowCKEditorError( () => {
  115. writer.remove( range );
  116. }, 'view-writer-cannot-break-empty-element', document );
  117. } );
  118. it( 'should remove UIElement', () => {
  119. test(
  120. '<container:p>foo[<ui:span></ui:span>]bar</container:p>',
  121. '<container:p>foo{}bar</container:p>',
  122. '<ui:span></ui:span>'
  123. );
  124. } );
  125. it( 'should throw if range is placed inside UIElement', () => {
  126. const uiElement = new UIElement( 'span' );
  127. const attributeElement = new AttributeElement( 'b' );
  128. new ContainerElement( 'p', null, [ uiElement, attributeElement ] ); // eslint-disable-line no-new
  129. const range = Range._createFromParentsAndOffsets( uiElement, 0, attributeElement, 0 );
  130. expectToThrowCKEditorError( () => {
  131. writer.remove( range );
  132. }, 'view-writer-cannot-break-ui-element', document );
  133. } );
  134. it( 'should remove single text node (as item)', () => {
  135. test( '<container:p>[foobar]</container:p>', '<container:p></container:p>', 'foobar', true );
  136. } );
  137. it( 'should not leave empty text nodes (as item)', () => {
  138. test( '<container:p>{foobar}</container:p>', '<container:p></container:p>', 'foobar', true );
  139. } );
  140. it( 'should remove part of the text node (as item)', () => {
  141. test( '<container:p>f{oob}ar</container:p>', '<container:p>far</container:p>', 'oob', true );
  142. } );
  143. it( 'should merge after removing (as item)', () => {
  144. test(
  145. '<container:p>' +
  146. '<attribute:b view-priority="1">foo</attribute:b>[bar]<attribute:b view-priority="1">bazqux</attribute:b>' +
  147. '</container:p>',
  148. '<container:p><attribute:b view-priority="1">foobazqux</attribute:b></container:p>',
  149. 'bar',
  150. true
  151. );
  152. } );
  153. it( 'should remove EmptyElement (as item)', () => {
  154. test(
  155. '<container:p>foo[<empty:img></empty:img>]bar</container:p>',
  156. '<container:p>foobar</container:p>',
  157. '<empty:img></empty:img>',
  158. true
  159. );
  160. } );
  161. it( 'should remove UIElement (as item)', () => {
  162. test(
  163. '<container:p>foo[<ui:span></ui:span>]bar</container:p>',
  164. '<container:p>foobar</container:p>',
  165. '<ui:span></ui:span>',
  166. true
  167. );
  168. } );
  169. it( 'should throw when item has no parent container', () => {
  170. const el = new AttributeElement( 'b' );
  171. expectToThrowCKEditorError( () => {
  172. writer.remove( el );
  173. }, 'view-position-before-root' );
  174. } );
  175. } );
  176. } );