changeoperation.js 11 KB

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