8
0

changedelta.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 assertAttrs( expected ) {
  54. var range = Range.createFromElement( root );
  55. var actual = Array.from( range ).map( value => value.node.getAttr( 'a' ) || '-' ).join( '' );
  56. // actual: 111---111222---111
  57. expect( actual ).to.be.deep.equal( expected );
  58. }
  59. describe( 'setAttr', () => {
  60. it( 'should set the attribute on the range', () => {
  61. transaction.setAttr( new Attribute( 'a', 3 ), getRange( 3, 6 ) );
  62. expect( getOperationsCount() ).
  63. assertAttrs( '111333111222---111' );
  64. } );
  65. it( 'should split the operations if parts of the range have different attributes', () => {
  66. transaction.setAttr( new Attribute( 'a', 3 ), getRange( 4, 14 ) );
  67. assertAttrs( '111-3333333333-111' );
  68. } );
  69. it( 'should split the operations if parts of the part of the range have the attribute', () => {
  70. transaction.setAttr( new Attribute( 'a', 2 ), getRange( 4, 14 ) );
  71. assertAttrs( '111-2222222222-111' );
  72. } );
  73. it( 'should strip the range if the beginning have the attribute', () => {
  74. transaction.setAttr( new Attribute( 'a', 1 ), getRange( 1, 5 ) );
  75. assertAttrs( '11111-111222---111' );
  76. } );
  77. it( 'should strip the range if the ending have the attribute', () => {
  78. transaction.setAttr( new Attribute( 'a', 3 ), getRange( 13, 17 ) );
  79. assertAttrs( '111---111222-11111' );
  80. } );
  81. it( 'should do nothing if the range has attribute', () => {
  82. transaction.setAttr( new Attribute( 'a', 3 ), getRange( 0, 3 ) );
  83. assertAttrs( '111---111222---111' );
  84. } );
  85. it( 'should create a proper operations for the mixed range', () => {
  86. transaction.setAttr( new Attribute( 'a', 1 ), getRange( 0, 18 ) );
  87. assertAttrs( '111111111111111111' );
  88. } );
  89. } );
  90. describe( 'removeAttr', () => {
  91. it( 'should remove the attribute on the range', () => {
  92. transaction.removeAttr( 'a', getRange( 0, 2 ) );
  93. assertAttrs( '--1---111222---111' );
  94. } );
  95. it( 'should split the operations if parts of the range have different attributes', () => {
  96. transaction.removeAttr( 'a', getRange( 7, 11 ) );
  97. assertAttrs( '111---1----2---111' );
  98. } );
  99. it( 'should split the operations if parts of the part of the range have no attribute', () => {
  100. transaction.removeAttr( 'a', getRange( 1, 7 ) );
  101. assertAttrs( '1------11222---111' );
  102. } );
  103. it( 'should strip the range if the beginning have no attribute', () => {
  104. transaction.removeAttr( 'a', getRange( 4, 12 ) );
  105. assertAttrs( '111------------111' );
  106. } );
  107. it( 'should strip the range if the ending have no attribute', () => {
  108. transaction.removeAttr( 'a', getRange( 7, 15 ) );
  109. assertAttrs( '111---1--------111' );
  110. } );
  111. it( 'should do nothing if the range has no attribute', () => {
  112. transaction.removeAttr( 'a', getRange( 4, 5 ) );
  113. assertAttrs( '111---111222---111' );
  114. } );
  115. it( 'should create a proper operations for the mixed range', () => {
  116. transaction.removeAttr( 'a', getRange( 3, 15 ) );
  117. assertAttrs( '111------------111' );
  118. } );
  119. } );
  120. } );