decouplededitor.js 11 KB

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