8
0

remove.js 8.8 KB

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