decouplededitor.js 11 KB

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