8
0

change-event.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. 'use strict';
  7. import Document from '/ckeditor5/core/treemodel/document.js';
  8. import Element from '/ckeditor5/core/treemodel/element.js';
  9. import Attribute from '/ckeditor5/core/treemodel/attribute.js';
  10. import Position from '/ckeditor5/core/treemodel/position.js';
  11. import Range from '/ckeditor5/core/treemodel/range.js';
  12. import AttributeOperation from '/ckeditor5/core/treemodel/operation/attributeoperation.js';
  13. import InsertOperation from '/ckeditor5/core/treemodel/operation/insertoperation.js';
  14. import MoveOperation from '/ckeditor5/core/treemodel/operation/moveoperation.js';
  15. import RemoveOperation from '/ckeditor5/core/treemodel/operation/removeoperation.js';
  16. describe( 'Document change event', () => {
  17. let doc, root, graveyard, types, changes;
  18. beforeEach( () => {
  19. doc = new Document();
  20. root = doc.createRoot( 'root' );
  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( 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( 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( 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' ), 'foo' ] );
  52. const p2 = new Element( 'p' );
  53. root.insertChildren( 0, [ p1, p2 ] );
  54. doc.applyOperation(
  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, 'foo' );
  69. const removeOperation = new RemoveOperation( new Position( root, [ 0 ] ), 3, doc.version );
  70. doc.applyOperation( removeOperation );
  71. const reinsertOperation = removeOperation.getReversed();
  72. doc.applyOperation( reinsertOperation );
  73. expect( changes ).to.have.length( 2 );
  74. expect( types[ 0 ] ).to.equal( 'remove' );
  75. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( graveyard, 0, graveyard, 3 ) );
  76. expect( changes[ 0 ].sourcePosition ).to.deep.equal( Position.createFromParentAndOffset( root, 0 ) );
  77. expect( types[ 1 ] ).to.equal( 'reinsert' );
  78. expect( changes[ 1 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 3 ) );
  79. expect( changes[ 1 ].sourcePosition ).to.deep.equal( Position.createFromParentAndOffset( graveyard, 0 ) );
  80. } );
  81. it( 'should be fired when attribute is inserted', () => {
  82. root.insertChildren( 0, 'foo' );
  83. doc.applyOperation(
  84. new AttributeOperation(
  85. Range.createFromParentsAndOffsets( root, 0, root, 3 ),
  86. null,
  87. new Attribute( 'key', 'new' ),
  88. doc.version
  89. )
  90. );
  91. expect( changes ).to.have.length( 1 );
  92. expect( types[ 0 ] ).to.equal( 'attr' );
  93. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 3 ) );
  94. expect( changes[ 0 ].oldAttr ).to.be.null;
  95. expect( changes[ 0 ].newAttr ).to.deep.equal( new Attribute( 'key', 'new' ) );
  96. } );
  97. it( 'should be fired when attribute is removed', () => {
  98. const elem = new Element( 'p', [ new Attribute( 'key', 'old' ) ] );
  99. root.insertChildren( 0, elem );
  100. doc.applyOperation(
  101. new AttributeOperation(
  102. Range.createFromParentsAndOffsets( root, 0, elem, 0 ),
  103. new Attribute( 'key', 'old' ),
  104. null,
  105. doc.version
  106. )
  107. );
  108. expect( changes ).to.have.length( 1 );
  109. expect( types[ 0 ] ).to.equal( 'attr' );
  110. expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, elem, 0 ) );
  111. expect( changes[ 0 ].oldAttr ).to.deep.equal( new Attribute( 'key', 'old' ) );
  112. expect( changes[ 0 ].newAttr ).to.be.null;
  113. } );
  114. it( 'should be fired when attribute changes', () => {
  115. const elem = new Element( 'p', [ new Attribute( 'key', 'old' ) ] );
  116. root.insertChildren( 0, elem );
  117. doc.applyOperation(
  118. new AttributeOperation(
  119. Range.createFromParentsAndOffsets( root, 0, elem, 0 ),
  120. new Attribute( 'key', 'old' ),
  121. new Attribute( 'key', 'new' ),
  122. doc.version
  123. )
  124. );
  125. expect( changes ).to.have.length( 1 );
  126. expect( types[ 0 ] ).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.deep.equal( new Attribute( 'key', 'new' ) );
  130. } );
  131. } );