remove.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view, browser-only */
  6. import { remove } from '/ckeditor5/engine/view/writer.js';
  7. import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
  8. import Range from '/ckeditor5/engine/view/range.js';
  9. import DocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
  10. import { stringify, parse } from '/tests/engine/_utils/view.js';
  11. import AttributeElement from '/ckeditor5/engine/view/attributeelement.js';
  12. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  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', () => {
  61. test(
  62. '<container:p>f{oo<attribute:b:10>ba}r</attribute:b:10></container:p>',
  63. '<container:p>f[]<attribute:b:10>r</attribute:b:10></container:p>',
  64. 'oo<attribute:b:10>ba</attribute:b:10>'
  65. );
  66. } );
  67. it( 'should support unicode', () => {
  68. test(
  69. '<container:p>நி{லை<attribute:b:10>க்}கு</attribute:b:10></container:p>',
  70. '<container:p>நி[]<attribute:b:10>கு</attribute:b:10></container:p>',
  71. 'லை<attribute:b:10>க்</attribute:b:10>'
  72. );
  73. } );
  74. it( 'should merge after removing #1', () => {
  75. test(
  76. '<container:p><attribute:b:1>foo</attribute:b:1>[bar]<attribute:b:1>bazqux</attribute:b:1></container:p>',
  77. '<container:p><attribute:b:1>foo{}bazqux</attribute:b:1></container:p>',
  78. 'bar'
  79. );
  80. } );
  81. it( 'should merge after removing #2', () => {
  82. test(
  83. '<container:p><attribute:b:1>fo{o</attribute:b:1>bar<attribute:b:1>ba}zqux</attribute:b:1></container:p>',
  84. '<container:p><attribute:b:1>fo{}zqux</attribute:b:1></container:p>',
  85. '<attribute:b:1>o</attribute:b:1>bar<attribute:b:1>ba</attribute:b:1>'
  86. );
  87. } );
  88. it( 'should remove part of the text node in document fragment', () => {
  89. test( 'fo{ob}ar', 'fo{}ar', 'ob' );
  90. } );
  91. } );
  92. } );