remove.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. 'use strict';
  7. import Writer from '/ckeditor5/engine/view/writer.js';
  8. import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
  9. import Range from '/ckeditor5/engine/view/range.js';
  10. import DocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
  11. import { stringify, parse } from '/tests/engine/_utils/view.js';
  12. import AttributeElement from '/ckeditor5/engine/view/attributeelement.js';
  13. import Text from '/ckeditor5/engine/view/text.js';
  14. describe( 'Writer', () => {
  15. let writer;
  16. /**
  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. */
  24. function test( input, expectedResult, expectedRemoved ) {
  25. let { view, selection } = parse( input );
  26. if ( view instanceof AttributeElement || view instanceof Text ) {
  27. view = new DocumentFragment( view );
  28. }
  29. const range = selection.getFirstRange();
  30. const removed = writer.remove( range );
  31. expect( stringify( view, range, { showType: true, showPriority: true } ) ).to.equal( expectedResult );
  32. expect( stringify( removed, null, { showType: true, showPriority: true } ) ).to.equal( expectedRemoved );
  33. }
  34. beforeEach( () => {
  35. writer = new Writer();
  36. } );
  37. describe( 'remove', () => {
  38. it( 'should throw when range placed in two containers', () => {
  39. const p1 = new ContainerElement( 'p' );
  40. const p2 = new ContainerElement( 'p' );
  41. expect( () => {
  42. writer.remove( Range.createFromParentsAndOffsets( p1, 0, p2, 0 ) );
  43. } ).to.throw( 'view-writer-invalid-range-container' );
  44. } );
  45. it( 'should return empty DocumentFragment when range is collapsed', () => {
  46. const p = new ContainerElement( 'p' );
  47. const range = Range.createFromParentsAndOffsets( p, 0, p, 0 );
  48. const fragment = writer.remove( range );
  49. expect( fragment ).to.be.instanceof( DocumentFragment );
  50. expect( fragment.getChildCount() ).to.equal( 0 );
  51. expect( range.isCollapsed ).to.be.true;
  52. } );
  53. it( 'should remove single text node', () => {
  54. test( '<container:p>[foobar]</container:p>', '<container:p>[]</container:p>', 'foobar' );
  55. } );
  56. it( 'should not leave empty text nodes', () => {
  57. test( '<container:p>{foobar}</container:p>', '<container:p>[]</container:p>', 'foobar' );
  58. } );
  59. it( 'should remove part of the text node', () => {
  60. test( '<container:p>f{oob}ar</container:p>', '<container:p>f{}ar</container:p>', 'oob' );
  61. } );
  62. it( 'should remove parts of nodes', () => {
  63. test(
  64. '<container:p>f{oo<attribute:b:10>ba}r</attribute:b:10></container:p>',
  65. '<container:p>f[]<attribute:b:10>r</attribute:b:10></container:p>',
  66. 'oo<attribute:b:10>ba</attribute:b:10>'
  67. );
  68. } );
  69. it( 'should merge after removing #1', () => {
  70. test(
  71. '<container:p><attribute:b:1>foo</attribute:b:1>[bar]<attribute:b:1>bazqux</attribute:b:1></container:p>',
  72. '<container:p><attribute:b:1>foo{}bazqux</attribute:b:1></container:p>',
  73. 'bar'
  74. );
  75. } );
  76. it( 'should merge after removing #2', () => {
  77. test(
  78. '<container:p><attribute:b:1>fo{o</attribute:b:1>bar<attribute:b:1>ba}zqux</attribute:b:1></container:p>',
  79. '<container:p><attribute:b:1>fo{}zqux</attribute:b:1></container:p>',
  80. '<attribute:b:1>o</attribute:b:1>bar<attribute:b:1>ba</attribute:b:1>'
  81. );
  82. } );
  83. it( 'should remove part of the text node in document fragment', () => {
  84. test( 'fo{ob}ar', 'fo{}ar', 'ob' );
  85. } );
  86. } );
  87. } );