8
0

remove.js 5.7 KB

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