document-change-event.js 6.2 KB

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