mergedelta.js 2.3 KB

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