change-event.js 5.9 KB

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