8
0

changeoperation.js 9.5 KB

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