change-event.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 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. describe( 'Document change event', () => {
  16. let doc, root, graveyard, types, changes;
  17. beforeEach( () => {
  18. doc = new Document();
  19. root = doc.createRoot( '$root', 'root' );
  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( 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( 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( 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' ), 'foo' ] );
  51. const p2 = new Element( 'p' );
  52. root.insertChildren( 0, [ p1, p2 ] );
  53. doc.applyOperation(
  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, 'foo' );
  68. const removeOperation = new RemoveOperation( new Position( root, [ 0 ] ), 3, doc.version );
  69. doc.applyOperation( removeOperation );
  70. const reinsertOperation = removeOperation.getReversed();
  71. doc.applyOperation( reinsertOperation );
  72. expect( changes ).to.have.length( 2 );
  73. expect( types[ 0 ] ).to.equal( 'remove' );
  74. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( graveyard, 0, graveyard, 3 ) );
  75. expect( changes[ 0 ].sourcePosition ).to.deep.equal( Position.createFromParentAndOffset( root, 0 ) );
  76. expect( types[ 1 ] ).to.equal( 'reinsert' );
  77. expect( changes[ 1 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 3 ) );
  78. expect( changes[ 1 ].sourcePosition ).to.deep.equal( Position.createFromParentAndOffset( graveyard, 0 ) );
  79. } );
  80. it( 'should be fired when attribute is inserted', () => {
  81. root.insertChildren( 0, 'foo' );
  82. doc.applyOperation(
  83. new AttributeOperation(
  84. Range.createFromParentsAndOffsets( root, 0, root, 3 ),
  85. 'key',
  86. null,
  87. 'new',
  88. doc.version
  89. )
  90. );
  91. expect( changes ).to.have.length( 1 );
  92. expect( types[ 0 ] ).to.equal( 'addAttribute' );
  93. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 3 ) );
  94. expect( changes[ 0 ].key ).to.equal( 'key' );
  95. expect( changes[ 0 ].oldValue ).to.be.null;
  96. expect( changes[ 0 ].newValue ).to.equal( 'new' );
  97. } );
  98. it( 'should be fired when attribute is removed', () => {
  99. const elem = new Element( 'p', { key: 'old' } );
  100. root.insertChildren( 0, elem );
  101. doc.applyOperation(
  102. new AttributeOperation(
  103. Range.createFromParentsAndOffsets( root, 0, elem, 0 ),
  104. 'key',
  105. 'old',
  106. null,
  107. doc.version
  108. )
  109. );
  110. expect( changes ).to.have.length( 1 );
  111. expect( types[ 0 ] ).to.equal( 'removeAttribute' );
  112. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, elem, 0 ) );
  113. expect( changes[ 0 ].key ).to.equal( 'key' );
  114. expect( changes[ 0 ].oldValue ).to.equal( 'old' );
  115. expect( changes[ 0 ].newValue ).to.be.null;
  116. } );
  117. it( 'should be fired when attribute changes', () => {
  118. const elem = new Element( 'p', { key: 'old' } );
  119. root.insertChildren( 0, elem );
  120. doc.applyOperation(
  121. new AttributeOperation(
  122. Range.createFromParentsAndOffsets( root, 0, elem, 0 ),
  123. 'key',
  124. 'old',
  125. 'new',
  126. doc.version
  127. )
  128. );
  129. expect( changes ).to.have.length( 1 );
  130. expect( types[ 0 ] ).to.equal( 'changeAttribute' );
  131. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, elem, 0 ) );
  132. expect( changes[ 0 ].key ).to.equal( 'key' );
  133. expect( changes[ 0 ].oldValue ).to.equal( 'old' );
  134. expect( changes[ 0 ].newValue ).to.equal( 'new' );
  135. } );
  136. } );