8
0

insertdelta.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/engine/treemodel/document.js';
  8. import Element from '/ckeditor5/engine/treemodel/element.js';
  9. import Position from '/ckeditor5/engine/treemodel/position.js';
  10. import InsertOperation from '/ckeditor5/engine/treemodel/operation/insertoperation.js';
  11. import InsertDelta from '/ckeditor5/engine/treemodel/delta/insertdelta.js';
  12. import Delta from '/ckeditor5/engine/treemodel/delta/delta.js';
  13. import RemoveDelta from '/ckeditor5/engine/treemodel/delta/removedelta.js';
  14. import RemoveOperation from '/ckeditor5/engine/treemodel/operation/removeoperation.js';
  15. import { jsonParseStringify } from '/tests/engine/treemodel/_utils/utils.js';
  16. describe( 'Batch', () => {
  17. let doc, root, batch, p, ul, chain;
  18. beforeEach( () => {
  19. doc = new Document();
  20. root = doc.createRoot( 'root' );
  21. root.insertChildren( 0, 'abc' );
  22. batch = doc.batch();
  23. p = new Element( 'p' );
  24. ul = new Element( 'ul' );
  25. chain = batch.insert( new Position( root, [ 2 ] ), [ p, ul ] );
  26. } );
  27. describe( 'insert', () => {
  28. it( 'should insert given nodes at given position', () => {
  29. expect( root.getChildCount() ).to.equal( 5 );
  30. expect( root.getChild( 2 ) ).to.equal( p );
  31. expect( root.getChild( 3 ) ).to.equal( ul );
  32. } );
  33. it( 'should be chainable', () => {
  34. expect( chain ).to.equal( batch );
  35. } );
  36. it( 'should add delta to batch and operation to delta before applying operation', () => {
  37. sinon.spy( doc, 'applyOperation' );
  38. batch.insert( new Position( root, [ 2 ] ), [ p, ul ] );
  39. const correctDeltaMatcher = sinon.match( ( operation ) => {
  40. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  41. } );
  42. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  43. } );
  44. } );
  45. } );
  46. describe( 'InsertDelta', () => {
  47. let insertDelta, doc, root;
  48. beforeEach( () => {
  49. doc = new Document();
  50. root = doc.createRoot( 'root' );
  51. insertDelta = new InsertDelta();
  52. } );
  53. describe( 'constructor', () => {
  54. it( 'should create insert delta with no operations added', () => {
  55. expect( insertDelta.operations.length ).to.equal( 0 );
  56. } );
  57. } );
  58. describe( 'position', () => {
  59. it( 'should be null if there are no operations in delta', () => {
  60. expect( insertDelta.position ).to.be.null;
  61. } );
  62. it( 'should be equal to the position where node is inserted', () => {
  63. insertDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2, 3 ] ), new Element( 'x' ), 0 ) );
  64. expect( insertDelta.position.root ).to.equal( root );
  65. expect( insertDelta.position.path ).to.deep.equal( [ 1, 2, 3 ] );
  66. } );
  67. } );
  68. describe( 'nodeList', () => {
  69. it( 'should be null if there are no operations in delta', () => {
  70. expect( insertDelta.nodeList ).to.be.null;
  71. } );
  72. it( 'should be equal to the node list inserted by the delta', () => {
  73. let elementX = new Element( 'x' );
  74. insertDelta.operations.push( new InsertOperation( new Position( root, [ 1, 2, 3 ] ), elementX, 0 ) );
  75. expect( insertDelta.nodeList.length ).to.equal( 1 );
  76. expect( insertDelta.nodeList.get( 0 ) ).to.equal( elementX );
  77. } );
  78. } );
  79. describe( 'getReversed', () => {
  80. it( 'should return empty RemoveDelta if there are no operations in delta', () => {
  81. let reversed = insertDelta.getReversed();
  82. expect( reversed ).to.be.instanceof( RemoveDelta );
  83. expect( reversed.operations.length ).to.equal( 0 );
  84. } );
  85. it( 'should return correct RemoveDelta', () => {
  86. let position = new Position( root, [ 1, 2, 3 ] );
  87. let elementX = new Element( 'x' );
  88. insertDelta.operations.push( new InsertOperation( position, elementX, 0 ) );
  89. let reversed = insertDelta.getReversed();
  90. expect( reversed ).to.be.instanceof( RemoveDelta );
  91. expect( reversed.operations.length ).to.equal( 1 );
  92. expect( reversed.operations[ 0 ] ).to.be.instanceof( RemoveOperation );
  93. expect( reversed.operations[ 0 ].sourcePosition.isEqual( position ) ).to.be.true;
  94. expect( reversed.operations[ 0 ].howMany ).to.equal( 1 );
  95. } );
  96. } );
  97. describe( 'fromJSON', () => {
  98. it( 'should create InsertDelta instance from serialized JSON object', () => {
  99. let serialized = jsonParseStringify( insertDelta );
  100. let deserialized = Delta.fromJSON( serialized, doc );
  101. expect( deserialized ).to.be.instanceOf( InsertDelta );
  102. expect( deserialized.operations ).to.have.length( 0 );
  103. } );
  104. } );
  105. it( 'should provide proper className', () => {
  106. expect( InsertDelta.className ).to.equal( 'engine.treeModel.delta.InsertDelta' );
  107. } );
  108. } );