insertoperation.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, operation */
  6. 'use strict';
  7. import Document from '/ckeditor5/core/treemodel/document.js';
  8. import Node from '/ckeditor5/core/treemodel/node.js';
  9. import NodeList from '/ckeditor5/core/treemodel/nodelist.js';
  10. import InsertOperation from '/ckeditor5/core/treemodel/operation/insertoperation.js';
  11. import RemoveOperation from '/ckeditor5/core/treemodel/operation/removeoperation.js';
  12. import Position from '/ckeditor5/core/treemodel/position.js';
  13. import Character from '/ckeditor5/core/treemodel/character.js';
  14. describe( 'InsertOperation', () => {
  15. let doc, root;
  16. beforeEach( () => {
  17. doc = new Document();
  18. root = doc.createRoot( 'root' );
  19. } );
  20. it( 'should have proper type', () => {
  21. const op = new InsertOperation(
  22. new Position( root, [ 0 ] ),
  23. new Character( 'x' ),
  24. doc.version
  25. );
  26. expect( op.type ).to.equal( 'insert' );
  27. } );
  28. it( 'should insert node', () => {
  29. doc.applyOperation(
  30. new InsertOperation(
  31. new Position( root, [ 0 ] ),
  32. new Character( 'x' ),
  33. doc.version
  34. )
  35. );
  36. expect( doc.version ).to.equal( 1 );
  37. expect( root.getChildCount() ).to.equal( 1 );
  38. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  39. } );
  40. it( 'should insert set of nodes', () => {
  41. doc.applyOperation(
  42. new InsertOperation(
  43. new Position( root, [ 0 ] ),
  44. 'bar',
  45. doc.version
  46. )
  47. );
  48. expect( doc.version ).to.equal( 1 );
  49. expect( root.getChildCount() ).to.equal( 3 );
  50. expect( root.getChild( 0 ).character ).to.equal( 'b' );
  51. expect( root.getChild( 1 ).character ).to.equal( 'a' );
  52. expect( root.getChild( 2 ).character ).to.equal( 'r' );
  53. } );
  54. it( 'should insert between existing nodes', () => {
  55. root.insertChildren( 0, 'xy' );
  56. doc.applyOperation(
  57. new InsertOperation(
  58. new Position( root, [ 1 ] ),
  59. 'bar',
  60. doc.version
  61. )
  62. );
  63. expect( doc.version ).to.equal( 1 );
  64. expect( root.getChildCount() ).to.equal( 5 );
  65. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  66. expect( root.getChild( 1 ).character ).to.equal( 'b' );
  67. expect( root.getChild( 2 ).character ).to.equal( 'a' );
  68. expect( root.getChild( 3 ).character ).to.equal( 'r' );
  69. expect( root.getChild( 4 ).character ).to.equal( 'y' );
  70. } );
  71. it( 'should insert text', () => {
  72. doc.applyOperation(
  73. new InsertOperation(
  74. new Position( root, [ 0 ] ),
  75. [ 'foo', new Character( 'x' ), 'bar' ],
  76. doc.version
  77. )
  78. );
  79. expect( doc.version ).to.equal( 1 );
  80. expect( root.getChildCount() ).to.equal( 7 );
  81. expect( root.getChild( 0 ).character ).to.equal( 'f' );
  82. expect( root.getChild( 1 ).character ).to.equal( 'o' );
  83. expect( root.getChild( 2 ).character ).to.equal( 'o' );
  84. expect( root.getChild( 3 ).character ).to.equal( 'x' );
  85. expect( root.getChild( 4 ).character ).to.equal( 'b' );
  86. expect( root.getChild( 5 ).character ).to.equal( 'a' );
  87. expect( root.getChild( 6 ).character ).to.equal( 'r' );
  88. } );
  89. it( 'should create a RemoveOperation as a reverse', () => {
  90. let position = new Position( root, [ 0 ] );
  91. let operation = new InsertOperation(
  92. position,
  93. [ 'foo', new Character( 'x' ), 'bar' ],
  94. 0
  95. );
  96. let reverse = operation.getReversed();
  97. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  98. expect( reverse.baseVersion ).to.equal( 1 );
  99. expect( reverse.sourcePosition.isEqual( position ) ).to.be.true;
  100. expect( reverse.howMany ).to.equal( 7 );
  101. } );
  102. it( 'should undo insert node by applying reverse operation', () => {
  103. let operation = new InsertOperation(
  104. new Position( root, [ 0 ] ),
  105. new Character( 'x' ),
  106. doc.version
  107. );
  108. let reverse = operation.getReversed();
  109. doc.applyOperation( operation );
  110. expect( doc.version ).to.equal( 1 );
  111. doc.applyOperation( reverse );
  112. expect( doc.version ).to.equal( 2 );
  113. expect( root.getChildCount() ).to.equal( 0 );
  114. } );
  115. it( 'should undo insert set of nodes by applying reverse operation', () => {
  116. let operation = new InsertOperation(
  117. new Position( root, [ 0 ] ),
  118. 'bar',
  119. doc.version
  120. );
  121. let reverse = operation.getReversed();
  122. doc.applyOperation( operation );
  123. expect( doc.version ).to.equal( 1 );
  124. doc.applyOperation( reverse );
  125. expect( doc.version ).to.equal( 2 );
  126. expect( root.getChildCount() ).to.equal( 0 );
  127. } );
  128. it( 'should create operation with the same parameters when cloned', () => {
  129. let position = new Position( root, [ 0 ] );
  130. let nodeA = new Node();
  131. let nodeB = new Node();
  132. let nodes = new NodeList( [ nodeA, nodeB ] );
  133. let baseVersion = doc.version;
  134. let op = new InsertOperation( position, nodes, baseVersion );
  135. let clone = op.clone();
  136. // New instance rather than a pointer to the old instance.
  137. expect( clone ).not.to.be.equal( op );
  138. expect( clone ).to.be.instanceof( InsertOperation );
  139. expect( clone.position.isEqual( position ) ).to.be.true;
  140. expect( clone.nodeList.get( 0 ) ).to.equal( nodeA );
  141. expect( clone.nodeList.get( 1 ) ).to.equal( nodeB );
  142. expect( clone.nodeList.length ).to.equal( 2 );
  143. expect( clone.baseVersion ).to.equal( baseVersion );
  144. } );
  145. } );