8
0

changedelta.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. describe( 'Transaction', () => {
  17. let Transaction, Document, Text, Attribute, Range, Position;
  18. let doc, root, transaction;
  19. before( () => {
  20. Transaction = modules[ 'document/transaction' ];
  21. Document = modules[ 'document/document' ];
  22. Text = modules[ 'document/text' ];
  23. Attribute = modules[ 'document/attribute' ];
  24. Range = modules[ 'document/range' ];
  25. Position = modules[ 'document/position' ];
  26. } );
  27. beforeEach( () => {
  28. doc = new Document();
  29. root = doc.createRoot( 'root' );
  30. root.insertChildren( 0, [
  31. new Text( 'xxx', [ new Attribute( 'a', 1 ) ] ),
  32. 'xxx',
  33. new Text( 'xxx', [ new Attribute( 'a', 1 ) ] ),
  34. new Text( 'xxx', [ new Attribute( 'a', 2 ) ] ),
  35. 'xxx',
  36. new Text( 'xxx', [ new Attribute( 'a', 1 ) ] )
  37. ] );
  38. transaction = doc.makeTransaction();
  39. } );
  40. function getRange( startIndex, endIndex ) {
  41. return new Range(
  42. Position.createFromParentAndOffset( root, startIndex ),
  43. Position.createFromParentAndOffset( root, endIndex )
  44. );
  45. }
  46. function getOperationsCount() {
  47. let count = 0;
  48. for ( let delta of transaction ) {
  49. count += getIteratorCount( delta );
  50. }
  51. return count;
  52. }
  53. function getChangesAttrsCount() {
  54. let count = 0;
  55. for ( let delta of transaction ) {
  56. for ( let operation of delta ) {
  57. count += getIteratorCount( operation.range );
  58. }
  59. }
  60. return count;
  61. }
  62. function getCompressedAttrs() {
  63. // default: 111---111222---111
  64. const range = Range.createFromElement( root );
  65. return Array.from( range ).map( value => value.node.getAttr( 'a' ) || '-' ).join( '' );
  66. }
  67. describe( 'setAttr', () => {
  68. it( 'should set the attribute on the range', () => {
  69. transaction.setAttr( 'a', 3, getRange( 3, 6 ) );
  70. expect( getOperationsCount() ).to.equal( 1 );
  71. expect( getChangesAttrsCount() ).to.equal( 3 );
  72. expect( getCompressedAttrs() ).to.equal( '111333111222---111' );
  73. } );
  74. it( 'should split the operations if parts of the range have different attributes', () => {
  75. transaction.setAttr( 'a', 3, getRange( 4, 14 ) );
  76. expect( getOperationsCount() ).to.equal( 4 );
  77. expect( getChangesAttrsCount() ).to.equal( 10 );
  78. expect( getCompressedAttrs() ).to.equal( '111-3333333333-111' );
  79. } );
  80. it( 'should split the operations if parts of the part of the range have the attribute', () => {
  81. transaction.setAttr( 'a', 2, getRange( 4, 14 ) );
  82. expect( getOperationsCount() ).to.equal( 3 );
  83. expect( getChangesAttrsCount() ).to.equal( 7 );
  84. expect( getCompressedAttrs() ).to.equal( '111-2222222222-111' );
  85. } );
  86. it( 'should strip the range if the beginning have the attribute', () => {
  87. transaction.setAttr( 'a', 1, getRange( 1, 5 ) );
  88. expect( getOperationsCount() ).to.equal( 1 );
  89. expect( getChangesAttrsCount() ).to.equal( 2 );
  90. expect( getCompressedAttrs() ).to.equal( '11111-111222---111' );
  91. } );
  92. it( 'should strip the range if the ending have the attribute', () => {
  93. transaction.setAttr( 'a', 1, getRange( 13, 17 ) );
  94. expect( getOperationsCount() ).to.equal( 1 );
  95. expect( getChangesAttrsCount() ).to.equal( 2 );
  96. expect( getCompressedAttrs() ).to.equal( '111---111222-11111' );
  97. } );
  98. it( 'should do nothing if the range has attribute', () => {
  99. transaction.setAttr( 'a', 1, getRange( 0, 3 ) );
  100. expect( getOperationsCount() ).to.equal( 0 );
  101. expect( getCompressedAttrs() ).to.equal( '111---111222---111' );
  102. } );
  103. it( 'should create a proper operations for the mixed range', () => {
  104. transaction.setAttr( 'a', 1, getRange( 0, 18 ) );
  105. expect( getOperationsCount() ).to.equal( 3 );
  106. expect( getChangesAttrsCount() ).to.equal( 9 );
  107. expect( getCompressedAttrs() ).to.equal( '111111111111111111' );
  108. } );
  109. } );
  110. describe( 'removeAttr', () => {
  111. it( 'should remove the attribute on the range', () => {
  112. transaction.removeAttr( 'a', getRange( 0, 2 ) );
  113. expect( getOperationsCount() ).to.equal( 1 );
  114. expect( getChangesAttrsCount() ).to.equal( 2 );
  115. expect( getCompressedAttrs() ).to.equal( '--1---111222---111' );
  116. } );
  117. it( 'should split the operations if parts of the range have different attributes', () => {
  118. transaction.removeAttr( 'a', getRange( 7, 11 ) );
  119. expect( getOperationsCount() ).to.equal( 2 );
  120. expect( getChangesAttrsCount() ).to.equal( 4 );
  121. expect( getCompressedAttrs() ).to.equal( '111---1----2---111' );
  122. } );
  123. it( 'should split the operations if parts of the part of the range have no attribute', () => {
  124. transaction.removeAttr( 'a', getRange( 1, 7 ) );
  125. expect( getOperationsCount() ).to.equal( 2 );
  126. expect( getChangesAttrsCount() ).to.equal( 3 );
  127. expect( getCompressedAttrs() ).to.equal( '1------11222---111' );
  128. } );
  129. it( 'should strip the range if the beginning have no attribute', () => {
  130. transaction.removeAttr( 'a', getRange( 4, 12 ) );
  131. expect( getOperationsCount() ).to.equal( 2 );
  132. expect( getChangesAttrsCount() ).to.equal( 6 );
  133. expect( getCompressedAttrs() ).to.equal( '111------------111' );
  134. } );
  135. it( 'should strip the range if the ending have no attribute', () => {
  136. transaction.removeAttr( 'a', getRange( 7, 15 ) );
  137. expect( getOperationsCount() ).to.equal( 2 );
  138. expect( getChangesAttrsCount() ).to.equal( 5 );
  139. expect( getCompressedAttrs() ).to.equal( '111---1--------111' );
  140. } );
  141. it( 'should do nothing if the range has no attribute', () => {
  142. transaction.removeAttr( 'a', getRange( 4, 5 ) );
  143. expect( getOperationsCount() ).to.equal( 0 );
  144. expect( getCompressedAttrs() ).to.equal( '111---111222---111' );
  145. } );
  146. it( 'should create a proper operations for the mixed range', () => {
  147. transaction.removeAttr( 'a', getRange( 3, 15 ) );
  148. expect( getOperationsCount() ).to.equal( 2 );
  149. expect( getChangesAttrsCount() ).to.equal( 6 );
  150. expect( getCompressedAttrs() ).to.equal( '111------------111' );
  151. } );
  152. } );
  153. } );