decouplededitor.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, setTimeout */
  6. import DecoupledEditorUI from '../src/decouplededitorui';
  7. import DecoupledEditorUIView from '../src/decouplededitoruiview';
  8. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  9. import DecoupledEditor from '../src/decouplededitor';
  10. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  13. import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
  14. import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
  15. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. import { describeMemoryUsage, testMemoryUsage } from '@ckeditor/ckeditor5-core/tests/_utils/memory';
  17. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  18. const editorData = '<p><strong>foo</strong> bar</p>';
  19. describe( 'DecoupledEditor', () => {
  20. let editor;
  21. testUtils.createSinonSandbox();
  22. describe( 'constructor()', () => {
  23. beforeEach( () => {
  24. editor = new DecoupledEditor();
  25. } );
  26. it( 'uses HTMLDataProcessor', () => {
  27. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  28. } );
  29. it( 'has a Data Interface', () => {
  30. expect( testUtils.isMixed( DecoupledEditor, DataApiMixin ) ).to.be.true;
  31. } );
  32. it( 'implements the EditorWithUI interface', () => {
  33. expect( editor.element ).to.be.null;
  34. } );
  35. it( 'creates main root element', () => {
  36. expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
  37. } );
  38. describe( 'ui', () => {
  39. it( 'is created', () => {
  40. editor.ui.init();
  41. expect( editor.ui ).to.be.instanceof( DecoupledEditorUI );
  42. expect( editor.ui.view ).to.be.instanceof( DecoupledEditorUIView );
  43. editor.ui.destroy();
  44. } );
  45. } );
  46. } );
  47. describe( 'create()', () => {
  48. it( 'should properly handled async data initialization', done => {
  49. const spy = sinon.spy();
  50. let resolver;
  51. class AsyncDataInit extends Plugin {
  52. init() {
  53. this.editor.on( 'dataReady', () => spy( 'dataReady' ) );
  54. this.editor.data.on( 'init', evt => {
  55. evt.stop();
  56. evt.return = new Promise( resolve => {
  57. resolver = () => {
  58. spy( 'asyncInit' );
  59. resolve();
  60. };
  61. } );
  62. }, { priority: 'high' } );
  63. }
  64. }
  65. DecoupledEditor.create( '<p>foo bar</p>', {
  66. plugins: [ Paragraph, Bold, AsyncDataInit ]
  67. } ).then( editor => {
  68. sinon.assert.calledWith( spy.firstCall, 'asyncInit' );
  69. sinon.assert.calledWith( spy.secondCall, 'dataReady' );
  70. editor.destroy().then( done );
  71. } );
  72. // Resolve init promise in next cycle to hold data initialization.
  73. setTimeout( () => resolver() );
  74. } );
  75. describe( 'editor with data', () => {
  76. test( () => editorData );
  77. } );
  78. describe( 'editor with editable element', () => {
  79. let editableElement;
  80. beforeEach( () => {
  81. editableElement = document.createElement( 'div' );
  82. editableElement.innerHTML = editorData;
  83. } );
  84. test( () => editableElement );
  85. } );
  86. function test( getElementOrData ) {
  87. it( 'creates an instance which inherits from the DecoupledEditor', () => {
  88. return DecoupledEditor
  89. .create( getElementOrData(), {
  90. plugins: [ Paragraph, Bold ]
  91. } )
  92. .then( newEditor => {
  93. expect( newEditor ).to.be.instanceof( DecoupledEditor );
  94. return newEditor.destroy();
  95. } );
  96. } );
  97. it( 'loads the initial data', () => {
  98. return DecoupledEditor
  99. .create( getElementOrData(), {
  100. plugins: [ Paragraph, Bold ]
  101. } )
  102. .then( newEditor => {
  103. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  104. return newEditor.destroy();
  105. } );
  106. } );
  107. // https://github.com/ckeditor/ckeditor5-editor-classic/issues/53
  108. it( 'creates an instance of a DecoupledEditor child class', () => {
  109. class CustomDecoupledEditor extends DecoupledEditor {}
  110. return CustomDecoupledEditor
  111. .create( getElementOrData(), {
  112. plugins: [ Paragraph, Bold ]
  113. } )
  114. .then( newEditor => {
  115. expect( newEditor ).to.be.instanceof( CustomDecoupledEditor );
  116. expect( newEditor ).to.be.instanceof( DecoupledEditor );
  117. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  118. return newEditor.destroy();
  119. } );
  120. } );
  121. // https://github.com/ckeditor/ckeditor5-editor-decoupled/issues/3
  122. it( 'initializes the data controller', () => {
  123. let dataInitSpy;
  124. class DataInitAssertPlugin extends Plugin {
  125. constructor( editor ) {
  126. super();
  127. this.editor = editor;
  128. }
  129. init() {
  130. dataInitSpy = sinon.spy( this.editor.data, 'init' );
  131. }
  132. }
  133. return DecoupledEditor
  134. .create( getElementOrData(), {
  135. plugins: [ Paragraph, Bold, DataInitAssertPlugin ]
  136. } )
  137. .then( newEditor => {
  138. sinon.assert.calledOnce( dataInitSpy );
  139. return newEditor.destroy();
  140. } );
  141. } );
  142. describe( 'events', () => {
  143. it( 'fires all events in the right order', () => {
  144. const fired = [];
  145. function spy( evt ) {
  146. fired.push( evt.name );
  147. }
  148. class EventWatcher extends Plugin {
  149. init() {
  150. this.editor.on( 'pluginsReady', spy );
  151. this.editor.on( 'uiReady', spy );
  152. this.editor.on( 'dataReady', spy );
  153. this.editor.on( 'ready', spy );
  154. }
  155. }
  156. return DecoupledEditor
  157. .create( getElementOrData(), {
  158. plugins: [ EventWatcher ]
  159. } )
  160. .then( newEditor => {
  161. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  162. return newEditor.destroy();
  163. } );
  164. } );
  165. it( 'fires dataReady once data is loaded', () => {
  166. let data;
  167. class EventWatcher extends Plugin {
  168. init() {
  169. this.editor.on( 'dataReady', () => {
  170. data = this.editor.getData();
  171. } );
  172. }
  173. }
  174. return DecoupledEditor
  175. .create( getElementOrData(), {
  176. plugins: [ EventWatcher, Paragraph, Bold ]
  177. } )
  178. .then( newEditor => {
  179. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  180. return newEditor.destroy();
  181. } );
  182. } );
  183. it( 'fires uiReady once UI is rendered', () => {
  184. let isReady;
  185. class EventWatcher extends Plugin {
  186. init() {
  187. this.editor.on( 'uiReady', () => {
  188. isReady = this.editor.ui.view.isRendered;
  189. } );
  190. }
  191. }
  192. return DecoupledEditor
  193. .create( getElementOrData(), {
  194. plugins: [ EventWatcher ]
  195. } )
  196. .then( newEditor => {
  197. expect( isReady ).to.be.true;
  198. return newEditor.destroy();
  199. } );
  200. } );
  201. } );
  202. }
  203. } );
  204. describe( 'destroy', () => {
  205. describe( 'editor with data', () => {
  206. beforeEach( function() {
  207. return DecoupledEditor
  208. .create( editorData, { plugins: [ Paragraph ] } )
  209. .then( newEditor => {
  210. editor = newEditor;
  211. } );
  212. } );
  213. test( () => editorData );
  214. } );
  215. describe( 'editor with editable element', () => {
  216. let editableElement;
  217. beforeEach( function() {
  218. editableElement = document.createElement( 'div' );
  219. editableElement.innerHTML = editorData;
  220. return DecoupledEditor
  221. .create( editableElement, { plugins: [ Paragraph ] } )
  222. .then( newEditor => {
  223. editor = newEditor;
  224. } );
  225. } );
  226. it( 'sets data back to the element', () => {
  227. editor.setData( '<p>foo</p>' );
  228. return editor.destroy()
  229. .then( () => {
  230. expect( editableElement.innerHTML ).to.equal( '<p>foo</p>' );
  231. } );
  232. } );
  233. test( () => editableElement );
  234. } );
  235. function test() {
  236. it( 'destroys the UI', () => {
  237. const spy = sinon.spy( editor.ui, 'destroy' );
  238. return editor.destroy()
  239. .then( () => {
  240. sinon.assert.calledOnce( spy );
  241. } );
  242. } );
  243. }
  244. } );
  245. describeMemoryUsage( () => {
  246. testMemoryUsage(
  247. 'should not grow on multiple create/destroy',
  248. () => DecoupledEditor
  249. .create( document.querySelector( '#mem-editor' ), {
  250. plugins: [ ArticlePluginSet ],
  251. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  252. image: {
  253. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  254. }
  255. } ) );
  256. } );
  257. } );