removeoperation.js 4.6 KB

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