splitdelta.js 3.6 KB

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