remove.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 { remove } 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. /**
  16. * Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
  17. * test ranges.
  18. *
  19. * @param {String} input
  20. * @param {String} expectedResult
  21. * @param {String} expectedRemoved
  22. */
  23. function test( input, expectedResult, expectedRemoved ) {
  24. let { view, selection } = parse( input );
  25. if ( view instanceof AttributeElement || view instanceof Text ) {
  26. view = new DocumentFragment( view );
  27. }
  28. const range = selection.getFirstRange();
  29. const removed = remove( range );
  30. expect( stringify( view, range, { showType: true, showPriority: true } ) ).to.equal( expectedResult );
  31. expect( stringify( removed, null, { showType: true, showPriority: true } ) ).to.equal( expectedRemoved );
  32. }
  33. describe( 'remove', () => {
  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. remove( Range.createFromParentsAndOffsets( p1, 0, p2, 0 ) );
  39. } ).to.throw( 'view-writer-invalid-range-container' );
  40. } );
  41. it( 'should return empty DocumentFragment when range is collapsed', () => {
  42. const p = new ContainerElement( 'p' );
  43. const range = Range.createFromParentsAndOffsets( p, 0, p, 0 );
  44. const fragment = remove( range );
  45. expect( fragment ).to.be.instanceof( DocumentFragment );
  46. expect( fragment.getChildCount() ).to.equal( 0 );
  47. expect( range.isCollapsed ).to.be.true;
  48. } );
  49. it( 'should remove single text node', () => {
  50. test( '<container:p>[foobar]</container:p>', '<container:p>[]</container:p>', 'foobar' );
  51. } );
  52. it( 'should not leave empty text nodes', () => {
  53. test( '<container:p>{foobar}</container:p>', '<container:p>[]</container:p>', 'foobar' );
  54. } );
  55. it( 'should remove part of the text node', () => {
  56. test( '<container:p>f{oob}ar</container:p>', '<container:p>f{}ar</container:p>', 'oob' );
  57. } );
  58. it( 'should remove parts of nodes', () => {
  59. test(
  60. '<container:p>f{oo<attribute:b:10>ba}r</attribute:b:10></container:p>',
  61. '<container:p>f[]<attribute:b:10>r</attribute:b:10></container:p>',
  62. 'oo<attribute:b:10>ba</attribute:b:10>'
  63. );
  64. } );
  65. it( 'should merge after removing #1', () => {
  66. test(
  67. '<container:p><attribute:b:1>foo</attribute:b:1>[bar]<attribute:b:1>bazqux</attribute:b:1></container:p>',
  68. '<container:p><attribute:b:1>foo{}bazqux</attribute:b:1></container:p>',
  69. 'bar'
  70. );
  71. } );
  72. it( 'should merge after removing #2', () => {
  73. test(
  74. '<container:p><attribute:b:1>fo{o</attribute:b:1>bar<attribute:b:1>ba}zqux</attribute:b:1></container:p>',
  75. '<container:p><attribute:b:1>fo{}zqux</attribute:b:1></container:p>',
  76. '<attribute:b:1>o</attribute:b:1>bar<attribute:b:1>ba</attribute:b:1>'
  77. );
  78. } );
  79. it( 'should remove part of the text node in document fragment', () => {
  80. test( 'fo{ob}ar', 'fo{}ar', 'ob' );
  81. } );
  82. } );
  83. } );