mergedelta.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, delta */
  6. 'use strict';
  7. import coreTestUtils from '/tests/core/_utils/utils.js';
  8. import Document from '/ckeditor5/core/treemodel/document.js';
  9. import Position from '/ckeditor5/core/treemodel/position.js';
  10. import Element from '/ckeditor5/core/treemodel/element.js';
  11. import Attribute from '/ckeditor5/core/treemodel/attribute.js';
  12. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  13. const getIteratorCount = coreTestUtils.getIteratorCount;
  14. describe( 'Batch', () => {
  15. let doc, root, p1, p2;
  16. beforeEach( () => {
  17. doc = new Document();
  18. root = doc.createRoot( 'root' );
  19. p1 = new Element( 'p', [ new Attribute( 'key1', 'value1' ) ], 'foo' );
  20. p2 = new Element( 'p', [ new Attribute( 'key2', 'value2' ) ], 'bar' );
  21. root.insertChildren( 0, [ p1, p2 ] );
  22. } );
  23. describe( 'merge', () => {
  24. it( 'should merge foo and bar into foobar', () => {
  25. doc.batch().merge( new Position( root, [ 1 ] ) );
  26. expect( root.getChildCount() ).to.equal( 1 );
  27. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  28. expect( root.getChild( 0 ).getChildCount() ).to.equal( 6 );
  29. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 1 );
  30. expect( root.getChild( 0 ).getAttr( 'key1' ) ).to.equal( 'value1' );
  31. expect( root.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  32. expect( root.getChild( 0 ).getChild( 1 ).character ).to.equal( 'o' );
  33. expect( root.getChild( 0 ).getChild( 2 ).character ).to.equal( 'o' );
  34. expect( root.getChild( 0 ).getChild( 3 ).character ).to.equal( 'b' );
  35. expect( root.getChild( 0 ).getChild( 4 ).character ).to.equal( 'a' );
  36. expect( root.getChild( 0 ).getChild( 5 ).character ).to.equal( 'r' );
  37. } );
  38. it( 'should throw if there is no element after', () => {
  39. expect( () => {
  40. doc.batch().merge( new Position( root, [ 2 ] ) );
  41. } ).to.throw( CKEditorError, /^batch-merge-no-element-after/ );
  42. } );
  43. it( 'should throw if there is no element before', () => {
  44. expect( () => {
  45. doc.batch().merge( new Position( root, [ 0, 2 ] ) );
  46. } ).to.throw( CKEditorError, /^batch-merge-no-element-before/ );
  47. } );
  48. it( 'should be chainable', () => {
  49. const batch = doc.batch();
  50. const chain = batch.merge( new Position( root, [ 1 ] ) );
  51. expect( chain ).to.equal( batch );
  52. } );
  53. } );
  54. } );