splitdelta.js 3.3 KB

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