mergedelta.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 Document from '/ckeditor5/core/treemodel/document.js';
  8. import Position from '/ckeditor5/core/treemodel/position.js';
  9. import Element from '/ckeditor5/core/treemodel/element.js';
  10. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  11. describe( 'Batch', () => {
  12. let doc, root, p1, p2;
  13. beforeEach( () => {
  14. doc = new Document();
  15. root = doc.createRoot( 'root' );
  16. p1 = new Element( 'p', { key1: 'value1' }, 'foo' );
  17. p2 = new Element( 'p', { key2: 'value2' }, 'bar' );
  18. root.insertChildren( 0, [ p1, p2 ] );
  19. } );
  20. describe( 'merge', () => {
  21. it( 'should merge foo and bar into foobar', () => {
  22. doc.batch().merge( new Position( root, [ 1 ] ) );
  23. expect( root.getChildCount() ).to.equal( 1 );
  24. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  25. expect( root.getChild( 0 ).getChildCount() ).to.equal( 6 );
  26. expect( root.getChild( 0 )._attrs.size ).to.equal( 1 );
  27. expect( root.getChild( 0 ).getAttribute( 'key1' ) ).to.equal( 'value1' );
  28. expect( root.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  29. expect( root.getChild( 0 ).getChild( 1 ).character ).to.equal( 'o' );
  30. expect( root.getChild( 0 ).getChild( 2 ).character ).to.equal( 'o' );
  31. expect( root.getChild( 0 ).getChild( 3 ).character ).to.equal( 'b' );
  32. expect( root.getChild( 0 ).getChild( 4 ).character ).to.equal( 'a' );
  33. expect( root.getChild( 0 ).getChild( 5 ).character ).to.equal( 'r' );
  34. } );
  35. it( 'should throw if there is no element after', () => {
  36. expect( () => {
  37. doc.batch().merge( new Position( root, [ 2 ] ) );
  38. } ).to.throw( CKEditorError, /^batch-merge-no-element-after/ );
  39. } );
  40. it( 'should throw if there is no element before', () => {
  41. expect( () => {
  42. doc.batch().merge( new Position( root, [ 0, 2 ] ) );
  43. } ).to.throw( CKEditorError, /^batch-merge-no-element-before/ );
  44. } );
  45. it( 'should be chainable', () => {
  46. const batch = doc.batch();
  47. const chain = batch.merge( new Position( root, [ 1 ] ) );
  48. expect( chain ).to.equal( batch );
  49. } );
  50. } );
  51. } );