splitdelta.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 coreTestUtils from '/tests/core/_utils/utils.js';
  8. import Document from '/ckeditor5/core/treemodel/document.js';
  9. import Position from '/ckeditor5/core/treemodel/position.js';
  10. import Element from '/ckeditor5/core/treemodel/element.js';
  11. import Attribute from '/ckeditor5/core/treemodel/attribute.js';
  12. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  13. const getIteratorCount = coreTestUtils.getIteratorCount;
  14. describe( 'Batch', () => {
  15. let doc, root, p;
  16. beforeEach( () => {
  17. doc = new Document();
  18. root = doc.createRoot( 'root' );
  19. p = new Element( 'p', [ new Attribute( 'key', 'value' ) ], 'foobar' );
  20. root.insertChildren( 0, p );
  21. } );
  22. describe( 'split', () => {
  23. it( 'should split foobar to foo and bar', () => {
  24. doc.batch().split( new Position( root, [ 0, 3 ] ) );
  25. expect( root.getChildCount() ).to.equal( 2 );
  26. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  27. expect( root.getChild( 0 ).getChildCount() ).to.equal( 3 );
  28. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 1 );
  29. expect( root.getChild( 0 ).getAttr( 'key' ) ).to.equal( 'value' );
  30. expect( root.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  31. expect( root.getChild( 0 ).getChild( 1 ).character ).to.equal( 'o' );
  32. expect( root.getChild( 0 ).getChild( 2 ).character ).to.equal( 'o' );
  33. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  34. expect( root.getChild( 1 ).getChildCount() ).to.equal( 3 );
  35. expect( getIteratorCount( root.getChild( 1 ).getAttrs() ) ).to.equal( 1 );
  36. expect( root.getChild( 1 ).getAttr( 'key' ) ).to.equal( 'value' );
  37. expect( root.getChild( 1 ).getChild( 0 ).character ).to.equal( 'b' );
  38. expect( root.getChild( 1 ).getChild( 1 ).character ).to.equal( 'a' );
  39. expect( root.getChild( 1 ).getChild( 2 ).character ).to.equal( 'r' );
  40. } );
  41. it( 'should create an empty paragraph if we split at the end', () => {
  42. doc.batch().split( new Position( root, [ 0, 6 ] ) );
  43. expect( root.getChildCount() ).to.equal( 2 );
  44. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  45. expect( root.getChild( 0 ).getChildCount() ).to.equal( 6 );
  46. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 1 );
  47. expect( root.getChild( 0 ).getAttr( 'key' ) ).to.equal( 'value' );
  48. expect( root.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  49. expect( root.getChild( 0 ).getChild( 1 ).character ).to.equal( 'o' );
  50. expect( root.getChild( 0 ).getChild( 2 ).character ).to.equal( 'o' );
  51. expect( root.getChild( 0 ).getChild( 3 ).character ).to.equal( 'b' );
  52. expect( root.getChild( 0 ).getChild( 4 ).character ).to.equal( 'a' );
  53. expect( root.getChild( 0 ).getChild( 5 ).character ).to.equal( 'r' );
  54. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  55. expect( root.getChild( 1 ).getChildCount() ).to.equal( 0 );
  56. expect( getIteratorCount( root.getChild( 1 ).getAttrs() ) ).to.equal( 1 );
  57. expect( root.getChild( 1 ).getAttr( 'key' ) ).to.equal( 'value' );
  58. } );
  59. it( 'should throw if we try to split a root', () => {
  60. expect( () => {
  61. doc.batch().split( new Position( root, [ 0 ] ) );
  62. } ).to.throw( CKEditorError, /^batch-split-root/ );
  63. } );
  64. it( 'should be chainable', () => {
  65. const batch = doc.batch();
  66. const chain = batch.split( new Position( root, [ 0, 3 ] ) );
  67. expect( chain ).to.equal( batch );
  68. } );
  69. } );
  70. } );