remove.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import Writer from '/ckeditor5/engine/treeview/writer.js';
  8. import ContainerElement from '/ckeditor5/engine/treeview/containerelement.js';
  9. import Range from '/ckeditor5/engine/treeview/range.js';
  10. import DocumentFragment from '/ckeditor5/engine/treeview/documentfragment.js';
  11. import { stringify, parse } from '/tests/engine/_utils/view.js';
  12. describe( 'Writer', () => {
  13. let 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. const { view, selection } = parse( input );
  24. const range = selection.getFirstRange();
  25. const removed = writer.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. beforeEach( () => {
  30. writer = new Writer();
  31. } );
  32. describe( 'remove', () => {
  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( 'treeview-writer-invalid-range-container' );
  39. } );
  40. it( 'should return empty DocumentFragment when range is collapsed', () => {
  41. const p = new ContainerElement( 'p' );
  42. const range = Range.createFromParentsAndOffsets( p, 0, p, 0 );
  43. const fragment = writer.remove( range );
  44. expect( fragment ).to.be.instanceof( DocumentFragment );
  45. expect( fragment.getChildCount() ).to.equal( 0 );
  46. expect( range.isCollapsed ).to.be.true;
  47. } );
  48. it( 'should remove single text node', () => {
  49. test( '<container:p>[foobar]</container:p>', '<container:p>[]</container:p>', 'foobar' );
  50. } );
  51. it( 'should not leave empty text nodes', () => {
  52. test( '<container:p>{foobar}</container:p>', '<container:p>[]</container:p>', 'foobar' );
  53. } );
  54. it( 'should remove part of the text node', () => {
  55. test( '<container:p>f{oob}ar</container:p>', '<container:p>f{}ar</container:p>', 'oob' );
  56. } );
  57. it( 'should remove parts of nodes', () => {
  58. test(
  59. '<container:p>f{oo<attribute:b:10>ba}r</attribute:b:10></container:p>',
  60. '<container:p>f[]<attribute:b:10>r</attribute:b:10></container:p>',
  61. 'oo<attribute:b:10>ba</attribute:b:10>'
  62. );
  63. } );
  64. it( 'should merge after removing #1', () => {
  65. test(
  66. '<container:p><attribute:b:1>foo</attribute:b:1>[bar]<attribute:b:1>bazqux</attribute:b:1></container:p>',
  67. '<container:p><attribute:b:1>foo{}bazqux</attribute:b:1></container:p>',
  68. 'bar'
  69. );
  70. } );
  71. it( 'should merge after removing #2', () => {
  72. test(
  73. '<container:p><attribute:b:1>fo{o</attribute:b:1>bar<attribute:b:1>ba}zqux</attribute:b:1></container:p>',
  74. '<container:p><attribute:b:1>fo{}zqux</attribute:b:1></container:p>',
  75. '<attribute:b:1>o</attribute:b:1>bar<attribute:b:1>ba</attribute:b:1>'
  76. );
  77. } );
  78. } );
  79. } );