8
0

remove.js 5.8 KB

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