document-change-event.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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, changes;
  37. beforeEach( () => {
  38. doc = new Document();
  39. root = doc.createRoot( 'root' );
  40. graveyard = doc._graveyard;
  41. changes = [];
  42. doc.on( 'change', ( evt, data ) => {
  43. changes.push( data );
  44. } );
  45. } );
  46. it( 'should be fired when text is inserted', () => {
  47. doc.applyOperation( new InsertOperation( new Position( [ 0 ], root ), 'foo', doc.version ) );
  48. expect( changes ).to.have.length( 1 );
  49. expect( changes[ 0 ].type ).to.equal( 'insert' );
  50. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 3 ) );
  51. } );
  52. it( 'should be fired when element is inserted', () => {
  53. const element = new Element( 'p' );
  54. doc.applyOperation( new InsertOperation( new Position( [ 0 ], root ), element, doc.version ) );
  55. expect( changes ).to.have.length( 1 );
  56. expect( changes[ 0 ].type ).to.equal( 'insert' );
  57. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
  58. } );
  59. it( 'should be fired when nodes are inserted', () => {
  60. const element = new Element( 'p' );
  61. doc.applyOperation( new InsertOperation( new Position( [ 0 ], root ), [ element, 'foo' ], doc.version ) );
  62. expect( changes ).to.have.length( 1 );
  63. expect( changes[ 0 ].type ).to.equal( 'insert' );
  64. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 4 ) );
  65. } );
  66. it( 'should be fired when nodes are moved', () => {
  67. const p1 = new Element( 'p' );
  68. p1.insertChildren( 0, [ new Element( 'p' ), 'foo' ] );
  69. const p2 = new Element( 'p' );
  70. root.insertChildren( 0, [ p1, p2 ] );
  71. doc.applyOperation(
  72. new MoveOperation(
  73. new Position( [ 0, 0 ], root ),
  74. new Position( [ 1, 0 ], root ),
  75. 3,
  76. doc.version
  77. )
  78. );
  79. expect( changes ).to.have.length( 1 );
  80. expect( changes[ 0 ].type ).to.equal( 'move' );
  81. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( p2, 0, p2, 3 ) );
  82. expect( changes[ 0 ].sourcePosition ).to.deep.equal( Position.createFromParentAndOffset( p1, 0 ) );
  83. } );
  84. it( 'should be fired when multiple nodes are removed and reinserted', () => {
  85. root.insertChildren( 0, 'foo' );
  86. const removeOperation = new RemoveOperation( new Position( [ 0 ], root ), 3, doc.version );
  87. doc.applyOperation( removeOperation );
  88. const reinsertOperation = removeOperation.getReversed();
  89. doc.applyOperation( reinsertOperation );
  90. expect( changes ).to.have.length( 2 );
  91. expect( changes[ 0 ].type ).to.equal( 'remove' );
  92. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( graveyard, 0, graveyard, 3 ) );
  93. expect( changes[ 0 ].sourcePosition ).to.deep.equal( Position.createFromParentAndOffset( root, 0 ) );
  94. expect( changes[ 1 ].type ).to.equal( 'reinsert' );
  95. expect( changes[ 1 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 3 ) );
  96. expect( changes[ 1 ].sourcePosition ).to.deep.equal( Position.createFromParentAndOffset( graveyard, 0 ) );
  97. } );
  98. it( 'should be fired when attribute is inserted', () => {
  99. root.insertChildren( 0, 'foo' );
  100. doc.applyOperation(
  101. new ChangeOperation(
  102. Range.createFromParentsAndOffsets( root, 0, root, 3 ),
  103. null,
  104. new Attribute( 'key', 'new' ),
  105. doc.version
  106. )
  107. );
  108. expect( changes ).to.have.length( 1 );
  109. expect( changes[ 0 ].type ).to.equal( 'attr' );
  110. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 3 ) );
  111. expect( changes[ 0 ].oldAttr ).to.be.undefined;
  112. expect( changes[ 0 ].newAttr ).to.deep.equal( new Attribute( 'key', 'new' ) );
  113. } );
  114. it( 'should be fired when attribute is removed', () => {
  115. const elem = new Element( 'p', [ new Attribute( 'key', 'old' ) ] );
  116. root.insertChildren( 0, elem );
  117. doc.applyOperation(
  118. new ChangeOperation(
  119. Range.createFromParentsAndOffsets( root, 0, elem, 0 ),
  120. new Attribute( 'key', 'old' ),
  121. null,
  122. doc.version
  123. )
  124. );
  125. expect( changes ).to.have.length( 1 );
  126. expect( changes[ 0 ].type ).to.equal( 'attr' );
  127. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, elem, 0 ) );
  128. expect( changes[ 0 ].oldAttr ).to.deep.equal( new Attribute( 'key', 'old' ) );
  129. expect( changes[ 0 ].newAttr ).to.be.undefined;
  130. } );
  131. it( 'should be fired when attribute changes', () => {
  132. const elem = new Element( 'p', [ new Attribute( 'key', 'old' ) ] );
  133. root.insertChildren( 0, elem );
  134. doc.applyOperation(
  135. new ChangeOperation(
  136. Range.createFromParentsAndOffsets( root, 0, elem, 0 ),
  137. new Attribute( 'key', 'old' ),
  138. new Attribute( 'key', 'new' ),
  139. doc.version
  140. )
  141. );
  142. expect( changes ).to.have.length( 1 );
  143. expect( changes[ 0 ].type ).to.equal( 'attr' );
  144. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, elem, 0 ) );
  145. expect( changes[ 0 ].oldAttr ).to.deep.equal( new Attribute( 'key', 'old' ) );
  146. expect( changes[ 0 ].newAttr ).to.deep.equal( new Attribute( 'key', 'new' ) );
  147. } );
  148. } );