8
0

remove.js 7.8 KB

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