8
0

remove.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  14. import Document from '../../../src/view/document';
  15. describe( 'DowncastWriter', () => {
  16. describe( 'remove()', () => {
  17. let writer;
  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. before( () => {
  34. writer = new DowncastWriter( new Document() );
  35. } );
  36. it( 'should throw when range placed in two containers', () => {
  37. const p1 = new ContainerElement( 'p' );
  38. const p2 = new ContainerElement( 'p' );
  39. expect( () => {
  40. writer.remove( Range._createFromParentsAndOffsets( p1, 0, p2, 0 ) );
  41. } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
  42. } );
  43. it( 'should throw when range has no parent container', () => {
  44. const el = new AttributeElement( 'b' );
  45. expect( () => {
  46. writer.remove( Range._createFromParentsAndOffsets( el, 0, el, 0 ) );
  47. } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
  48. } );
  49. it( 'should return empty DocumentFragment when range is collapsed', () => {
  50. const p = new ContainerElement( 'p' );
  51. const range = Range._createFromParentsAndOffsets( p, 0, p, 0 );
  52. const fragment = writer.remove( range );
  53. expect( fragment ).to.be.instanceof( DocumentFragment );
  54. expect( fragment.childCount ).to.equal( 0 );
  55. expect( range.isCollapsed ).to.be.true;
  56. } );
  57. it( 'should remove single text node', () => {
  58. test( '<container:p>[foobar]</container:p>', '<container:p>[]</container:p>', 'foobar' );
  59. } );
  60. it( 'should not leave empty text nodes', () => {
  61. test( '<container:p>{foobar}</container:p>', '<container:p>[]</container:p>', 'foobar' );
  62. } );
  63. it( 'should remove part of the text node', () => {
  64. test( '<container:p>f{oob}ar</container:p>', '<container:p>f{}ar</container:p>', 'oob' );
  65. } );
  66. it( 'should remove parts of nodes #1', () => {
  67. test(
  68. '<container:p>f{oo<attribute:b view-priority="10">ba}r</attribute:b></container:p>',
  69. '<container:p>f[]<attribute:b view-priority="10">r</attribute:b></container:p>',
  70. 'oo<attribute:b view-priority="10">ba</attribute:b>'
  71. );
  72. } );
  73. it( 'should support unicode', () => {
  74. test(
  75. '<container:p>நி{லை<attribute:b view-priority="10">க்}கு</attribute:b></container:p>',
  76. '<container:p>நி[]<attribute:b view-priority="10">கு</attribute:b></container:p>',
  77. 'லை<attribute:b view-priority="10">க்</attribute:b>'
  78. );
  79. } );
  80. it( 'should merge after removing #1', () => {
  81. test(
  82. '<container:p>' +
  83. '<attribute:b view-priority="1">foo</attribute:b>[bar]<attribute:b view-priority="1">bazqux</attribute:b>' +
  84. '</container:p>',
  85. '<container:p><attribute:b view-priority="1">foo{}bazqux</attribute:b></container:p>',
  86. 'bar'
  87. );
  88. } );
  89. it( 'should merge after removing #2', () => {
  90. test(
  91. '<container:p>' +
  92. '<attribute:b view-priority="1">fo{o</attribute:b>bar<attribute:b view-priority="1">ba}zqux</attribute:b>' +
  93. '</container:p>',
  94. '<container:p><attribute:b view-priority="1">fo{}zqux</attribute:b></container:p>',
  95. '<attribute:b view-priority="1">o</attribute:b>bar<attribute:b view-priority="1">ba</attribute:b>'
  96. );
  97. } );
  98. it( 'should remove part of the text node in document fragment', () => {
  99. test( 'fo{ob}ar', 'fo{}ar', 'ob' );
  100. } );
  101. it( 'should remove EmptyElement', () => {
  102. test(
  103. '<container:p>foo[<empty:img></empty:img>]bar</container:p>',
  104. '<container:p>foo{}bar</container:p>',
  105. '<empty:img></empty:img>'
  106. );
  107. } );
  108. it( 'should throw if range is placed inside EmptyElement', () => {
  109. const emptyElement = new EmptyElement( 'img' );
  110. const attributeElement = new AttributeElement( 'b' );
  111. new ContainerElement( 'p', null, [ emptyElement, attributeElement ] ); // eslint-disable-line no-new
  112. const range = Range._createFromParentsAndOffsets( emptyElement, 0, attributeElement, 0 );
  113. expect( () => {
  114. writer.remove( range );
  115. } ).to.throw( CKEditorError, 'view-writer-cannot-break-empty-element' );
  116. } );
  117. it( 'should remove UIElement', () => {
  118. test(
  119. '<container:p>foo[<ui:span></ui:span>]bar</container:p>',
  120. '<container:p>foo{}bar</container:p>',
  121. '<ui:span></ui:span>'
  122. );
  123. } );
  124. it( 'should throw if range is placed inside UIElement', () => {
  125. const uiElement = new UIElement( 'span' );
  126. const attributeElement = new AttributeElement( 'b' );
  127. new ContainerElement( 'p', null, [ uiElement, attributeElement ] ); // eslint-disable-line no-new
  128. const range = Range._createFromParentsAndOffsets( uiElement, 0, attributeElement, 0 );
  129. expect( () => {
  130. writer.remove( range );
  131. } ).to.throw( CKEditorError, 'view-writer-cannot-break-ui-element' );
  132. } );
  133. it( 'should remove single text node (as item)', () => {
  134. test( '<container:p>[foobar]</container:p>', '<container:p></container:p>', 'foobar', true );
  135. } );
  136. it( 'should not leave empty text nodes (as item)', () => {
  137. test( '<container:p>{foobar}</container:p>', '<container:p></container:p>', 'foobar', true );
  138. } );
  139. it( 'should remove part of the text node (as item)', () => {
  140. test( '<container:p>f{oob}ar</container:p>', '<container:p>far</container:p>', 'oob', true );
  141. } );
  142. it( 'should merge after removing (as item)', () => {
  143. test(
  144. '<container:p>' +
  145. '<attribute:b view-priority="1">foo</attribute:b>[bar]<attribute:b view-priority="1">bazqux</attribute:b>' +
  146. '</container:p>',
  147. '<container:p><attribute:b view-priority="1">foobazqux</attribute:b></container:p>',
  148. 'bar',
  149. true
  150. );
  151. } );
  152. it( 'should remove EmptyElement (as item)', () => {
  153. test(
  154. '<container:p>foo[<empty:img></empty:img>]bar</container:p>',
  155. '<container:p>foobar</container:p>',
  156. '<empty:img></empty:img>',
  157. true
  158. );
  159. } );
  160. it( 'should remove UIElement (as item)', () => {
  161. test(
  162. '<container:p>foo[<ui:span></ui:span>]bar</container:p>',
  163. '<container:p>foobar</container:p>',
  164. '<ui:span></ui:span>',
  165. true
  166. );
  167. } );
  168. it( 'should throw when item has no parent container', () => {
  169. const el = new AttributeElement( 'b' );
  170. expect( () => {
  171. writer.remove( el );
  172. } ).to.throw( CKEditorError, 'view-position-before-root' );
  173. } );
  174. } );
  175. } );