changedelta.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/transaction',
  11. 'document/document',
  12. 'document/text',
  13. 'document/attribute',
  14. 'document/range',
  15. 'document/position',
  16. 'document/element',
  17. 'document/character' );
  18. describe( 'Transaction', () => {
  19. let Transaction, Document, Text, Attribute, Range, Position, Element, Character;
  20. let doc, root, transaction;
  21. before( () => {
  22. Transaction = modules[ 'document/transaction' ];
  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. transaction = doc.createTransaction();
  35. } );
  36. function getOperationsCount() {
  37. let count = 0;
  38. for ( let delta of transaction ) {
  39. count += getIteratorCount( delta );
  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. transaction.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. transaction.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. transaction.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. transaction.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. transaction.setAttr( 'a', 1, node );
  73. expect( getOperationsCount() ).to.equal( 0 );
  74. expect( node.getAttr( 'a' ) ).to.equal( 1 );
  75. } );
  76. } );
  77. describe( 'removeAttr', () => {
  78. it( 'should remove the attribute from element', () => {
  79. transaction.removeAttr( 'a', node );
  80. expect( getOperationsCount() ).to.equal( 1 );
  81. expect( node.getAttr( 'a' ) ).to.be.null;
  82. } );
  83. it( 'should remove the attribute from character', () => {
  84. transaction.removeAttr( 'a', character );
  85. expect( getOperationsCount() ).to.equal( 1 );
  86. expect( character.getAttr( 'a' ) ).to.be.null;
  87. } );
  88. it( 'should do nothing if the attribute is not set', () => {
  89. transaction.removeAttr( 'b', node );
  90. expect( getOperationsCount() ).to.equal( 0 );
  91. } );
  92. } );
  93. } );
  94. describe( 'change attribute on range', () => {
  95. beforeEach( () => {
  96. root.insertChildren( 0, [
  97. new Text( 'xxx', [ new Attribute( 'a', 1 ) ] ),
  98. 'xxx',
  99. new Text( 'xxx', [ new Attribute( 'a', 1 ) ] ),
  100. new Text( 'xxx', [ new Attribute( 'a', 2 ) ] ),
  101. 'xxx',
  102. new Text( 'xxx', [ new Attribute( 'a', 1 ) ] )
  103. ] );
  104. } );
  105. function getRange( startIndex, endIndex ) {
  106. return new Range(
  107. Position.createFromParentAndOffset( root, startIndex ),
  108. Position.createFromParentAndOffset( root, endIndex )
  109. );
  110. }
  111. function getChangesAttrsCount() {
  112. let count = 0;
  113. for ( let delta of transaction ) {
  114. for ( let operation of delta ) {
  115. count += getIteratorCount( operation.range );
  116. }
  117. }
  118. return count;
  119. }
  120. function getCompressedAttrs() {
  121. // default: 111---111222---111
  122. const range = Range.createFromElement( root );
  123. return Array.from( range ).map( value => value.node.getAttr( 'a' ) || '-' ).join( '' );
  124. }
  125. describe( 'setAttr', () => {
  126. it( 'should set the attribute on the range', () => {
  127. transaction.setAttr( 'a', 3, getRange( 3, 6 ) );
  128. expect( getOperationsCount() ).to.equal( 1 );
  129. expect( getChangesAttrsCount() ).to.equal( 3 );
  130. expect( getCompressedAttrs() ).to.equal( '111333111222---111' );
  131. } );
  132. it( 'should split the operations if parts of the range have different attributes', () => {
  133. transaction.setAttr( 'a', 3, getRange( 4, 14 ) );
  134. expect( getOperationsCount() ).to.equal( 4 );
  135. expect( getChangesAttrsCount() ).to.equal( 10 );
  136. expect( getCompressedAttrs() ).to.equal( '111-3333333333-111' );
  137. } );
  138. it( 'should split the operations if parts of the part of the range have the attribute', () => {
  139. transaction.setAttr( 'a', 2, getRange( 4, 14 ) );
  140. expect( getOperationsCount() ).to.equal( 3 );
  141. expect( getChangesAttrsCount() ).to.equal( 7 );
  142. expect( getCompressedAttrs() ).to.equal( '111-2222222222-111' );
  143. } );
  144. it( 'should strip the range if the beginning have the attribute', () => {
  145. transaction.setAttr( 'a', 1, getRange( 1, 5 ) );
  146. expect( getOperationsCount() ).to.equal( 1 );
  147. expect( getChangesAttrsCount() ).to.equal( 2 );
  148. expect( getCompressedAttrs() ).to.equal( '11111-111222---111' );
  149. } );
  150. it( 'should strip the range if the ending have the attribute', () => {
  151. transaction.setAttr( 'a', 1, getRange( 13, 17 ) );
  152. expect( getOperationsCount() ).to.equal( 1 );
  153. expect( getChangesAttrsCount() ).to.equal( 2 );
  154. expect( getCompressedAttrs() ).to.equal( '111---111222-11111' );
  155. } );
  156. it( 'should do nothing if the range has attribute', () => {
  157. transaction.setAttr( 'a', 1, getRange( 0, 3 ) );
  158. expect( getOperationsCount() ).to.equal( 0 );
  159. expect( getCompressedAttrs() ).to.equal( '111---111222---111' );
  160. } );
  161. it( 'should create a proper operations for the mixed range', () => {
  162. transaction.setAttr( 'a', 1, getRange( 0, 18 ) );
  163. expect( getOperationsCount() ).to.equal( 3 );
  164. expect( getChangesAttrsCount() ).to.equal( 9 );
  165. expect( getCompressedAttrs() ).to.equal( '111111111111111111' );
  166. } );
  167. } );
  168. describe( 'removeAttr', () => {
  169. it( 'should remove the attribute on the range', () => {
  170. transaction.removeAttr( 'a', getRange( 0, 2 ) );
  171. expect( getOperationsCount() ).to.equal( 1 );
  172. expect( getChangesAttrsCount() ).to.equal( 2 );
  173. expect( getCompressedAttrs() ).to.equal( '--1---111222---111' );
  174. } );
  175. it( 'should split the operations if parts of the range have different attributes', () => {
  176. transaction.removeAttr( 'a', getRange( 7, 11 ) );
  177. expect( getOperationsCount() ).to.equal( 2 );
  178. expect( getChangesAttrsCount() ).to.equal( 4 );
  179. expect( getCompressedAttrs() ).to.equal( '111---1----2---111' );
  180. } );
  181. it( 'should split the operations if parts of the part of the range have no attribute', () => {
  182. transaction.removeAttr( 'a', getRange( 1, 7 ) );
  183. expect( getOperationsCount() ).to.equal( 2 );
  184. expect( getChangesAttrsCount() ).to.equal( 3 );
  185. expect( getCompressedAttrs() ).to.equal( '1------11222---111' );
  186. } );
  187. it( 'should strip the range if the beginning have no attribute', () => {
  188. transaction.removeAttr( 'a', getRange( 4, 12 ) );
  189. expect( getOperationsCount() ).to.equal( 2 );
  190. expect( getChangesAttrsCount() ).to.equal( 6 );
  191. expect( getCompressedAttrs() ).to.equal( '111------------111' );
  192. } );
  193. it( 'should strip the range if the ending have no attribute', () => {
  194. transaction.removeAttr( 'a', getRange( 7, 15 ) );
  195. expect( getOperationsCount() ).to.equal( 2 );
  196. expect( getChangesAttrsCount() ).to.equal( 5 );
  197. expect( getCompressedAttrs() ).to.equal( '111---1--------111' );
  198. } );
  199. it( 'should do nothing if the range has no attribute', () => {
  200. transaction.removeAttr( 'a', getRange( 4, 5 ) );
  201. expect( getOperationsCount() ).to.equal( 0 );
  202. expect( getCompressedAttrs() ).to.equal( '111---111222---111' );
  203. } );
  204. it( 'should create a proper operations for the mixed range', () => {
  205. transaction.removeAttr( 'a', getRange( 3, 15 ) );
  206. expect( getOperationsCount() ).to.equal( 2 );
  207. expect( getChangesAttrsCount() ).to.equal( 6 );
  208. expect( getCompressedAttrs() ).to.equal( '111------------111' );
  209. } );
  210. } );
  211. } );
  212. } );