mergedelta.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /* bender-include: ../../_tools/tools.js */
  7. 'use strict';
  8. const getIteratorCount = bender.tools.core.getIteratorCount;
  9. const modules = bender.amd.require(
  10. 'document/document',
  11. 'document/position',
  12. 'document/element',
  13. 'document/attribute',
  14. 'ckeditorerror' );
  15. describe( 'Transaction', () => {
  16. let Document, Position, Element, Attribute, CKEditorError;
  17. let doc, root, p1, p2;
  18. before( () => {
  19. Document = modules[ 'document/document' ];
  20. Position = modules[ 'document/position' ];
  21. Element = modules[ 'document/element' ];
  22. Attribute = modules[ 'document/attribute' ];
  23. CKEditorError = modules.ckeditorerror;
  24. } );
  25. beforeEach( () => {
  26. doc = new Document();
  27. root = doc.createRoot( 'root' );
  28. p1 = new Element( 'p', [ new Attribute( 'key1', 'value1' ) ], 'foo' );
  29. p2 = new Element( 'p', [ new Attribute( 'key2', 'value2' ) ], 'bar' );
  30. root.insertChildren( 0, [ p1, p2 ] );
  31. } );
  32. describe( 'merge', () => {
  33. it( 'should merge foo and bar into foobar', () => {
  34. doc.createTransaction().merge( new Position( [ 1 ], root ) );
  35. expect( root.getChildCount() ).to.equal( 1 );
  36. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  37. expect( root.getChild( 0 ).getChildCount() ).to.equal( 6 );
  38. expect( getIteratorCount( root.getChild( 0 ).getAttrIterator() ) ).to.equal( 1 );
  39. expect( root.getChild( 0 ).getAttr( 'key1' ) ).to.equal( 'value1' );
  40. expect( root.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  41. expect( root.getChild( 0 ).getChild( 1 ).character ).to.equal( 'o' );
  42. expect( root.getChild( 0 ).getChild( 2 ).character ).to.equal( 'o' );
  43. expect( root.getChild( 0 ).getChild( 3 ).character ).to.equal( 'b' );
  44. expect( root.getChild( 0 ).getChild( 4 ).character ).to.equal( 'a' );
  45. expect( root.getChild( 0 ).getChild( 5 ).character ).to.equal( 'r' );
  46. } );
  47. it( 'should throw if there is no element after', () => {
  48. expect( () => {
  49. doc.createTransaction().merge( new Position( [ 2 ], root ) );
  50. } ).to.throw( CKEditorError, /^transaction-merge-no-element-after/ );
  51. } );
  52. it( 'should throw if there is no element before', () => {
  53. expect( () => {
  54. doc.createTransaction().merge( new Position( [ 0, 2 ], root ) );
  55. } ).to.throw( CKEditorError, /^transaction-merge-no-element-before/ );
  56. } );
  57. it( 'should be chainable', () => {
  58. const transaction = doc.createTransaction();
  59. const chain = transaction.merge( new Position( [ 1 ], root ) );
  60. expect( chain ).to.equal( transaction );
  61. } );
  62. } );
  63. } );