remove.js 7.8 KB

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