insertoperation.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: document */
  6. 'use strict';
  7. var modules = bender.amd.require(
  8. 'document/document',
  9. 'document/insertoperation',
  10. 'document/removeoperation',
  11. 'document/position',
  12. 'document/character',
  13. 'document/nodelist' );
  14. describe( 'InsertOperation', function() {
  15. var Document, InsertOperation, RemoveOperation, Position, Character;
  16. before( function() {
  17. Document = modules[ 'document/document' ];
  18. InsertOperation = modules[ 'document/insertoperation' ];
  19. RemoveOperation = modules[ 'document/removeoperation' ];
  20. Position = modules[ 'document/position' ];
  21. Character = modules[ 'document/character' ];
  22. } );
  23. var doc, root;
  24. beforeEach( function() {
  25. doc = new Document();
  26. root = doc.createRoot( 'root' );
  27. } );
  28. it( 'should insert node', function() {
  29. doc.applyOperation( new InsertOperation(
  30. new Position( [ 0 ], root ),
  31. new Character( 'x' ),
  32. doc.version ) );
  33. expect( doc.version ).to.be.equal( 1 );
  34. expect( root.getChildCount() ).to.be.equal( 1 );
  35. expect( root.getChild( 0 ).character ).to.be.equal( 'x' );
  36. } );
  37. it( 'should insert set of nodes', function() {
  38. doc.applyOperation( new InsertOperation( new Position( [ 0 ], root ), 'bar', doc.version ) );
  39. expect( doc.version ).to.be.equal( 1 );
  40. expect( root.getChildCount() ).to.be.equal( 3 );
  41. expect( root.getChild( 0 ).character ).to.be.equal( 'b' );
  42. expect( root.getChild( 1 ).character ).to.be.equal( 'a' );
  43. expect( root.getChild( 2 ).character ).to.be.equal( 'r' );
  44. } );
  45. it( 'should insert between existing nodes', function() {
  46. root.insertChildren( 0, 'xy' );
  47. doc.applyOperation( new InsertOperation( new Position( [ 1 ], root ), 'bar', doc.version ) );
  48. expect( doc.version ).to.be.equal( 1 );
  49. expect( root.getChildCount() ).to.be.equal( 5 );
  50. expect( root.getChild( 0 ).character ).to.be.equal( 'x' );
  51. expect( root.getChild( 1 ).character ).to.be.equal( 'b' );
  52. expect( root.getChild( 2 ).character ).to.be.equal( 'a' );
  53. expect( root.getChild( 3 ).character ).to.be.equal( 'r' );
  54. expect( root.getChild( 4 ).character ).to.be.equal( 'y' );
  55. } );
  56. it( 'should insert text', function() {
  57. doc.applyOperation( new InsertOperation(
  58. new Position( [ 0 ], root ),
  59. [ 'foo', new Character( 'x' ), 'bar' ],
  60. doc.version ) );
  61. expect( doc.version ).to.be.equal( 1 );
  62. expect( root.getChildCount() ).to.be.equal( 7 );
  63. expect( root.getChild( 0 ).character ).to.be.equal( 'f' );
  64. expect( root.getChild( 1 ).character ).to.be.equal( 'o' );
  65. expect( root.getChild( 2 ).character ).to.be.equal( 'o' );
  66. expect( root.getChild( 3 ).character ).to.be.equal( 'x' );
  67. expect( root.getChild( 4 ).character ).to.be.equal( 'b' );
  68. expect( root.getChild( 5 ).character ).to.be.equal( 'a' );
  69. expect( root.getChild( 6 ).character ).to.be.equal( 'r' );
  70. } );
  71. it( 'should create a remove operation as a reverse', function() {
  72. var position = new Position( [ 0 ], root );
  73. var operation = new InsertOperation( position, [ 'foo', new Character( 'x' ), 'bar' ], 0 );
  74. var reverse = operation.reverseOperation();
  75. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  76. expect( reverse.baseVersion ).to.equal( 1 );
  77. expect( reverse.sourcePosition ).to.equal( position );
  78. expect( reverse.howMany ).to.equal( 7 );
  79. } );
  80. it( 'should undo insert node by applying reverse operation', function() {
  81. var operation = new InsertOperation(
  82. new Position( [ 0 ], root ),
  83. new Character( 'x' ),
  84. doc.version );
  85. var reverse = operation.reverseOperation();
  86. doc.applyOperation( operation );
  87. expect( doc.version ).to.be.equal( 1 );
  88. doc.applyOperation( reverse );
  89. expect( doc.version ).to.be.equal( 2 );
  90. expect( root.getChildCount() ).to.be.equal( 0 );
  91. } );
  92. it( 'should undo insert set of nodes by applying reverse operation', function() {
  93. var operation = new InsertOperation( new Position( [ 0 ], root ), 'bar', doc.version );
  94. var reverse = operation.reverseOperation();
  95. doc.applyOperation( operation );
  96. expect( doc.version ).to.be.equal( 1 );
  97. doc.applyOperation( reverse );
  98. expect( doc.version ).to.be.equal( 2 );
  99. expect( root.getChildCount() ).to.be.equal( 0 );
  100. } );
  101. } );