removeoperation.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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( 'RemoveOperation', function() {
  15. it( 'should remove node', function() {
  16. var Document = modules[ 'document/document' ];
  17. var RemoveOperation = modules[ 'document/removeoperation' ];
  18. var Position = modules[ 'document/position' ];
  19. var doc = new Document();
  20. doc.root.insertChildren( 0, 'x' );
  21. doc.applyOperation( new RemoveOperation(
  22. new Position( [ 0 ], doc.root ),
  23. doc.root.children.get( 0 ),
  24. doc.version ) );
  25. expect( doc.version ).to.be.equal( 1 );
  26. expect( doc.root.children.length ).to.be.equal( 0 );
  27. } );
  28. it( 'should remove set of nodes', function() {
  29. var Document = modules[ 'document/document' ];
  30. var RemoveOperation = modules[ 'document/removeoperation' ];
  31. var Position = modules[ 'document/position' ];
  32. var doc = new Document();
  33. doc.root.insertChildren( 0, 'bar' );
  34. doc.applyOperation( new RemoveOperation(
  35. new Position( [ 0 ], doc.root ),
  36. [ doc.root.children.get( 0 ), doc.root.children.get( 1 ), doc.root.children.get( 2 ) ],
  37. doc.version ) );
  38. expect( doc.version ).to.be.equal( 1 );
  39. expect( doc.root.children.length ).to.be.equal( 0 );
  40. } );
  41. it( 'should remove from between existing nodes', function() {
  42. var Document = modules[ 'document/document' ];
  43. var RemoveOperation = modules[ 'document/removeoperation' ];
  44. var Position = modules[ 'document/position' ];
  45. var doc = new Document();
  46. doc.root.insertChildren( 0, 'bar' );
  47. doc.applyOperation( new RemoveOperation(
  48. new Position( [ 1 ], doc.root ),
  49. [ doc.root.children.get( 1 ) ],
  50. doc.version ) );
  51. expect( doc.version ).to.be.equal( 1 );
  52. expect( doc.root.children.length ).to.be.equal( 2 );
  53. expect( doc.root.children.get( 0 ).character ).to.be.equal( 'b' );
  54. expect( doc.root.children.get( 1 ).character ).to.be.equal( 'r' );
  55. } );
  56. it( 'should create a insert operation as a reverse', function() {
  57. var Document = modules[ 'document/document' ];
  58. var InsertOperation = modules[ 'document/insertoperation' ];
  59. var RemoveOperation = modules[ 'document/removeoperation' ];
  60. var Position = modules[ 'document/position' ];
  61. var NodeList = modules[ 'document/nodelist' ];
  62. var doc = new Document();
  63. var nodeList = new NodeList( 'bar' );
  64. var position = new Position( [ 0 ], doc.root );
  65. doc.root.insertChildren( 0, nodeList );
  66. var operation = new RemoveOperation( position, nodeList, 0 );
  67. var reverse = operation.reverseOperation();
  68. expect( reverse ).to.be.an.instanceof( InsertOperation );
  69. expect( reverse.baseVersion ).to.be.equals( 1 );
  70. expect( reverse.nodeList ).to.be.equals( nodeList );
  71. expect( reverse.position ).to.be.equals( position );
  72. } );
  73. it( 'should undo remove set of nodes by applying reverse operation', function() {
  74. var Document = modules[ 'document/document' ];
  75. var RemoveOperation = modules[ 'document/removeoperation' ];
  76. var Position = modules[ 'document/position' ];
  77. var NodeList = modules[ 'document/nodelist' ];
  78. var doc = new Document();
  79. var nodeList = new NodeList( 'bar' );
  80. var position = new Position( [ 0 ], doc.root );
  81. doc.root.insertChildren( 0, nodeList );
  82. var operation = new RemoveOperation( position, nodeList, 0 );
  83. var reverse = operation.reverseOperation();
  84. doc.applyOperation( operation );
  85. expect( doc.version ).to.be.equal( 1 );
  86. expect( doc.root.children.length ).to.be.equal( 0 );
  87. doc.applyOperation( reverse );
  88. expect( doc.version ).to.be.equal( 2 );
  89. expect( doc.root.children.length ).to.be.equal( 3 );
  90. expect( doc.root.children.get( 0 ).character ).to.be.equal( 'b' );
  91. expect( doc.root.children.get( 1 ).character ).to.be.equal( 'a' );
  92. expect( doc.root.children.get( 2 ).character ).to.be.equal( 'r' );
  93. } );
  94. } );