insertdelta.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 Element from '/ckeditor5/core/treemodel/element.js';
  9. import Position from '/ckeditor5/core/treemodel/position.js';
  10. import InsertOperation from '/ckeditor5/core/treemodel/operation/insertoperation.js';
  11. import InsertDelta from '/ckeditor5/core/treemodel/delta/insertdelta.js';
  12. import RemoveDelta from '/ckeditor5/core/treemodel/delta/removedelta.js';
  13. import RemoveOperation from '/ckeditor5/core/treemodel/operation/removeoperation.js';
  14. describe( 'Batch', () => {
  15. let doc, root, batch, p, ul, chain;
  16. beforeEach( () => {
  17. doc = new Document();
  18. root = doc.createRoot( 'root' );
  19. root.insertChildren( 0, 'abc' );
  20. batch = doc.batch();
  21. p = new Element( 'p' );
  22. ul = new Element( 'ul' );
  23. chain = batch.insert( new Position( root, [ 2 ] ), [ p, ul ] );
  24. } );
  25. describe( 'insert', () => {
  26. it( 'should insert given nodes at given position', () => {
  27. expect( root.getChildCount() ).to.equal( 5 );
  28. expect( root.getChild( 2 ) ).to.equal( p );
  29. expect( root.getChild( 3 ) ).to.equal( ul );
  30. } );
  31. it( 'should be chainable', () => {
  32. expect( chain ).to.equal( batch );
  33. } );
  34. } );
  35. } );
  36. describe( 'InsertDelta', () => {
  37. let insertDelta, doc, root;
  38. beforeEach( () => {
  39. doc = new Document();
  40. root = doc.createRoot( 'root' );
  41. insertDelta = new InsertDelta();
  42. } );
  43. describe( 'constructor', () => {
  44. it( 'should create insert delta with no operations added', () => {
  45. expect( insertDelta.operations.length ).to.equal( 0 );
  46. } );
  47. } );
  48. describe( 'position', () => {
  49. it( 'should be null if there are no operations in delta', () => {
  50. expect( insertDelta.position ).to.be.null;
  51. } );
  52. it( 'should be equal to the position where node is inserted', () => {
  53. insertDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2, 3 ] ), new Element( 'x' ), 0 ) );
  54. expect( insertDelta.position.root ).to.equal( root );
  55. expect( insertDelta.position.path ).to.deep.equal( [ 1, 2, 3 ] );
  56. } );
  57. } );
  58. describe( 'nodeList', () => {
  59. it( 'should be null if there are no operations in delta', () => {
  60. expect( insertDelta.nodeList ).to.be.null;
  61. } );
  62. it( 'should be equal to the node list inserted by the delta', () => {
  63. let elementX = new Element( 'x' );
  64. insertDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2, 3 ] ), elementX, 0 ) );
  65. expect( insertDelta.nodeList.length ).to.equal( 1 );
  66. expect( insertDelta.nodeList.get( 0 ) ).to.equal( elementX );
  67. } );
  68. } );
  69. describe( 'getReversed', () => {
  70. it( 'should return empty RemoveDelta if there are no operations in delta', () => {
  71. let reversed = insertDelta.getReversed();
  72. expect( reversed ).to.be.instanceof( RemoveDelta );
  73. expect( reversed.operations.length ).to.equal( 0 );
  74. } );
  75. it( 'should return correct RemoveDelta', () => {
  76. let position = new Position( root, [ 1, 2, 3 ] );
  77. let elementX = new Element( 'x' );
  78. insertDelta.operations.push( new InsertOperation( position, elementX, 0 ) );
  79. let reversed = insertDelta.getReversed();
  80. expect( reversed ).to.be.instanceof( RemoveDelta );
  81. expect( reversed.operations.length ).to.equal( 1 );
  82. expect( reversed.operations[ 0 ] ).to.be.instanceof( RemoveOperation );
  83. expect( reversed.operations[ 0 ].sourcePosition.isEqual( position ) ).to.be.true;
  84. expect( reversed.operations[ 0 ].howMany ).to.equal( 1 );
  85. } );
  86. } );
  87. } );