document.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 Schema from 'ckeditor5/engine/model/schema.js';
  8. import RootElement from 'ckeditor5/engine/model/rootelement.js';
  9. import Element from 'ckeditor5/engine/model/element.js';
  10. import Batch from 'ckeditor5/engine/model/batch.js';
  11. import Delta from 'ckeditor5/engine/model/delta/delta.js';
  12. import Position from 'ckeditor5/engine/model/position.js';
  13. import Range from 'ckeditor5/engine/model/range.js';
  14. import CKEditorError from 'ckeditor5/utils/ckeditorerror.js';
  15. import count from 'ckeditor5/utils/count.js';
  16. import { jsonParseStringify } from 'tests/engine/model/_utils/utils.js';
  17. describe( 'Document', () => {
  18. let doc;
  19. beforeEach( () => {
  20. doc = new Document();
  21. } );
  22. describe( 'constructor()', () => {
  23. it( 'should create Document with no data, empty graveyard and selection set to default range', () => {
  24. expect( doc ).to.have.property( '_roots' ).that.is.instanceof( Map );
  25. expect( doc._roots.size ).to.equal( 1 );
  26. expect( doc.graveyard ).to.be.instanceof( RootElement );
  27. expect( doc.graveyard.maxOffset ).to.equal( 0 );
  28. expect( count( doc.selection.getRanges() ) ).to.equal( 1 );
  29. expect( doc.schema ).to.be.instanceof( Schema );
  30. } );
  31. } );
  32. describe( 'getRootNames()', () => {
  33. it( 'should return empty iterator if no roots exist', () => {
  34. expect( count( doc.getRootNames() ) ).to.equal( 0 );
  35. } );
  36. it( 'should return an iterator of all roots without the graveyard', () => {
  37. doc.createRoot( '$root', 'a' );
  38. doc.createRoot( '$root', 'b' );
  39. expect( Array.from( doc.getRootNames() ) ).to.deep.equal( [ 'a', 'b' ] );
  40. } );
  41. } );
  42. describe( 'createRoot', () => {
  43. it( 'should create a new RootElement with default element and root names, add it to roots map and return it', () => {
  44. let root = doc.createRoot();
  45. expect( doc._roots.size ).to.equal( 2 );
  46. expect( root ).to.be.instanceof( RootElement );
  47. expect( root.maxOffset ).to.equal( 0 );
  48. expect( root ).to.have.property( 'name', '$root' );
  49. expect( root ).to.have.property( 'rootName', 'main' );
  50. } );
  51. it( 'should create a new RootElement with custom element and root names, add it to roots map and return it', () => {
  52. let root = doc.createRoot( 'customElementName', 'customRootName' );
  53. expect( doc._roots.size ).to.equal( 2 );
  54. expect( root ).to.be.instanceof( RootElement );
  55. expect( root.maxOffset ).to.equal( 0 );
  56. expect( root ).to.have.property( 'name', 'customElementName' );
  57. expect( root ).to.have.property( 'rootName', 'customRootName' );
  58. } );
  59. it( 'should throw an error when trying to create a second root with the same name', () => {
  60. doc.createRoot( '$root', 'rootName' );
  61. expect(
  62. () => {
  63. doc.createRoot( '$root', 'rootName' );
  64. }
  65. ).to.throw( CKEditorError, /model-document-createRoot-name-exists/ );
  66. } );
  67. } );
  68. describe( 'getRoot', () => {
  69. it( 'should return a RootElement previously created with given name', () => {
  70. let newRoot = doc.createRoot();
  71. let getRoot = doc.getRoot();
  72. expect( getRoot ).to.equal( newRoot );
  73. } );
  74. it( 'should throw an error when trying to get non-existent root', () => {
  75. expect(
  76. () => {
  77. doc.getRoot( 'root' );
  78. }
  79. ).to.throw( CKEditorError, /model-document-getRoot-root-not-exist/ );
  80. } );
  81. } );
  82. describe( 'hasRoot', () => {
  83. it( 'should return true when Document has RootElement with given name', () => {
  84. doc.createRoot();
  85. expect( doc.hasRoot( 'main' ) ).to.be.true;
  86. } );
  87. it( 'should return false when Document does not have RootElement with given name', () => {
  88. expect( doc.hasRoot( 'noroot' ) ).to.be.false;
  89. } );
  90. } );
  91. describe( 'applyOperation', () => {
  92. it( 'should increase document version, execute operation and fire event with proper data', () => {
  93. const changeCallback = sinon.spy();
  94. const type = 't';
  95. const data = { data: 'x' };
  96. const batch = new Batch();
  97. const delta = new Delta();
  98. let operation = {
  99. type: type,
  100. baseVersion: 0,
  101. _execute: sinon.stub().returns( data )
  102. };
  103. delta.addOperation( operation );
  104. batch.addDelta( delta );
  105. doc.on( 'change', changeCallback );
  106. doc.applyOperation( operation );
  107. expect( doc.version ).to.equal( 1 );
  108. sinon.assert.calledOnce( operation._execute );
  109. sinon.assert.calledOnce( changeCallback );
  110. expect( changeCallback.args[ 0 ][ 1 ] ).to.equal( type );
  111. expect( changeCallback.args[ 0 ][ 2 ] ).to.equal( data );
  112. expect( changeCallback.args[ 0 ][ 3 ] ).to.equal( batch );
  113. } );
  114. it( 'should throw an error on the operation base version and the document version is different', () => {
  115. let operation = {
  116. baseVersion: 1
  117. };
  118. expect(
  119. () => {
  120. doc.applyOperation( operation );
  121. }
  122. ).to.throw( CKEditorError, /model-document-applyOperation-wrong-version/ );
  123. } );
  124. } );
  125. describe( 'batch', () => {
  126. it( 'should create a new batch with the document property', () => {
  127. const batch = doc.batch();
  128. expect( batch ).to.be.instanceof( Batch );
  129. expect( batch ).to.have.property( 'document' ).that.equals( doc );
  130. } );
  131. it( 'should set given batch type', () => {
  132. const batch = doc.batch( 'ignore' );
  133. expect( batch ).to.have.property( 'type' ).that.equals( 'ignore' );
  134. } );
  135. } );
  136. describe( 'enqueue', () => {
  137. it( 'should be executed immediately and fire changesDone event', () => {
  138. let order = [];
  139. doc.on( 'changesDone', () => order.push( 'done' ) );
  140. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  141. expect( order ).to.have.length( 2 );
  142. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  143. expect( order[ 1 ] ).to.equal( 'done' );
  144. } );
  145. it( 'should fire done every time queue is empty', () => {
  146. let order = [];
  147. doc.on( 'changesDone', () => order.push( 'done' ) );
  148. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  149. doc.enqueueChanges( () => order.push( 'enqueue2' ) );
  150. expect( order ).to.have.length( 4 );
  151. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  152. expect( order[ 1 ] ).to.equal( 'done' );
  153. expect( order[ 2 ] ).to.equal( 'enqueue2' );
  154. expect( order[ 3 ] ).to.equal( 'done' );
  155. } );
  156. it( 'should put callbacks in the proper order', () => {
  157. let order = [];
  158. doc.on( 'changesDone', () => order.push( 'done' ) );
  159. doc.enqueueChanges( () => {
  160. order.push( 'enqueue1 start' );
  161. doc.enqueueChanges( () => {
  162. order.push( 'enqueue2 start' );
  163. doc.enqueueChanges( () => order.push( 'enqueue4' ) );
  164. order.push( 'enqueue2 end' );
  165. } );
  166. doc.enqueueChanges( () => order.push( 'enqueue3' ) );
  167. order.push( 'enqueue1 end' );
  168. } );
  169. expect( order ).to.have.length( 7 );
  170. expect( order[ 0 ] ).to.equal( 'enqueue1 start' );
  171. expect( order[ 1 ] ).to.equal( 'enqueue1 end' );
  172. expect( order[ 2 ] ).to.equal( 'enqueue2 start' );
  173. expect( order[ 3 ] ).to.equal( 'enqueue2 end' );
  174. expect( order[ 4 ] ).to.equal( 'enqueue3' );
  175. expect( order[ 5 ] ).to.equal( 'enqueue4' );
  176. expect( order[ 6 ] ).to.equal( 'done' );
  177. } );
  178. } );
  179. describe( 'selection', () => {
  180. it( 'should get updated attributes whenever attribute operation is applied', () => {
  181. sinon.spy( doc.selection, '_updateAttributes' );
  182. doc.fire( 'change', 'addAttribute' );
  183. expect( doc.selection._updateAttributes.called ).to.be.true;
  184. } );
  185. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  186. const root = doc.createRoot();
  187. root.appendChildren( '\uD83D\uDCA9' );
  188. expect( () => {
  189. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 0, root, 1 ) ] );
  190. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  191. expect( () => {
  192. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 2 ) ] );
  193. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  194. } );
  195. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  196. const root = doc.createRoot();
  197. root.appendChildren( 'foo̻̐ͩbar' );
  198. expect( () => {
  199. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 3, root, 9 ) ] );
  200. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  201. expect( () => {
  202. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 4, root, 9 ) ] );
  203. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  204. expect( () => {
  205. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 5, root, 9 ) ] );
  206. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  207. expect( () => {
  208. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 3 ) ] );
  209. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  210. expect( () => {
  211. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 4 ) ] );
  212. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  213. expect( () => {
  214. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 5 ) ] );
  215. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  216. } );
  217. } );
  218. describe( 'getNearestSelectionPosition', () => {
  219. let root;
  220. beforeEach( () => {
  221. doc.schema.registerItem( 'paragraph', '$block' );
  222. root = doc.createRoot();
  223. } );
  224. it( 'should return equal position if text node can be placed at that position', () => {
  225. root.appendChildren( new Element( 'paragraph' ) );
  226. const position = new Position( root, [ 0, 0 ] );
  227. const selectionPosition = doc.getNearestSelectionPosition( position );
  228. expect( position.isEqual( selectionPosition ) ).to.be.true;
  229. } );
  230. it( 'should return a position before if it is a closer position at which text node can be placed', () => {
  231. root.appendChildren( [ new Element( 'paragraph' ), new Element( 'paragraph' ) ] );
  232. const position = new Position( root, [ 1 ] );
  233. const selectionPosition = doc.getNearestSelectionPosition( position );
  234. expect( selectionPosition.path ).to.deep.equal( [ 0, 0 ] );
  235. } );
  236. it( 'should return a position after if it is a closer position at which text node can be placed', () => {
  237. root.appendChildren( [ new Element( 'paragraph' ), new Element( 'image' ), new Element( 'paragraph' ) ] );
  238. const position = new Position( root, [ 2 ] );
  239. const selectionPosition = doc.getNearestSelectionPosition( position );
  240. expect( selectionPosition.path ).to.deep.equal( [ 2, 0 ] );
  241. } );
  242. it( 'should return first position in root if there is no position where text node can be placed', () => {
  243. root.appendChildren( new Element( 'table' ) );
  244. const position = new Position( root, [ 0, 0 ] );
  245. const selectionPosition = doc.getNearestSelectionPosition( position );
  246. expect( selectionPosition.path ).to.deep.equal( [ 0 ] );
  247. } );
  248. } );
  249. describe( '_getDefaultRoot', () => {
  250. it( 'should return graveyard root if there are no other roots in the document', () => {
  251. expect( doc._getDefaultRoot() ).to.equal( doc.graveyard );
  252. } );
  253. it( 'should return the first root added to the document', () => {
  254. let rootA = doc.createRoot( '$root', 'rootA' );
  255. doc.createRoot( '$root', 'rootB' );
  256. doc.createRoot( '$root', 'rootC' );
  257. expect( doc._getDefaultRoot() ).to.equal( rootA );
  258. } );
  259. } );
  260. it( 'should be correctly converted to json', () => {
  261. expect( jsonParseStringify( doc ).selection ).to.equal( '[engine.model.LiveSelection]' );
  262. } );
  263. } );