decouplededitor.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  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. it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => {
  100. DecoupledEditor.create( '<p>Hello world!</p>', {
  101. initialData: '<p>I am evil!</p>',
  102. plugins: [ Paragraph ]
  103. } )
  104. .then(
  105. () => {
  106. expect.fail( 'Decoupled editor should throw an error when both initial data are passed' );
  107. },
  108. err => {
  109. assertCKEditorError( err,
  110. // eslint-disable-next-line max-len
  111. /^editor-create-initial-data: The config\.initialData option cannot be used together with initial data passed in Editor\.create\(\)\./,
  112. null
  113. );
  114. }
  115. )
  116. .then( done )
  117. .catch( done );
  118. } );
  119. it( 'throws error if it is initialized in textarea', done => {
  120. DecoupledEditor.create( document.createElement( 'textarea' ) )
  121. .then(
  122. () => {
  123. expect.fail( 'Decoupled editor should throw an error when is initialized in textarea.' );
  124. },
  125. err => {
  126. assertCKEditorError( err,
  127. /^editor-wrong-element: This type of editor cannot be initialized inside <textarea> element\./,
  128. null
  129. );
  130. }
  131. )
  132. .then( done )
  133. .catch( done );
  134. } );
  135. function test( getElementOrData ) {
  136. it( 'creates an instance which inherits from the DecoupledEditor', () => {
  137. return DecoupledEditor
  138. .create( getElementOrData(), {
  139. plugins: [ Paragraph, Bold ]
  140. } )
  141. .then( newEditor => {
  142. expect( newEditor ).to.be.instanceof( DecoupledEditor );
  143. return newEditor.destroy();
  144. } );
  145. } );
  146. it( 'loads the initial data', () => {
  147. return DecoupledEditor
  148. .create( getElementOrData(), {
  149. plugins: [ Paragraph, Bold ]
  150. } )
  151. .then( newEditor => {
  152. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  153. return newEditor.destroy();
  154. } );
  155. } );
  156. it( 'should not require config object', () => {
  157. // Just being safe with `builtinPlugins` static property.
  158. class CustomDecoupledEditor extends DecoupledEditor {}
  159. CustomDecoupledEditor.builtinPlugins = [ Paragraph, Bold ];
  160. return CustomDecoupledEditor.create( getElementOrData() )
  161. .then( newEditor => {
  162. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  163. return newEditor.destroy();
  164. } );
  165. } );
  166. // https://github.com/ckeditor/ckeditor5-editor-classic/issues/53
  167. it( 'creates an instance of a DecoupledEditor child class', () => {
  168. class CustomDecoupledEditor extends DecoupledEditor {}
  169. return CustomDecoupledEditor
  170. .create( getElementOrData(), {
  171. plugins: [ Paragraph, Bold ]
  172. } )
  173. .then( newEditor => {
  174. expect( newEditor ).to.be.instanceof( CustomDecoupledEditor );
  175. expect( newEditor ).to.be.instanceof( DecoupledEditor );
  176. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  177. return newEditor.destroy();
  178. } );
  179. } );
  180. // https://github.com/ckeditor/ckeditor5-editor-decoupled/issues/3
  181. it( 'initializes the data controller', () => {
  182. let dataInitSpy;
  183. class DataInitAssertPlugin extends Plugin {
  184. constructor( editor ) {
  185. super();
  186. this.editor = editor;
  187. }
  188. init() {
  189. dataInitSpy = sinon.spy( this.editor.data, 'init' );
  190. }
  191. }
  192. return DecoupledEditor
  193. .create( getElementOrData(), {
  194. plugins: [ Paragraph, Bold, DataInitAssertPlugin ]
  195. } )
  196. .then( newEditor => {
  197. sinon.assert.calledOnce( dataInitSpy );
  198. return newEditor.destroy();
  199. } );
  200. } );
  201. describe( 'events', () => {
  202. it( 'fires all events in the right order', () => {
  203. const fired = [];
  204. function spy( evt ) {
  205. fired.push( `${ evt.name }-${ evt.source.constructor.name.toLowerCase() }` );
  206. }
  207. class EventWatcher extends Plugin {
  208. init() {
  209. this.editor.ui.on( 'ready', spy );
  210. this.editor.data.on( 'ready', spy );
  211. this.editor.on( 'ready', spy );
  212. }
  213. }
  214. return DecoupledEditor
  215. .create( getElementOrData(), {
  216. plugins: [ EventWatcher ]
  217. } )
  218. .then( newEditor => {
  219. expect( fired ).to.deep.equal( [
  220. 'ready-decouplededitorui',
  221. 'ready-datacontroller',
  222. 'ready-decouplededitor'
  223. ] );
  224. return newEditor.destroy();
  225. } );
  226. } );
  227. it( 'fires ready once UI is rendered', () => {
  228. let isReady;
  229. class EventWatcher extends Plugin {
  230. init() {
  231. this.editor.ui.on( 'ready', () => {
  232. isReady = this.editor.ui.view.isRendered;
  233. } );
  234. }
  235. }
  236. return DecoupledEditor
  237. .create( getElementOrData(), {
  238. plugins: [ EventWatcher ]
  239. } )
  240. .then( newEditor => {
  241. expect( isReady ).to.be.true;
  242. return newEditor.destroy();
  243. } );
  244. } );
  245. } );
  246. }
  247. } );
  248. describe( 'destroy', () => {
  249. describe( 'editor with data', () => {
  250. beforeEach( function() {
  251. return DecoupledEditor
  252. .create( editorData, { plugins: [ Paragraph ] } )
  253. .then( newEditor => {
  254. editor = newEditor;
  255. } );
  256. } );
  257. test( () => editorData );
  258. } );
  259. describe( 'editor with editable element', () => {
  260. let editableElement;
  261. beforeEach( function() {
  262. editableElement = document.createElement( 'div' );
  263. editableElement.innerHTML = editorData;
  264. return DecoupledEditor
  265. .create( editableElement, { plugins: [ Paragraph ] } )
  266. .then( newEditor => {
  267. editor = newEditor;
  268. } );
  269. } );
  270. it( 'sets data back to the element', () => {
  271. editor.setData( '<p>foo</p>' );
  272. return editor.destroy()
  273. .then( () => {
  274. expect( editableElement.innerHTML ).to.equal( '<p>foo</p>' );
  275. } );
  276. } );
  277. test( () => editableElement );
  278. } );
  279. function test() {
  280. it( 'destroys the UI', () => {
  281. const spy = sinon.spy( editor.ui, 'destroy' );
  282. return editor.destroy()
  283. .then( () => {
  284. sinon.assert.calledOnce( spy );
  285. } );
  286. } );
  287. }
  288. } );
  289. describeMemoryUsage( () => {
  290. testMemoryUsage(
  291. 'should not grow on multiple create/destroy',
  292. () => DecoupledEditor
  293. .create( document.querySelector( '#mem-editor' ), {
  294. plugins: [ ArticlePluginSet ],
  295. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  296. image: {
  297. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  298. }
  299. } ) );
  300. } );
  301. } );