8
0

decouplededitor.js 11 KB

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