8
0

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