8
0

change-event.js 6.0 KB

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