splitdelta.js 3.2 KB

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