decouplededitor.js 11 KB

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