changeoperation.js 10 KB

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