document.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 RootElement from '/ckeditor5/core/treemodel/rootelement.js';
  9. import Batch from '/ckeditor5/core/treemodel/batch.js';
  10. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  11. import Text from '/ckeditor5/core/treemodel/text.js';
  12. import Element from '/ckeditor5/core/treemodel/element.js';
  13. import Range from '/ckeditor5/core/treemodel/range.js';
  14. import Position from '/ckeditor5/core/treemodel/position.js';
  15. describe( 'Document', () => {
  16. let doc;
  17. beforeEach( () => {
  18. doc = new Document();
  19. } );
  20. describe( 'constructor', () => {
  21. it( 'should create Document with no data, empty graveyard and empty selection', () => {
  22. expect( doc ).to.have.property( 'roots' ).that.is.instanceof( Map );
  23. expect( doc.roots.size ).to.equal( 1 );
  24. expect( doc.graveyard ).to.be.instanceof( RootElement );
  25. expect( doc.graveyard.getChildCount() ).to.equal( 0 );
  26. expect( doc.selection.getRanges().length ).to.equal( 0 );
  27. } );
  28. } );
  29. describe( 'createRoot', () => {
  30. it( 'should create a new RootElement, add it to roots map and return it', () => {
  31. let root = doc.createRoot( 'root' );
  32. expect( doc.roots.size ).to.equal( 2 );
  33. expect( root ).to.be.instanceof( RootElement );
  34. expect( root.getChildCount() ).to.equal( 0 );
  35. } );
  36. it( 'should throw an error when trying to create a second root with the same name', () => {
  37. doc.createRoot( 'root' );
  38. expect(
  39. () => {
  40. doc.createRoot( 'root' );
  41. }
  42. ).to.throw( CKEditorError, /document-createRoot-name-exists/ );
  43. } );
  44. } );
  45. describe( 'getRoot', () => {
  46. it( 'should return a RootElement previously created with given name', () => {
  47. let newRoot = doc.createRoot( 'root' );
  48. let getRoot = doc.getRoot( 'root' );
  49. expect( getRoot ).to.equal( newRoot );
  50. } );
  51. it( 'should throw an error when trying to get non-existent root', () => {
  52. expect(
  53. () => {
  54. doc.getRoot( 'root' );
  55. }
  56. ).to.throw( CKEditorError, /document-createRoot-root-not-exist/ );
  57. } );
  58. } );
  59. describe( 'applyOperation', () => {
  60. it( 'should increase document version, execute operation and fire event with proper data', () => {
  61. const changeCallback = sinon.spy();
  62. const type = 't';
  63. const data = { data: 'x' };
  64. const batch = 'batch';
  65. let operation = {
  66. type: type,
  67. delta: { batch: batch },
  68. baseVersion: 0,
  69. _execute: sinon.stub().returns( data )
  70. };
  71. doc.on( 'change', changeCallback );
  72. doc.applyOperation( operation );
  73. expect( doc.version ).to.equal( 1 );
  74. sinon.assert.calledOnce( operation._execute );
  75. sinon.assert.calledOnce( changeCallback );
  76. expect( changeCallback.args[ 0 ][ 1 ] ).to.equal( type );
  77. expect( changeCallback.args[ 0 ][ 2 ] ).to.equal( data );
  78. expect( changeCallback.args[ 0 ][ 3 ] ).to.equal( batch );
  79. } );
  80. it( 'should throw an error on the operation base version and the document version is different', () => {
  81. let operation = {
  82. baseVersion: 1
  83. };
  84. expect(
  85. () => {
  86. doc.applyOperation( operation );
  87. }
  88. ).to.throw( CKEditorError, /document-applyOperation-wrong-version/ );
  89. } );
  90. } );
  91. describe( 'batch', () => {
  92. it( 'should create a new batch with the document property', () => {
  93. const batch = doc.batch();
  94. expect( batch ).to.be.instanceof( Batch );
  95. expect( batch ).to.have.property( 'doc' ).that.equals( doc );
  96. } );
  97. } );
  98. describe( 'enqueue', () => {
  99. it( 'should be executed immediately and fire changesDone event', () => {
  100. let order = [];
  101. doc.on( 'changesDone', () => order.push( 'done' ) );
  102. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  103. expect( order ).to.have.length( 2 );
  104. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  105. expect( order[ 1 ] ).to.equal( 'done' );
  106. } );
  107. it( 'should fire done every time queue is empty', () => {
  108. let order = [];
  109. doc.on( 'changesDone', () => order.push( 'done' ) );
  110. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  111. doc.enqueueChanges( () => order.push( 'enqueue2' ) );
  112. expect( order ).to.have.length( 4 );
  113. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  114. expect( order[ 1 ] ).to.equal( 'done' );
  115. expect( order[ 2 ] ).to.equal( 'enqueue2' );
  116. expect( order[ 3 ] ).to.equal( 'done' );
  117. } );
  118. it( 'should put callbacks in the proper order', () => {
  119. let order = [];
  120. doc.on( 'changesDone', () => order.push( 'done' ) );
  121. doc.enqueueChanges( () => {
  122. order.push( 'enqueue1 start' );
  123. doc.enqueueChanges( () => {
  124. order.push( 'enqueue2 start' );
  125. doc.enqueueChanges( () => order.push( 'enqueue4' ) );
  126. order.push( 'enqueue2 end' );
  127. } );
  128. doc.enqueueChanges( () => order.push( 'enqueue3' ) );
  129. order.push( 'enqueue1 end' );
  130. } );
  131. expect( order ).to.have.length( 7 );
  132. expect( order[ 0 ] ).to.equal( 'enqueue1 start' );
  133. expect( order[ 1 ] ).to.equal( 'enqueue1 end' );
  134. expect( order[ 2 ] ).to.equal( 'enqueue2 start' );
  135. expect( order[ 3 ] ).to.equal( 'enqueue2 end' );
  136. expect( order[ 4 ] ).to.equal( 'enqueue3' );
  137. expect( order[ 5 ] ).to.equal( 'enqueue4' );
  138. expect( order[ 6 ] ).to.equal( 'done' );
  139. } );
  140. } );
  141. describe( '_setSelectionAttributes', () => {
  142. let root;
  143. beforeEach( () => {
  144. root = doc.createRoot( 'root' );
  145. root.insertChildren( 0, [
  146. new Element( 'p', { p: true } ),
  147. new Text( 'a', { a: true } ),
  148. new Element( 'p', { p: true } ),
  149. new Text( 'b', { b: true } ),
  150. new Text( 'c', { c: true } ),
  151. new Element( 'p', [], [
  152. new Text( 'd', { d: true } )
  153. ] ),
  154. new Element( 'p', { p: true } ),
  155. new Text( 'e', { e: true } )
  156. ] );
  157. } );
  158. it( 'should be fired whenever selection gets updated', () => {
  159. sinon.spy( doc, '_setSelectionAttributes' );
  160. doc.selection.fire( 'update' );
  161. expect( doc._setSelectionAttributes.called ).to.be.true;
  162. } );
  163. it( 'should be fired whenever changes to Tree Model are applied', () => {
  164. sinon.spy( doc, '_setSelectionAttributes' );
  165. doc.fire( 'changesDone' );
  166. expect( doc._setSelectionAttributes.called ).to.be.true;
  167. } );
  168. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  169. doc.selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  170. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  171. // Step into elements when looking for first character:
  172. doc.selection.setRanges( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  173. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  174. } );
  175. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  176. // Take styles from character before selection.
  177. doc.selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  178. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  179. // If there are none,
  180. // Take styles from character after selection.
  181. doc.selection.setRanges( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  182. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  183. // If there are none,
  184. // Look from the selection position to the beginning of node looking for character to take attributes from.
  185. doc.selection.setRanges( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  186. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  187. // If there are none,
  188. // Look from the selection position to the end of node looking for character to take attributes from.
  189. doc.selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  190. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  191. // If there are no characters to copy attributes from, clear selection attributes.
  192. doc.selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  193. expect( Array.from( doc.selection.getAttributes() ) ).to.deep.equal( [] );
  194. } );
  195. } );
  196. } );