changeoperation.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/changeoperation',
  10. 'document/position',
  11. 'document/range',
  12. 'document/character',
  13. 'document/attribute',
  14. 'document/nodelist',
  15. 'document/text',
  16. 'ckeditorerror' );
  17. describe( 'ChangeOperation', function() {
  18. var Document, ChangeOperation, Position, Range, Character, Attribute, NodeList, Text, CKEditorError;
  19. before( function() {
  20. Document = modules[ 'document/document' ];
  21. ChangeOperation = modules[ 'document/changeoperation' ];
  22. Position = modules[ 'document/position' ];
  23. Range = modules[ 'document/range' ];
  24. Character = modules[ 'document/character' ];
  25. Attribute = modules[ 'document/attribute' ];
  26. NodeList = modules[ 'document/nodelist' ];
  27. Text = modules[ 'document/text' ];
  28. CKEditorError = modules.ckeditorerror;
  29. } );
  30. var doc, root;
  31. beforeEach( function() {
  32. doc = new Document();
  33. root = doc.createRoot( 'root' );
  34. } );
  35. it( 'should insert attribute to the set of nodes', function() {
  36. var newAttr = new Attribute( 'isNew', true );
  37. root.insertChildren( 0, 'bar' );
  38. doc.applyOperation( new ChangeOperation(
  39. new Range( new Position( [ 0 ], root ), new Position( [ 2 ], root ) ),
  40. null,
  41. newAttr,
  42. doc.version ) );
  43. expect( doc.version ).to.equal( 1 );
  44. expect( root.getChildCount() ).to.equal( 3 );
  45. expect( root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
  46. expect( root.getChild( 1 ).hasAttr( newAttr ) ).to.be.true;
  47. expect( root.getChild( 2 )._getAttrCount() ).to.equal( 0 );
  48. } );
  49. it( 'should add attribute to the existing attributes', function() {
  50. var newAttr = new Attribute( 'isNew', true );
  51. var fooAttr = new Attribute( 'foo', true );
  52. var barAttr = new Attribute( 'bar', true );
  53. root.insertChildren( 0, new Character( 'x', [ fooAttr, barAttr ] ) );
  54. doc.applyOperation( new ChangeOperation(
  55. new Range( new Position( [ 0 ], root ), new Position( [ 1 ], root ) ),
  56. null,
  57. newAttr,
  58. doc.version ) );
  59. expect( doc.version ).to.equal( 1 );
  60. expect( root.getChildCount() ).to.equal( 1 );
  61. expect( root.getChild( 0 )._getAttrCount() ).to.equal( 3 );
  62. expect( root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
  63. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
  64. expect( root.getChild( 0 ).hasAttr( barAttr ) ).to.be.true;
  65. } );
  66. it( 'should change attribute to the set of nodes', function() {
  67. var oldAttr = new Attribute( 'isNew', false );
  68. var newAttr = new Attribute( 'isNew', true );
  69. root.insertChildren( 0, new Text( 'bar', [ oldAttr ] ) );
  70. doc.applyOperation( new ChangeOperation(
  71. new Range( new Position( [ 0 ], root ), new Position( [ 2 ], root ) ),
  72. oldAttr,
  73. newAttr,
  74. doc.version ) );
  75. expect( doc.version ).to.equal( 1 );
  76. expect( root.getChildCount() ).to.equal( 3 );
  77. expect( root.getChild( 0 )._getAttrCount() ).to.equal( 1 );
  78. expect( root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
  79. expect( root.getChild( 1 )._getAttrCount() ).to.equal( 1 );
  80. expect( root.getChild( 1 ).hasAttr( newAttr ) ).to.be.true;
  81. expect( root.getChild( 2 )._getAttrCount() ).to.equal( 1 );
  82. expect( root.getChild( 2 ).hasAttr( oldAttr ) ).to.be.true;
  83. } );
  84. it( 'should change attribute in the middle of existing attributes', function() {
  85. var fooAttr = new Attribute( 'foo', true );
  86. var x1Attr = new Attribute( 'x', 1 );
  87. var x2Attr = new Attribute( 'x', 2 );
  88. var barAttr = new Attribute( 'bar', true );
  89. root.insertChildren( 0, new Character( 'x', [ fooAttr, x1Attr, barAttr ] ) );
  90. doc.applyOperation( new ChangeOperation(
  91. new Range( new Position( [ 0 ], root ), new Position( [ 1 ], root ) ),
  92. x1Attr,
  93. x2Attr,
  94. doc.version ) );
  95. expect( doc.version ).to.equal( 1 );
  96. expect( root.getChildCount() ).to.equal( 1 );
  97. expect( root.getChild( 0 )._getAttrCount() ).to.equal( 3 );
  98. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
  99. expect( root.getChild( 0 ).hasAttr( x2Attr ) ).to.be.true;
  100. expect( root.getChild( 0 ).hasAttr( barAttr ) ).to.be.true;
  101. } );
  102. it( 'should remove attribute', function() {
  103. var fooAttr = new Attribute( 'foo', true );
  104. var xAttr = new Attribute( 'x', true );
  105. var barAttr = new Attribute( 'bar', true );
  106. root.insertChildren( 0, new Character( 'x', [ fooAttr, xAttr, barAttr ] ) );
  107. doc.applyOperation( new ChangeOperation(
  108. new Range( new Position( [ 0 ], root ), new Position( [ 1 ], root ) ),
  109. xAttr,
  110. null,
  111. doc.version ) );
  112. expect( doc.version ).to.equal( 1 );
  113. expect( root.getChildCount() ).to.equal( 1 );
  114. expect( root.getChild( 0 )._getAttrCount() ).to.equal( 2 );
  115. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
  116. expect( root.getChild( 0 ).hasAttr( barAttr ) ).to.be.true;
  117. } );
  118. it( 'should create a change operation as a reverse', function() {
  119. var oldAttr = new Attribute( 'x', 'old' );
  120. var newAttr = new Attribute( 'x', 'new' );
  121. var range = new Range( new Position( [ 0 ], root ), new Position( [ 3 ], root ) );
  122. var operation = new ChangeOperation( range, oldAttr, newAttr, doc.version );
  123. var reverse = operation.reverseOperation();
  124. expect( reverse ).to.be.an.instanceof( ChangeOperation );
  125. expect( reverse.baseVersion ).to.equal( 1 );
  126. expect( reverse.range ).to.equal( range );
  127. expect( reverse.oldAttr ).to.equal( newAttr );
  128. expect( reverse.newAttr ).to.equal( oldAttr );
  129. } );
  130. it( 'should undo adding attribute by applying reverse operation', function() {
  131. var newAttr = new Attribute( 'isNew', true );
  132. root.insertChildren( 0, 'bar' );
  133. var operation = new ChangeOperation(
  134. new Range( new Position( [ 0 ], root ), new Position( [ 3 ], root ) ),
  135. null,
  136. newAttr,
  137. doc.version );
  138. var reverse = operation.reverseOperation();
  139. doc.applyOperation( operation );
  140. doc.applyOperation( reverse );
  141. expect( doc.version ).to.equal( 2 );
  142. expect( root.getChildCount() ).to.equal( 3 );
  143. expect( root.getChild( 0 )._getAttrCount() ).to.equal( 0 );
  144. expect( root.getChild( 1 )._getAttrCount() ).to.equal( 0 );
  145. expect( root.getChild( 2 )._getAttrCount() ).to.equal( 0 );
  146. } );
  147. it( 'should undo changing attribute by applying reverse operation', function() {
  148. var oldAttr = new Attribute( 'isNew', false );
  149. var newAttr = new Attribute( 'isNew', true );
  150. root.insertChildren( 0, new Text( 'bar', [ oldAttr ] ) );
  151. var operation = new ChangeOperation(
  152. new Range( new Position( [ 0 ], root ), new Position( [ 3 ], root ) ),
  153. oldAttr,
  154. newAttr,
  155. doc.version );
  156. var reverse = operation.reverseOperation();
  157. doc.applyOperation( operation );
  158. doc.applyOperation( reverse );
  159. expect( doc.version ).to.equal( 2 );
  160. expect( root.getChildCount() ).to.equal( 3 );
  161. expect( root.getChild( 0 )._getAttrCount() ).to.equal( 1 );
  162. expect( root.getChild( 0 ).hasAttr( oldAttr ) ).to.be.true;
  163. expect( root.getChild( 1 )._getAttrCount() ).to.equal( 1 );
  164. expect( root.getChild( 1 ).hasAttr( oldAttr ) ).to.be.true;
  165. expect( root.getChild( 2 )._getAttrCount() ).to.equal( 1 );
  166. expect( root.getChild( 2 ).hasAttr( oldAttr ) ).to.be.true;
  167. } );
  168. it( 'should undo remove attribute by applying reverse operation', function() {
  169. var fooAttr = new Attribute( 'foo', false );
  170. root.insertChildren( 0, new Text( 'bar', [ fooAttr ] ) );
  171. var operation = new ChangeOperation(
  172. new Range( new Position( [ 0 ], root ), new Position( [ 3 ], root ) ),
  173. fooAttr,
  174. null,
  175. doc.version );
  176. var reverse = operation.reverseOperation();
  177. doc.applyOperation( operation );
  178. doc.applyOperation( reverse );
  179. expect( doc.version ).to.equal( 2 );
  180. expect( root.getChildCount() ).to.equal( 3 );
  181. expect( root.getChild( 0 )._getAttrCount() ).to.equal( 1 );
  182. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
  183. expect( root.getChild( 1 )._getAttrCount() ).to.equal( 1 );
  184. expect( root.getChild( 1 ).hasAttr( fooAttr ) ).to.be.true;
  185. expect( root.getChild( 2 )._getAttrCount() ).to.equal( 1 );
  186. expect( root.getChild( 2 ).hasAttr( fooAttr ) ).to.be.true;
  187. } );
  188. it( 'should throw an error when one try to remove and the attribute does not exists', function() {
  189. var fooAttr = new Attribute( 'foo', true );
  190. root.insertChildren( 0, 'x' );
  191. expect( function() {
  192. doc.applyOperation( new ChangeOperation(
  193. new Range( new Position( [ 0 ], root ), new Position( [ 1 ], root ) ),
  194. fooAttr,
  195. null,
  196. doc.version ) );
  197. } ).to.throw( CKEditorError, /operation-change-no-attr-to-remove/ );
  198. } );
  199. it( 'should throw an error when one try to insert and the attribute already exists', function() {
  200. var x1Attr = new Attribute( 'x', 1 );
  201. var x2Attr = new Attribute( 'x', 2 );
  202. root.insertChildren( 0, new Character( 'x', [ x1Attr ] ) );
  203. expect( function() {
  204. doc.applyOperation( new ChangeOperation(
  205. new Range( new Position( [ 0 ], root ), new Position( [ 1 ], root ) ),
  206. null,
  207. x2Attr,
  208. doc.version ) );
  209. } ).to.throw( CKEditorError, /operation-change-attr-exists/ );
  210. } );
  211. it( 'should throw an error when one try to change and the new and old attributes have different keys', function() {
  212. var fooAttr = new Attribute( 'foo', true );
  213. var barAttr = new Attribute( 'bar', true );
  214. root.insertChildren( 0, 'x' );
  215. expect( function() {
  216. doc.applyOperation( new ChangeOperation(
  217. new Range( new Position( [ 0 ], root ), new Position( [ 1 ], root ) ),
  218. fooAttr,
  219. barAttr,
  220. doc.version ) );
  221. } ).to.throw( CKEditorError, /operation-change-different-keys/ );
  222. } );
  223. } );