insertoperation.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. it( 'should insert node', function() {
  16. var Document = modules[ 'document/document' ];
  17. var InsertOperation = modules[ 'document/insertoperation' ];
  18. var Position = modules[ 'document/position' ];
  19. var Character = modules[ 'document/character' ];
  20. var doc = new Document();
  21. doc.applyOperation( new InsertOperation(
  22. new Position( [ 0 ], doc.root ),
  23. new Character( 'x' ),
  24. doc.version ) );
  25. expect( doc.version ).to.be.equal( 1 );
  26. expect( doc.root.children.length ).to.be.equal( 1 );
  27. expect( doc.root.children.get( 0 ).character ).to.be.equal( 'x' );
  28. } );
  29. it( 'should insert set of nodes', function() {
  30. var Document = modules[ 'document/document' ];
  31. var InsertOperation = modules[ 'document/insertoperation' ];
  32. var Position = modules[ 'document/position' ];
  33. var doc = new Document();
  34. doc.applyOperation( new InsertOperation( new Position( [ 0 ], doc.root ), 'bar', doc.version ) );
  35. expect( doc.version ).to.be.equal( 1 );
  36. expect( doc.root.children.length ).to.be.equal( 3 );
  37. expect( doc.root.children.get( 0 ).character ).to.be.equal( 'b' );
  38. expect( doc.root.children.get( 1 ).character ).to.be.equal( 'a' );
  39. expect( doc.root.children.get( 2 ).character ).to.be.equal( 'r' );
  40. } );
  41. it( 'should insert between existing nodes', function() {
  42. var Document = modules[ 'document/document' ];
  43. var InsertOperation = modules[ 'document/insertoperation' ];
  44. var Position = modules[ 'document/position' ];
  45. var doc = new Document();
  46. doc.root.insertChildren( 0, 'xy' );
  47. doc.applyOperation( new InsertOperation( new Position( [ 1 ], doc.root ), 'bar', doc.version ) );
  48. expect( doc.version ).to.be.equal( 1 );
  49. expect( doc.root.children.length ).to.be.equal( 5 );
  50. expect( doc.root.children.get( 0 ).character ).to.be.equal( 'x' );
  51. expect( doc.root.children.get( 1 ).character ).to.be.equal( 'b' );
  52. expect( doc.root.children.get( 2 ).character ).to.be.equal( 'a' );
  53. expect( doc.root.children.get( 3 ).character ).to.be.equal( 'r' );
  54. expect( doc.root.children.get( 4 ).character ).to.be.equal( 'y' );
  55. } );
  56. it( 'should insert text', function() {
  57. var Document = modules[ 'document/document' ];
  58. var InsertOperation = modules[ 'document/insertoperation' ];
  59. var Position = modules[ 'document/position' ];
  60. var Character = modules[ 'document/character' ];
  61. var doc = new Document();
  62. doc.applyOperation( new InsertOperation(
  63. new Position( [ 0 ], doc.root ),
  64. [ 'foo', new Character( 'x' ), 'bar' ],
  65. doc.version ) );
  66. expect( doc.version ).to.be.equal( 1 );
  67. expect( doc.root.children.length ).to.be.equal( 7 );
  68. expect( doc.root.children.get( 0 ).character ).to.be.equal( 'f' );
  69. expect( doc.root.children.get( 1 ).character ).to.be.equal( 'o' );
  70. expect( doc.root.children.get( 2 ).character ).to.be.equal( 'o' );
  71. expect( doc.root.children.get( 3 ).character ).to.be.equal( 'x' );
  72. expect( doc.root.children.get( 4 ).character ).to.be.equal( 'b' );
  73. expect( doc.root.children.get( 5 ).character ).to.be.equal( 'a' );
  74. expect( doc.root.children.get( 6 ).character ).to.be.equal( 'r' );
  75. } );
  76. it( 'should create a remove operation as a reverse', function() {
  77. var Document = modules[ 'document/document' ];
  78. var InsertOperation = modules[ 'document/insertoperation' ];
  79. var RemoveOperation = modules[ 'document/removeoperation' ];
  80. var Position = modules[ 'document/position' ];
  81. var NodeList = modules[ 'document/nodelist' ];
  82. var doc = new Document();
  83. var nodeList = new NodeList( 'bar' );
  84. var position = new Position( [ 0 ], doc.root );
  85. var operation = new InsertOperation( position, nodeList, 0 );
  86. var reverse = operation.reverseOperation();
  87. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  88. expect( reverse.baseVersion ).to.be.equals( 1 );
  89. expect( reverse.nodeList ).to.be.equals( nodeList );
  90. expect( reverse.position ).to.be.equals( position );
  91. } );
  92. it( 'should undo insert node by applying reverse operation', function() {
  93. var Document = modules[ 'document/document' ];
  94. var InsertOperation = modules[ 'document/insertoperation' ];
  95. var Position = modules[ 'document/position' ];
  96. var Character = modules[ 'document/character' ];
  97. var doc = new Document();
  98. var operation = new InsertOperation(
  99. new Position( [ 0 ], doc.root ),
  100. new Character( 'x' ),
  101. doc.version );
  102. var reverse = operation.reverseOperation();
  103. doc.applyOperation( operation );
  104. expect( doc.version ).to.be.equal( 1 );
  105. doc.applyOperation( reverse );
  106. expect( doc.version ).to.be.equal( 2 );
  107. expect( doc.root.children.length ).to.be.equal( 0 );
  108. } );
  109. it( 'should undo insert set of nodes by applying reverse operation', function() {
  110. var Document = modules[ 'document/document' ];
  111. var InsertOperation = modules[ 'document/insertoperation' ];
  112. var Position = modules[ 'document/position' ];
  113. var doc = new Document();
  114. var operation = new InsertOperation( new Position( [ 0 ], doc.root ), 'bar', doc.version );
  115. var reverse = operation.reverseOperation();
  116. doc.applyOperation( operation );
  117. expect( doc.version ).to.be.equal( 1 );
  118. doc.applyOperation( reverse );
  119. expect( doc.version ).to.be.equal( 2 );
  120. expect( doc.root.children.length ).to.be.equal( 0 );
  121. } );
  122. } );