changeoperation.js 9.7 KB

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