splitdelta.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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, p;
  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. p = new Element( 'p', [ new Attribute( 'key', 'value' ) ], 'foobar' );
  27. root.insertChildren( 0, p );
  28. } );
  29. describe( 'split', () => {
  30. it( 'should split foobar to foo and bar', () => {
  31. doc.makeTransaction().split( new Position( [ 0, 3 ], root ) );
  32. expect( root.getChildCount() ).to.equal( 2 );
  33. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  34. expect( root.getChild( 0 ).getChildCount() ).to.equal( 3 );
  35. expect( root.getChild( 0 )._getAttrCount() ).to.equal( 1 );
  36. expect( root.getChild( 0 ).getAttr( 'key' ) ).to.equal( 'value' );
  37. expect( root.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  38. expect( root.getChild( 0 ).getChild( 1 ).character ).to.equal( 'o' );
  39. expect( root.getChild( 0 ).getChild( 2 ).character ).to.equal( 'o' );
  40. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  41. expect( root.getChild( 1 ).getChildCount() ).to.equal( 3 );
  42. expect( root.getChild( 1 )._getAttrCount() ).to.equal( 1 );
  43. expect( root.getChild( 1 ).getAttr( 'key' ) ).to.equal( 'value' );
  44. expect( root.getChild( 1 ).getChild( 0 ).character ).to.equal( 'b' );
  45. expect( root.getChild( 1 ).getChild( 1 ).character ).to.equal( 'a' );
  46. expect( root.getChild( 1 ).getChild( 2 ).character ).to.equal( 'r' );
  47. } );
  48. it( 'should create an empty paragraph if we split at the end', () => {
  49. doc.makeTransaction().split( new Position( [ 0, 6 ], root ) );
  50. expect( root.getChildCount() ).to.equal( 2 );
  51. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  52. expect( root.getChild( 0 ).getChildCount() ).to.equal( 6 );
  53. expect( root.getChild( 0 )._getAttrCount() ).to.equal( 1 );
  54. expect( root.getChild( 0 ).getAttr( 'key' ) ).to.equal( 'value' );
  55. expect( root.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  56. expect( root.getChild( 0 ).getChild( 1 ).character ).to.equal( 'o' );
  57. expect( root.getChild( 0 ).getChild( 2 ).character ).to.equal( 'o' );
  58. expect( root.getChild( 0 ).getChild( 3 ).character ).to.equal( 'b' );
  59. expect( root.getChild( 0 ).getChild( 4 ).character ).to.equal( 'a' );
  60. expect( root.getChild( 0 ).getChild( 5 ).character ).to.equal( 'r' );
  61. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  62. expect( root.getChild( 1 ).getChildCount() ).to.equal( 0 );
  63. expect( root.getChild( 1 )._getAttrCount() ).to.equal( 1 );
  64. expect( root.getChild( 1 ).getAttr( 'key' ) ).to.equal( 'value' );
  65. } );
  66. it( 'should throw if we try to split a root', () => {
  67. expect( () => {
  68. doc.makeTransaction().split( new Position( [ 0 ], root ) );
  69. } ).to.throw( CKEditorError, /^transaction-split-root/ );
  70. } );
  71. } );
  72. } );