decouplededitor.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 log from '@ckeditor/ckeditor5-utils/src/log';
  17. import { describeMemoryUsage, testMemoryUsage } from '@ckeditor/ckeditor5-core/tests/_utils/memory';
  18. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  19. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  20. const editorData = '<p><strong>foo</strong> bar</p>';
  21. describe( 'DecoupledEditor', () => {
  22. let editor;
  23. testUtils.createSinonSandbox();
  24. beforeEach( () => {
  25. testUtils.sinon.stub( log, 'warn' ).callsFake( () => {} );
  26. } );
  27. describe( 'constructor()', () => {
  28. beforeEach( () => {
  29. editor = new DecoupledEditor();
  30. } );
  31. it( 'uses HTMLDataProcessor', () => {
  32. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  33. } );
  34. it( 'has a Data Interface', () => {
  35. expect( testUtils.isMixed( DecoupledEditor, DataApiMixin ) ).to.be.true;
  36. } );
  37. it( 'creates main root element', () => {
  38. expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
  39. } );
  40. describe( 'ui', () => {
  41. it( 'is created', () => {
  42. editor.ui.init();
  43. expect( editor.ui ).to.be.instanceof( DecoupledEditorUI );
  44. expect( editor.ui.view ).to.be.instanceof( DecoupledEditorUIView );
  45. editor.ui.destroy();
  46. } );
  47. } );
  48. } );
  49. describe( 'create()', () => {
  50. it( 'should properly handled async data initialization', done => {
  51. const spy = sinon.spy();
  52. let resolver;
  53. class AsyncDataInit extends Plugin {
  54. init() {
  55. this.editor.data.on( 'ready', () => spy( 'ready' ) );
  56. this.editor.data.on( 'init', evt => {
  57. evt.stop();
  58. evt.return = new Promise( resolve => {
  59. resolver = () => {
  60. spy( 'asyncInit' );
  61. // Since we stop `init` event, `data#ready` needs to be fired manually.
  62. this.editor.data.fire( 'ready' );
  63. resolve();
  64. };
  65. } );
  66. }, { priority: 'high' } );
  67. }
  68. }
  69. DecoupledEditor.create( '<p>foo bar</p>', {
  70. plugins: [ Paragraph, Bold, AsyncDataInit ]
  71. } ).then( editor => {
  72. sinon.assert.calledWith( spy.firstCall, 'asyncInit' );
  73. sinon.assert.calledWith( spy.secondCall, 'ready' );
  74. editor.destroy().then( done );
  75. } );
  76. // Resolve init promise in next cycle to hold data initialization.
  77. setTimeout( () => resolver() );
  78. } );
  79. describe( 'editor with data', () => {
  80. test( () => editorData );
  81. } );
  82. describe( 'editor with editable element', () => {
  83. let editableElement;
  84. beforeEach( () => {
  85. editableElement = document.createElement( 'div' );
  86. editableElement.innerHTML = editorData;
  87. } );
  88. test( () => editableElement );
  89. } );
  90. it( 'initializes with config.initialData', () => {
  91. return DecoupledEditor.create( document.createElement( 'div' ), {
  92. initialData: '<p>Hello world!</p>',
  93. plugins: [ Paragraph ]
  94. } ).then( editor => {
  95. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  96. editor.destroy();
  97. } );
  98. } );
  99. // See: https://github.com/ckeditor/ckeditor5/issues/746
  100. it( 'should throw when trying to create the editor using the same source element more than once', () => {
  101. const sourceElement = document.createElement( 'div' );
  102. return DecoupledEditor.create( sourceElement )
  103. .then( editor => {
  104. expect( () => {
  105. new DecoupledEditor( sourceElement ); // eslint-disable-line no-new
  106. } ).to.throw( CKEditorError, /^securesourceelement-source-element-used-more-than-once/ );
  107. return editor.destroy();
  108. } );
  109. } );
  110. it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => {
  111. DecoupledEditor.create( '<p>Hello world!</p>', {
  112. initialData: '<p>I am evil!</p>',
  113. plugins: [ Paragraph ]
  114. } ).catch( () => {
  115. done();
  116. } );
  117. } );
  118. function test( getElementOrData ) {
  119. it( 'creates an instance which inherits from the DecoupledEditor', () => {
  120. return DecoupledEditor
  121. .create( getElementOrData(), {
  122. plugins: [ Paragraph, Bold ]
  123. } )
  124. .then( newEditor => {
  125. expect( newEditor ).to.be.instanceof( DecoupledEditor );
  126. return newEditor.destroy();
  127. } );
  128. } );
  129. it( 'loads the initial data', () => {
  130. return DecoupledEditor
  131. .create( getElementOrData(), {
  132. plugins: [ Paragraph, Bold ]
  133. } )
  134. .then( newEditor => {
  135. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  136. return newEditor.destroy();
  137. } );
  138. } );
  139. it( 'should not require config object', () => {
  140. // Just being safe with `builtinPlugins` static property.
  141. class CustomDecoupledEditor extends DecoupledEditor {}
  142. CustomDecoupledEditor.builtinPlugins = [ Paragraph, Bold ];
  143. return CustomDecoupledEditor.create( getElementOrData() )
  144. .then( newEditor => {
  145. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  146. return newEditor.destroy();
  147. } );
  148. } );
  149. // https://github.com/ckeditor/ckeditor5-editor-classic/issues/53
  150. it( 'creates an instance of a DecoupledEditor child class', () => {
  151. class CustomDecoupledEditor extends DecoupledEditor {}
  152. return CustomDecoupledEditor
  153. .create( getElementOrData(), {
  154. plugins: [ Paragraph, Bold ]
  155. } )
  156. .then( newEditor => {
  157. expect( newEditor ).to.be.instanceof( CustomDecoupledEditor );
  158. expect( newEditor ).to.be.instanceof( DecoupledEditor );
  159. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  160. return newEditor.destroy();
  161. } );
  162. } );
  163. // https://github.com/ckeditor/ckeditor5-editor-decoupled/issues/3
  164. it( 'initializes the data controller', () => {
  165. let dataInitSpy;
  166. class DataInitAssertPlugin extends Plugin {
  167. constructor( editor ) {
  168. super();
  169. this.editor = editor;
  170. }
  171. init() {
  172. dataInitSpy = sinon.spy( this.editor.data, 'init' );
  173. }
  174. }
  175. return DecoupledEditor
  176. .create( getElementOrData(), {
  177. plugins: [ Paragraph, Bold, DataInitAssertPlugin ]
  178. } )
  179. .then( newEditor => {
  180. sinon.assert.calledOnce( dataInitSpy );
  181. return newEditor.destroy();
  182. } );
  183. } );
  184. describe( 'events', () => {
  185. it( 'fires all events in the right order', () => {
  186. const fired = [];
  187. function spy( evt ) {
  188. fired.push( `${ evt.name }-${ evt.source.constructor.name.toLowerCase() }` );
  189. }
  190. class EventWatcher extends Plugin {
  191. init() {
  192. this.editor.ui.on( 'ready', spy );
  193. this.editor.data.on( 'ready', spy );
  194. this.editor.on( 'ready', spy );
  195. }
  196. }
  197. return DecoupledEditor
  198. .create( getElementOrData(), {
  199. plugins: [ EventWatcher ]
  200. } )
  201. .then( newEditor => {
  202. expect( fired ).to.deep.equal( [
  203. 'ready-decouplededitorui',
  204. 'ready-datacontroller',
  205. 'ready-decouplededitor'
  206. ] );
  207. return newEditor.destroy();
  208. } );
  209. } );
  210. it( 'fires ready once UI is rendered', () => {
  211. let isReady;
  212. class EventWatcher extends Plugin {
  213. init() {
  214. this.editor.ui.on( 'ready', () => {
  215. isReady = this.editor.ui.view.isRendered;
  216. } );
  217. }
  218. }
  219. return DecoupledEditor
  220. .create( getElementOrData(), {
  221. plugins: [ EventWatcher ]
  222. } )
  223. .then( newEditor => {
  224. expect( isReady ).to.be.true;
  225. return newEditor.destroy();
  226. } );
  227. } );
  228. } );
  229. }
  230. } );
  231. describe( 'destroy', () => {
  232. describe( 'editor with data', () => {
  233. beforeEach( function() {
  234. return DecoupledEditor
  235. .create( editorData, { plugins: [ Paragraph ] } )
  236. .then( newEditor => {
  237. editor = newEditor;
  238. } );
  239. } );
  240. test( () => editorData );
  241. } );
  242. describe( 'editor with editable element', () => {
  243. let editableElement;
  244. beforeEach( function() {
  245. editableElement = document.createElement( 'div' );
  246. editableElement.innerHTML = editorData;
  247. return DecoupledEditor
  248. .create( editableElement, { plugins: [ Paragraph ] } )
  249. .then( newEditor => {
  250. editor = newEditor;
  251. } );
  252. } );
  253. it( 'sets data back to the element', () => {
  254. editor.setData( '<p>foo</p>' );
  255. return editor.destroy()
  256. .then( () => {
  257. expect( editableElement.innerHTML ).to.equal( '<p>foo</p>' );
  258. } );
  259. } );
  260. test( () => editableElement );
  261. } );
  262. function test() {
  263. it( 'destroys the UI', () => {
  264. const spy = sinon.spy( editor.ui, 'destroy' );
  265. return editor.destroy()
  266. .then( () => {
  267. sinon.assert.calledOnce( spy );
  268. } );
  269. } );
  270. }
  271. } );
  272. describeMemoryUsage( () => {
  273. testMemoryUsage(
  274. 'should not grow on multiple create/destroy',
  275. () => DecoupledEditor
  276. .create( document.querySelector( '#mem-editor' ), {
  277. plugins: [ ArticlePluginSet ],
  278. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  279. image: {
  280. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  281. }
  282. } ) );
  283. } );
  284. } );