attributedelta.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: treemodel, delta */
  6. /* bender-include: ../../_tools/tools.js */
  7. 'use strict';
  8. const getIteratorCount = bender.tools.core.getIteratorCount;
  9. const modules = bender.amd.require(
  10. 'treemodel/batch',
  11. 'treemodel/document',
  12. 'treemodel/text',
  13. 'treemodel/attribute',
  14. 'treemodel/range',
  15. 'treemodel/position',
  16. 'treemodel/element',
  17. 'treemodel/character' );
  18. describe( 'Batch', () => {
  19. let Batch, Document, Text, Attribute, Range, Position, Element, Character;
  20. let doc, root, batch;
  21. before( () => {
  22. Batch = modules[ 'treemodel/batch' ];
  23. Document = modules[ 'treemodel/document' ];
  24. Text = modules[ 'treemodel/text' ];
  25. Attribute = modules[ 'treemodel/attribute' ];
  26. Range = modules[ 'treemodel/range' ];
  27. Position = modules[ 'treemodel/position' ];
  28. Element = modules[ 'treemodel/element' ];
  29. Character = modules[ 'treemodel/character' ];
  30. } );
  31. beforeEach( () => {
  32. doc = new Document();
  33. root = doc.createRoot( 'root' );
  34. batch = doc.batch();
  35. } );
  36. function getOperationsCount() {
  37. let count = 0;
  38. for ( let delta of batch.deltas ) {
  39. count += getIteratorCount( delta.operations );
  40. }
  41. return count;
  42. }
  43. describe( 'change attribute on node', () => {
  44. let node, character;
  45. beforeEach( () => {
  46. node = new Element( 'p', [ new Attribute( 'a', 1 ) ] );
  47. character = new Character( 'c', [ new Attribute( 'a', 1 ) ] );
  48. root.insertChildren( 0, [ node, character ] );
  49. } );
  50. describe( 'setAttr', () => {
  51. it( 'should create the attribute on element', () => {
  52. batch.setAttr( 'b', 2, node );
  53. expect( getOperationsCount() ).to.equal( 1 );
  54. expect( node.getAttr( 'b' ) ).to.equal( 2 );
  55. } );
  56. it( 'should change the attribute of element', () => {
  57. batch.setAttr( 'a', 2, node );
  58. expect( getOperationsCount() ).to.equal( 1 );
  59. expect( node.getAttr( 'a' ) ).to.equal( 2 );
  60. } );
  61. it( 'should create the attribute on character', () => {
  62. batch.setAttr( 'b', 2, character );
  63. expect( getOperationsCount() ).to.equal( 1 );
  64. expect( character.getAttr( 'b' ) ).to.equal( 2 );
  65. } );
  66. it( 'should change the attribute of character', () => {
  67. batch.setAttr( 'a', 2, character );
  68. expect( getOperationsCount() ).to.equal( 1 );
  69. expect( character.getAttr( 'a' ) ).to.equal( 2 );
  70. } );
  71. it( 'should do nothing if the attribute value is the same', () => {
  72. batch.setAttr( 'a', 1, node );
  73. expect( getOperationsCount() ).to.equal( 0 );
  74. expect( node.getAttr( 'a' ) ).to.equal( 1 );
  75. } );
  76. it( 'should be chainable', () => {
  77. const chain = batch.setAttr( 'b', 2, node );
  78. expect( chain ).to.equal( batch );
  79. } );
  80. } );
  81. describe( 'removeAttr', () => {
  82. it( 'should remove the attribute from element', () => {
  83. batch.removeAttr( 'a', node );
  84. expect( getOperationsCount() ).to.equal( 1 );
  85. expect( node.getAttr( 'a' ) ).to.be.null;
  86. } );
  87. it( 'should remove the attribute from character', () => {
  88. batch.removeAttr( 'a', character );
  89. expect( getOperationsCount() ).to.equal( 1 );
  90. expect( character.getAttr( 'a' ) ).to.be.null;
  91. } );
  92. it( 'should do nothing if the attribute is not set', () => {
  93. batch.removeAttr( 'b', node );
  94. expect( getOperationsCount() ).to.equal( 0 );
  95. } );
  96. it( 'should be chainable', () => {
  97. const chain = batch.removeAttr( 'a', node );
  98. expect( chain ).to.equal( batch );
  99. } );
  100. } );
  101. } );
  102. describe( 'change attribute on range', () => {
  103. beforeEach( () => {
  104. root.insertChildren( 0, [
  105. new Text( 'xxx', [ new Attribute( 'a', 1 ) ] ),
  106. 'xxx',
  107. new Text( 'xxx', [ new Attribute( 'a', 1 ) ] ),
  108. new Text( 'xxx', [ new Attribute( 'a', 2 ) ] ),
  109. 'xxx',
  110. new Text( 'xxx', [ new Attribute( 'a', 1 ) ] ),
  111. new Element( 'e', [ new Attribute( 'a', 2 ) ], 'xxx' ),
  112. 'xxx'
  113. ] );
  114. } );
  115. function getRange( startIndex, endIndex ) {
  116. return new Range(
  117. Position.createFromParentAndOffset( root, startIndex ),
  118. Position.createFromParentAndOffset( root, endIndex )
  119. );
  120. }
  121. function getChangesAttrsCount() {
  122. let count = 0;
  123. for ( let delta of batch.deltas ) {
  124. for ( let operation of delta.operations ) {
  125. count += getIteratorCount( operation.range.getNodes() );
  126. }
  127. }
  128. return count;
  129. }
  130. function getCompressedAttrs() {
  131. // default: 111---111222---1112------
  132. const range = Range.createFromElement( root );
  133. return Array.from( range.getNodes() ).map( node => node.getAttr( 'a' ) || '-' ).join( '' );
  134. }
  135. describe( 'setAttr', () => {
  136. it( 'should set the attribute on the range', () => {
  137. batch.setAttr( 'a', 3, getRange( 3, 6 ) );
  138. expect( getOperationsCount() ).to.equal( 1 );
  139. expect( getChangesAttrsCount() ).to.equal( 3 );
  140. expect( getCompressedAttrs() ).to.equal( '111333111222---1112------' );
  141. } );
  142. it( 'should split the operations if parts of the range have different attributes', () => {
  143. batch.setAttr( 'a', 3, getRange( 4, 14 ) );
  144. expect( getOperationsCount() ).to.equal( 4 );
  145. expect( getChangesAttrsCount() ).to.equal( 10 );
  146. expect( getCompressedAttrs() ).to.equal( '111-3333333333-1112------' );
  147. } );
  148. it( 'should split the operations if parts of the part of the range have the attribute', () => {
  149. batch.setAttr( 'a', 2, getRange( 4, 14 ) );
  150. expect( getOperationsCount() ).to.equal( 3 );
  151. expect( getChangesAttrsCount() ).to.equal( 7 );
  152. expect( getCompressedAttrs() ).to.equal( '111-2222222222-1112------' );
  153. } );
  154. it( 'should strip the range if the beginning have the attribute', () => {
  155. batch.setAttr( 'a', 1, getRange( 1, 5 ) );
  156. expect( getOperationsCount() ).to.equal( 1 );
  157. expect( getChangesAttrsCount() ).to.equal( 2 );
  158. expect( getCompressedAttrs() ).to.equal( '11111-111222---1112------' );
  159. } );
  160. it( 'should strip the range if the ending have the attribute', () => {
  161. batch.setAttr( 'a', 1, getRange( 13, 17 ) );
  162. expect( getOperationsCount() ).to.equal( 1 );
  163. expect( getChangesAttrsCount() ).to.equal( 2 );
  164. expect( getCompressedAttrs() ).to.equal( '111---111222-111112------' );
  165. } );
  166. it( 'should do nothing if the range has attribute', () => {
  167. batch.setAttr( 'a', 1, getRange( 0, 3 ) );
  168. expect( getOperationsCount() ).to.equal( 0 );
  169. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  170. } );
  171. it( 'should not check range\'s start position node when creating operations', () => {
  172. let range = new Range(
  173. new Position( root, [ 18, 1 ] ),
  174. new Position( root, [ 19 ] )
  175. );
  176. batch.setAttr( 'a', 1, range );
  177. expect( getOperationsCount() ).to.equal( 1 );
  178. expect( getChangesAttrsCount() ).to.equal( 2 );
  179. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-11---' );
  180. } );
  181. it( 'should not change elements attribute if range contains closing tag', () => {
  182. let range = new Range(
  183. new Position( root, [ 18, 1 ] ),
  184. new Position( root, [ 21 ] )
  185. );
  186. batch.setAttr( 'a', 1, range );
  187. expect( getOperationsCount() ).to.equal( 1 );
  188. expect( getChangesAttrsCount() ).to.equal( 4 );
  189. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-1111-' );
  190. } );
  191. it( 'should not create an operation if the range contains only closing tag', () => {
  192. let range = new Range(
  193. new Position( root, [ 18, 3 ] ),
  194. new Position( root, [ 19 ] )
  195. );
  196. batch.setAttr( 'a', 3, range );
  197. expect( getOperationsCount() ).to.equal( 0 );
  198. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  199. } );
  200. it( 'should not create an operation if is collapsed', () => {
  201. batch.setAttr( 'a', 1, getRange( 3, 3 ) );
  202. expect( getOperationsCount() ).to.equal( 0 );
  203. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  204. } );
  205. it( 'should create a proper operations for the mixed range', () => {
  206. batch.setAttr( 'a', 1, getRange( 0, 20 ) );
  207. expect( getOperationsCount() ).to.equal( 5 );
  208. expect( getChangesAttrsCount() ).to.equal( 14 );
  209. expect( getCompressedAttrs() ).to.equal( '11111111111111111111111--' );
  210. } );
  211. it( 'should be chainable', () => {
  212. const chain = batch.setAttr( 'a', 3, getRange( 3, 6 ) );
  213. expect( chain ).to.equal( batch );
  214. } );
  215. } );
  216. describe( 'removeAttr', () => {
  217. it( 'should remove the attribute on the range', () => {
  218. batch.removeAttr( 'a', getRange( 0, 2 ) );
  219. expect( getOperationsCount() ).to.equal( 1 );
  220. expect( getChangesAttrsCount() ).to.equal( 2 );
  221. expect( getCompressedAttrs() ).to.equal( '--1---111222---1112------' );
  222. } );
  223. it( 'should split the operations if parts of the range have different attributes', () => {
  224. batch.removeAttr( 'a', getRange( 7, 11 ) );
  225. expect( getOperationsCount() ).to.equal( 2 );
  226. expect( getChangesAttrsCount() ).to.equal( 4 );
  227. expect( getCompressedAttrs() ).to.equal( '111---1----2---1112------' );
  228. } );
  229. it( 'should split the operations if parts of the part of the range have no attribute', () => {
  230. batch.removeAttr( 'a', getRange( 1, 7 ) );
  231. expect( getOperationsCount() ).to.equal( 2 );
  232. expect( getChangesAttrsCount() ).to.equal( 3 );
  233. expect( getCompressedAttrs() ).to.equal( '1------11222---1112------' );
  234. } );
  235. it( 'should strip the range if the beginning have no attribute', () => {
  236. batch.removeAttr( 'a', getRange( 4, 12 ) );
  237. expect( getOperationsCount() ).to.equal( 2 );
  238. expect( getChangesAttrsCount() ).to.equal( 6 );
  239. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  240. } );
  241. it( 'should strip the range if the ending have no attribute', () => {
  242. batch.removeAttr( 'a', getRange( 7, 15 ) );
  243. expect( getOperationsCount() ).to.equal( 2 );
  244. expect( getChangesAttrsCount() ).to.equal( 5 );
  245. expect( getCompressedAttrs() ).to.equal( '111---1--------1112------' );
  246. } );
  247. it( 'should do nothing if the range has no attribute', () => {
  248. batch.removeAttr( 'a', getRange( 4, 5 ) );
  249. expect( getOperationsCount() ).to.equal( 0 );
  250. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  251. } );
  252. it( 'should not check range\'s start position node when creating operations', () => {
  253. let range = new Range(
  254. new Position( root, [ 18, 3 ] ),
  255. new Position( root, [ 19 ] )
  256. );
  257. batch.removeAttr( 'a', range );
  258. expect( getOperationsCount() ).to.equal( 0 );
  259. expect( getChangesAttrsCount() ).to.equal( 0 );
  260. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  261. } );
  262. it( 'should not apply operation twice in the range contains opening and closing tags', () => {
  263. batch.removeAttr( 'a', getRange( 18, 22 ) );
  264. expect( getOperationsCount() ).to.equal( 1 );
  265. expect( getChangesAttrsCount() ).to.equal( 1 );
  266. expect( getCompressedAttrs() ).to.equal( '111---111222---111-------' );
  267. } );
  268. it( 'should not create an operation if range is collapsed', () => {
  269. batch.removeAttr( 'a', getRange( 3, 3 ) );
  270. expect( getOperationsCount() ).to.equal( 0 );
  271. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  272. } );
  273. it( 'should create a proper operations for the mixed range', () => {
  274. batch.removeAttr( 'a', getRange( 3, 15 ) );
  275. expect( getOperationsCount() ).to.equal( 2 );
  276. expect( getChangesAttrsCount() ).to.equal( 6 );
  277. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  278. } );
  279. it( 'should be chainable', () => {
  280. const chain = batch.removeAttr( 'a', getRange( 0, 2 ) );
  281. expect( chain ).to.equal( batch );
  282. } );
  283. } );
  284. } );
  285. } );