change-event.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../../src/model/document';
  6. import Element from '../../../src/model/element';
  7. import Text from '../../../src/model/text';
  8. import Position from '../../../src/model/position';
  9. import Range from '../../../src/model/range';
  10. import AttributeOperation from '../../../src/model/operation/attributeoperation';
  11. import InsertOperation from '../../../src/model/operation/insertoperation';
  12. import MoveOperation from '../../../src/model/operation/moveoperation';
  13. import RemoveOperation from '../../../src/model/operation/removeoperation';
  14. import { wrapInDelta } from '../../../tests/model/_utils/utils';
  15. describe( 'Document change event', () => {
  16. let doc, root, graveyard, types, changes;
  17. beforeEach( () => {
  18. doc = new Document();
  19. root = doc.createRoot();
  20. graveyard = doc.graveyard;
  21. types = [];
  22. changes = [];
  23. doc.on( 'change', ( evt, type, change ) => {
  24. types.push( type );
  25. changes.push( change );
  26. } );
  27. } );
  28. it( 'should be fired when text is inserted', () => {
  29. doc.applyOperation( wrapInDelta( new InsertOperation( new Position( root, [ 0 ] ), 'foo', doc.version ) ) );
  30. expect( changes ).to.have.length( 1 );
  31. expect( types[ 0 ] ).to.equal( 'insert' );
  32. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 3 ) );
  33. } );
  34. it( 'should be fired when element is inserted', () => {
  35. const element = new Element( 'p' );
  36. doc.applyOperation( wrapInDelta( new InsertOperation( new Position( root, [ 0 ] ), element, doc.version ) ) );
  37. expect( changes ).to.have.length( 1 );
  38. expect( types[ 0 ] ).to.equal( 'insert' );
  39. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
  40. } );
  41. it( 'should be fired when nodes are inserted', () => {
  42. const element = new Element( 'p' );
  43. doc.applyOperation( wrapInDelta( new InsertOperation( new Position( root, [ 0 ] ), [ element, 'foo' ], doc.version ) ) );
  44. expect( changes ).to.have.length( 1 );
  45. expect( types[ 0 ] ).to.equal( 'insert' );
  46. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 4 ) );
  47. } );
  48. it( 'should be fired when nodes are moved', () => {
  49. const p1 = new Element( 'p' );
  50. p1.insertChildren( 0, [ new Element( 'p' ), new Text( 'foo' ) ] );
  51. const p2 = new Element( 'p' );
  52. root.insertChildren( 0, [ p1, p2 ] );
  53. doc.applyOperation( wrapInDelta(
  54. new MoveOperation(
  55. new Position( root, [ 0, 0 ] ),
  56. 3,
  57. new Position( root, [ 1, 0 ] ),
  58. doc.version
  59. )
  60. ) );
  61. expect( changes ).to.have.length( 1 );
  62. expect( types[ 0 ] ).to.equal( 'move' );
  63. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( p2, 0, p2, 3 ) );
  64. expect( changes[ 0 ].sourcePosition ).to.deep.equal( Position.createFromParentAndOffset( p1, 0 ) );
  65. } );
  66. it( 'should be fired when multiple nodes are removed and reinserted', () => {
  67. root.insertChildren( 0, new Text( 'foo' ) );
  68. const removeOperation = new RemoveOperation( new Position( root, [ 0 ] ), 3, doc.version );
  69. doc.applyOperation( wrapInDelta( removeOperation ) );
  70. const reinsertOperation = removeOperation.getReversed();
  71. doc.applyOperation( wrapInDelta( reinsertOperation ) );
  72. expect( changes ).to.have.length( 2 );
  73. const holderElement = graveyard.getChild( 0 );
  74. expect( types[ 0 ] ).to.equal( 'remove' );
  75. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( holderElement, 0, holderElement, 3 ) );
  76. expect( changes[ 0 ].sourcePosition ).to.deep.equal( Position.createFromParentAndOffset( root, 0 ) );
  77. expect( types[ 1 ] ).to.equal( 'reinsert' );
  78. expect( changes[ 1 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 3 ) );
  79. expect( changes[ 1 ].sourcePosition ).to.deep.equal( Position.createFromParentAndOffset( holderElement, 0 ) );
  80. } );
  81. it( 'should be fired when attribute is inserted', () => {
  82. root.insertChildren( 0, new Text( 'foo' ) );
  83. doc.applyOperation( wrapInDelta(
  84. new AttributeOperation(
  85. Range.createFromParentsAndOffsets( root, 0, root, 3 ),
  86. 'key',
  87. null,
  88. 'new',
  89. doc.version
  90. )
  91. ) );
  92. expect( changes ).to.have.length( 1 );
  93. expect( types[ 0 ] ).to.equal( 'addAttribute' );
  94. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 3 ) );
  95. expect( changes[ 0 ].key ).to.equal( 'key' );
  96. expect( changes[ 0 ].oldValue ).to.be.null;
  97. expect( changes[ 0 ].newValue ).to.equal( 'new' );
  98. } );
  99. it( 'should be fired when attribute is removed', () => {
  100. const elem = new Element( 'p', { key: 'old' } );
  101. root.insertChildren( 0, elem );
  102. doc.applyOperation( wrapInDelta(
  103. new AttributeOperation(
  104. Range.createFromParentsAndOffsets( root, 0, elem, 0 ),
  105. 'key',
  106. 'old',
  107. null,
  108. doc.version
  109. )
  110. ) );
  111. expect( changes ).to.have.length( 1 );
  112. expect( types[ 0 ] ).to.equal( 'removeAttribute' );
  113. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, elem, 0 ) );
  114. expect( changes[ 0 ].key ).to.equal( 'key' );
  115. expect( changes[ 0 ].oldValue ).to.equal( 'old' );
  116. expect( changes[ 0 ].newValue ).to.be.null;
  117. } );
  118. it( 'should be fired when attribute changes', () => {
  119. const elem = new Element( 'p', { key: 'old' } );
  120. root.insertChildren( 0, elem );
  121. doc.applyOperation( wrapInDelta(
  122. new AttributeOperation(
  123. Range.createFromParentsAndOffsets( root, 0, elem, 0 ),
  124. 'key',
  125. 'old',
  126. 'new',
  127. doc.version
  128. )
  129. ) );
  130. expect( changes ).to.have.length( 1 );
  131. expect( types[ 0 ] ).to.equal( 'changeAttribute' );
  132. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, elem, 0 ) );
  133. expect( changes[ 0 ].key ).to.equal( 'key' );
  134. expect( changes[ 0 ].oldValue ).to.equal( 'old' );
  135. expect( changes[ 0 ].newValue ).to.equal( 'new' );
  136. } );
  137. } );