8
0

remove.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { remove } from '../../../src/view/writer';
  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. describe( 'writer', () => {
  15. /**
  16. * Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
  17. * test ranges.
  18. *
  19. * @param {String} input
  20. * @param {String} expectedResult
  21. * @param {String} expectedRemoved
  22. */
  23. function test( input, expectedResult, expectedRemoved ) {
  24. const { view, selection } = parse( input );
  25. const range = selection.getFirstRange();
  26. const removed = remove( range );
  27. expect( stringify( view, range, { showType: true, showPriority: true } ) ).to.equal( expectedResult );
  28. expect( stringify( removed, null, { showType: true, showPriority: true } ) ).to.equal( expectedRemoved );
  29. }
  30. describe( 'remove', () => {
  31. it( 'should throw when range placed in two containers', () => {
  32. const p1 = new ContainerElement( 'p' );
  33. const p2 = new ContainerElement( 'p' );
  34. expect( () => {
  35. remove( Range.createFromParentsAndOffsets( p1, 0, p2, 0 ) );
  36. } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
  37. } );
  38. it( 'should throw when range has no parent container', () => {
  39. const el = new AttributeElement( 'b' );
  40. expect( () => {
  41. remove( Range.createFromParentsAndOffsets( el, 0, el, 0 ) );
  42. } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
  43. } );
  44. it( 'should return empty DocumentFragment when range is collapsed', () => {
  45. const p = new ContainerElement( 'p' );
  46. const range = Range.createFromParentsAndOffsets( p, 0, p, 0 );
  47. const fragment = remove( range );
  48. expect( fragment ).to.be.instanceof( DocumentFragment );
  49. expect( fragment.childCount ).to.equal( 0 );
  50. expect( range.isCollapsed ).to.be.true;
  51. } );
  52. it( 'should remove single text node', () => {
  53. test( '<container:p>[foobar]</container:p>', '<container:p>[]</container:p>', 'foobar' );
  54. } );
  55. it( 'should not leave empty text nodes', () => {
  56. test( '<container:p>{foobar}</container:p>', '<container:p>[]</container:p>', 'foobar' );
  57. } );
  58. it( 'should remove part of the text node', () => {
  59. test( '<container:p>f{oob}ar</container:p>', '<container:p>f{}ar</container:p>', 'oob' );
  60. } );
  61. it( 'should remove parts of nodes #1', () => {
  62. test(
  63. '<container:p>f{oo<attribute:b view-priority="10">ba}r</attribute:b></container:p>',
  64. '<container:p>f[]<attribute:b view-priority="10">r</attribute:b></container:p>',
  65. 'oo<attribute:b view-priority="10">ba</attribute:b>'
  66. );
  67. } );
  68. it( 'should support unicode', () => {
  69. test(
  70. '<container:p>நி{லை<attribute:b view-priority="10">க்}கு</attribute:b></container:p>',
  71. '<container:p>நி[]<attribute:b view-priority="10">கு</attribute:b></container:p>',
  72. 'லை<attribute:b view-priority="10">க்</attribute:b>'
  73. );
  74. } );
  75. it( 'should merge after removing #1', () => {
  76. test(
  77. '<container:p>' +
  78. '<attribute:b view-priority="1">foo</attribute:b>[bar]<attribute:b view-priority="1">bazqux</attribute:b>' +
  79. '</container:p>',
  80. '<container:p><attribute:b view-priority="1">foo{}bazqux</attribute:b></container:p>',
  81. 'bar'
  82. );
  83. } );
  84. it( 'should merge after removing #2', () => {
  85. test(
  86. '<container:p>' +
  87. '<attribute:b view-priority="1">fo{o</attribute:b>bar<attribute:b view-priority="1">ba}zqux</attribute:b>' +
  88. '</container:p>',
  89. '<container:p><attribute:b view-priority="1">fo{}zqux</attribute:b></container:p>',
  90. '<attribute:b view-priority="1">o</attribute:b>bar<attribute:b view-priority="1">ba</attribute:b>'
  91. );
  92. } );
  93. it( 'should remove part of the text node in document fragment', () => {
  94. test( 'fo{ob}ar', 'fo{}ar', 'ob' );
  95. } );
  96. it( 'should remove EmptyElement', () => {
  97. test(
  98. '<container:p>foo[<empty:img></empty:img>]bar</container:p>',
  99. '<container:p>foo{}bar</container:p>',
  100. '<empty:img></empty:img>'
  101. );
  102. } );
  103. it( 'should throw if range is placed inside EmptyElement', () => {
  104. const emptyElement = new EmptyElement( 'img' );
  105. const attributeElement = new AttributeElement( 'b' );
  106. new ContainerElement( 'p', null, [ emptyElement, attributeElement ] ); // eslint-disable-line no-new
  107. const range = Range.createFromParentsAndOffsets( emptyElement, 0, attributeElement, 0 );
  108. expect( () => {
  109. remove( range );
  110. } ).to.throw( CKEditorError, 'view-writer-cannot-break-empty-element' );
  111. } );
  112. it( 'should remove UIElement', () => {
  113. test(
  114. '<container:p>foo[<ui:span></ui:span>]bar</container:p>',
  115. '<container:p>foo{}bar</container:p>',
  116. '<ui:span></ui:span>'
  117. );
  118. } );
  119. it( 'should throw if range is placed inside UIElement', () => {
  120. const uiElement = new UIElement( 'span' );
  121. const attributeElement = new AttributeElement( 'b' );
  122. new ContainerElement( 'p', null, [ uiElement, attributeElement ] ); // eslint-disable-line no-new
  123. const range = Range.createFromParentsAndOffsets( uiElement, 0, attributeElement, 0 );
  124. expect( () => {
  125. remove( range );
  126. } ).to.throw( CKEditorError, 'view-writer-cannot-break-ui-element' );
  127. } );
  128. } );
  129. } );