decouplededitor.js 11 KB

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