remove.js 4.8 KB

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