attributedelta.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: document, delta */
  6. /* bender-include: ../../_tools/tools.js */
  7. 'use strict';
  8. const getIteratorCount = bender.tools.core.getIteratorCount;
  9. const modules = bender.amd.require(
  10. 'document/batch',
  11. 'document/document',
  12. 'document/text',
  13. 'document/attribute',
  14. 'document/range',
  15. 'document/position',
  16. 'document/element',
  17. 'document/character' );
  18. describe( 'Batch', () => {
  19. let Batch, Document, Text, Attribute, Range, Position, Element, Character;
  20. let doc, root, batch;
  21. before( () => {
  22. Batch = modules[ 'document/batch' ];
  23. Document = modules[ 'document/document' ];
  24. Text = modules[ 'document/text' ];
  25. Attribute = modules[ 'document/attribute' ];
  26. Range = modules[ 'document/range' ];
  27. Position = modules[ 'document/position' ];
  28. Element = modules[ 'document/element' ];
  29. Character = modules[ 'document/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. ] );
  112. } );
  113. function getRange( startIndex, endIndex ) {
  114. return new Range(
  115. Position.createFromParentAndOffset( root, startIndex ),
  116. Position.createFromParentAndOffset( root, endIndex )
  117. );
  118. }
  119. function getChangesAttrsCount() {
  120. let count = 0;
  121. for ( let delta of batch.deltas ) {
  122. for ( let operation of delta.operations ) {
  123. count += getIteratorCount( operation.range );
  124. }
  125. }
  126. return count;
  127. }
  128. function getCompressedAttrs() {
  129. // default: 111---111222---111
  130. const range = Range.createFromElement( root );
  131. return Array.from( range ).map( value => value.node.getAttr( 'a' ) || '-' ).join( '' );
  132. }
  133. describe( 'setAttr', () => {
  134. it( 'should set the attribute on the range', () => {
  135. batch.setAttr( 'a', 3, getRange( 3, 6 ) );
  136. expect( getOperationsCount() ).to.equal( 1 );
  137. expect( getChangesAttrsCount() ).to.equal( 3 );
  138. expect( getCompressedAttrs() ).to.equal( '111333111222---111' );
  139. } );
  140. it( 'should split the operations if parts of the range have different attributes', () => {
  141. batch.setAttr( 'a', 3, getRange( 4, 14 ) );
  142. expect( getOperationsCount() ).to.equal( 4 );
  143. expect( getChangesAttrsCount() ).to.equal( 10 );
  144. expect( getCompressedAttrs() ).to.equal( '111-3333333333-111' );
  145. } );
  146. it( 'should split the operations if parts of the part of the range have the attribute', () => {
  147. batch.setAttr( 'a', 2, getRange( 4, 14 ) );
  148. expect( getOperationsCount() ).to.equal( 3 );
  149. expect( getChangesAttrsCount() ).to.equal( 7 );
  150. expect( getCompressedAttrs() ).to.equal( '111-2222222222-111' );
  151. } );
  152. it( 'should strip the range if the beginning have the attribute', () => {
  153. batch.setAttr( 'a', 1, getRange( 1, 5 ) );
  154. expect( getOperationsCount() ).to.equal( 1 );
  155. expect( getChangesAttrsCount() ).to.equal( 2 );
  156. expect( getCompressedAttrs() ).to.equal( '11111-111222---111' );
  157. } );
  158. it( 'should strip the range if the ending have the attribute', () => {
  159. batch.setAttr( 'a', 1, getRange( 13, 17 ) );
  160. expect( getOperationsCount() ).to.equal( 1 );
  161. expect( getChangesAttrsCount() ).to.equal( 2 );
  162. expect( getCompressedAttrs() ).to.equal( '111---111222-11111' );
  163. } );
  164. it( 'should do nothing if the range has attribute', () => {
  165. batch.setAttr( 'a', 1, getRange( 0, 3 ) );
  166. expect( getOperationsCount() ).to.equal( 0 );
  167. expect( getCompressedAttrs() ).to.equal( '111---111222---111' );
  168. } );
  169. it( 'should create a proper operations for the mixed range', () => {
  170. batch.setAttr( 'a', 1, getRange( 0, 18 ) );
  171. expect( getOperationsCount() ).to.equal( 3 );
  172. expect( getChangesAttrsCount() ).to.equal( 9 );
  173. expect( getCompressedAttrs() ).to.equal( '111111111111111111' );
  174. } );
  175. it( 'should be chainable', () => {
  176. const chain = batch.setAttr( 'a', 3, getRange( 3, 6 ) );
  177. expect( chain ).to.equal( batch );
  178. } );
  179. } );
  180. describe( 'removeAttr', () => {
  181. it( 'should remove the attribute on the range', () => {
  182. batch.removeAttr( 'a', getRange( 0, 2 ) );
  183. expect( getOperationsCount() ).to.equal( 1 );
  184. expect( getChangesAttrsCount() ).to.equal( 2 );
  185. expect( getCompressedAttrs() ).to.equal( '--1---111222---111' );
  186. } );
  187. it( 'should split the operations if parts of the range have different attributes', () => {
  188. batch.removeAttr( 'a', getRange( 7, 11 ) );
  189. expect( getOperationsCount() ).to.equal( 2 );
  190. expect( getChangesAttrsCount() ).to.equal( 4 );
  191. expect( getCompressedAttrs() ).to.equal( '111---1----2---111' );
  192. } );
  193. it( 'should split the operations if parts of the part of the range have no attribute', () => {
  194. batch.removeAttr( 'a', getRange( 1, 7 ) );
  195. expect( getOperationsCount() ).to.equal( 2 );
  196. expect( getChangesAttrsCount() ).to.equal( 3 );
  197. expect( getCompressedAttrs() ).to.equal( '1------11222---111' );
  198. } );
  199. it( 'should strip the range if the beginning have no attribute', () => {
  200. batch.removeAttr( 'a', getRange( 4, 12 ) );
  201. expect( getOperationsCount() ).to.equal( 2 );
  202. expect( getChangesAttrsCount() ).to.equal( 6 );
  203. expect( getCompressedAttrs() ).to.equal( '111------------111' );
  204. } );
  205. it( 'should strip the range if the ending have no attribute', () => {
  206. batch.removeAttr( 'a', getRange( 7, 15 ) );
  207. expect( getOperationsCount() ).to.equal( 2 );
  208. expect( getChangesAttrsCount() ).to.equal( 5 );
  209. expect( getCompressedAttrs() ).to.equal( '111---1--------111' );
  210. } );
  211. it( 'should do nothing if the range has no attribute', () => {
  212. batch.removeAttr( 'a', getRange( 4, 5 ) );
  213. expect( getOperationsCount() ).to.equal( 0 );
  214. expect( getCompressedAttrs() ).to.equal( '111---111222---111' );
  215. } );
  216. it( 'should create a proper operations for the mixed range', () => {
  217. batch.removeAttr( 'a', getRange( 3, 15 ) );
  218. expect( getOperationsCount() ).to.equal( 2 );
  219. expect( getChangesAttrsCount() ).to.equal( 6 );
  220. expect( getCompressedAttrs() ).to.equal( '111------------111' );
  221. } );
  222. it( 'should be chainable', () => {
  223. const chain = batch.removeAttr( 'a', getRange( 0, 2 ) );
  224. expect( chain ).to.equal( batch );
  225. } );
  226. } );
  227. } );
  228. } );