classiceditor.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, Event */
  6. import ClassicEditorUI from '../src/classiceditorui';
  7. import ClassicEditorUIView from '../src/classiceditoruiview';
  8. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  9. import ClassicEditor from '../src/classiceditor';
  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 log from '@ckeditor/ckeditor5-utils/src/log';
  18. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  19. import { describeMemoryUsage, testMemoryUsage } from '@ckeditor/ckeditor5-core/tests/_utils/memory';
  20. describe( 'ClassicEditor', () => {
  21. let editor, editorElement;
  22. testUtils.createSinonSandbox();
  23. beforeEach( () => {
  24. editorElement = document.createElement( 'div' );
  25. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  26. document.body.appendChild( editorElement );
  27. testUtils.sinon.stub( log, 'warn' ).callsFake( () => {} );
  28. } );
  29. afterEach( () => {
  30. editorElement.remove();
  31. } );
  32. describe( 'constructor()', () => {
  33. beforeEach( () => {
  34. editor = new ClassicEditor( editorElement );
  35. } );
  36. it( 'uses HTMLDataProcessor', () => {
  37. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  38. } );
  39. it( 'has a Data Interface', () => {
  40. expect( testUtils.isMixed( ClassicEditor, DataApiMixin ) ).to.true;
  41. } );
  42. it( 'has a Element Interface', () => {
  43. expect( testUtils.isMixed( ClassicEditor, ElementApiMixin ) ).to.true;
  44. } );
  45. it( 'creates main root element', () => {
  46. expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
  47. } );
  48. it( 'contains the source element as #sourceElement property', () => {
  49. expect( editor.sourceElement ).to.equal( editorElement );
  50. } );
  51. it( 'handles form element', () => {
  52. const form = document.createElement( 'form' );
  53. const textarea = document.createElement( 'textarea' );
  54. form.appendChild( textarea );
  55. document.body.appendChild( form );
  56. // Prevents page realods in Firefox ;|
  57. form.addEventListener( 'submit', evt => {
  58. evt.preventDefault();
  59. } );
  60. return ClassicEditor.create( textarea, {
  61. plugins: [ Paragraph ]
  62. } ).then( editor => {
  63. expect( textarea.value ).to.equal( '' );
  64. editor.setData( '<p>Foo</p>' );
  65. form.dispatchEvent( new Event( 'submit', {
  66. // We need to be able to do preventDefault() to prevent page reloads in Firefox.
  67. cancelable: true
  68. } ) );
  69. expect( textarea.value ).to.equal( '<p>Foo</p>' );
  70. return editor.destroy().then( () => {
  71. form.remove();
  72. } );
  73. } );
  74. } );
  75. describe( 'ui', () => {
  76. it( 'creates the UI using BoxedEditorUI classes', () => {
  77. expect( editor.ui ).to.be.instanceof( ClassicEditorUI );
  78. expect( editor.ui.view ).to.be.instanceof( ClassicEditorUIView );
  79. } );
  80. } );
  81. } );
  82. describe( 'create()', () => {
  83. beforeEach( () => {
  84. return ClassicEditor
  85. .create( editorElement, {
  86. plugins: [ Paragraph, Bold ]
  87. } )
  88. .then( newEditor => {
  89. editor = newEditor;
  90. } );
  91. } );
  92. afterEach( () => {
  93. return editor.destroy();
  94. } );
  95. it( 'creates an instance which inherits from the ClassicEditor', () => {
  96. expect( editor ).to.be.instanceof( ClassicEditor );
  97. } );
  98. it( 'loads data from the editor element', () => {
  99. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  100. } );
  101. // #53
  102. it( 'creates an instance of a ClassicEditor child class', () => {
  103. class CustomClassicEditor extends ClassicEditor {}
  104. return CustomClassicEditor
  105. .create( editorElement, {
  106. plugins: [ Paragraph, Bold ]
  107. } )
  108. .then( newEditor => {
  109. expect( newEditor ).to.be.instanceof( CustomClassicEditor );
  110. expect( newEditor ).to.be.instanceof( ClassicEditor );
  111. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  112. return newEditor.destroy();
  113. } );
  114. } );
  115. it( 'should not require config object', () => {
  116. // Just being safe with `builtinPlugins` static property.
  117. class CustomClassicEditor extends ClassicEditor {}
  118. CustomClassicEditor.builtinPlugins = [ Paragraph, Bold ];
  119. return CustomClassicEditor.create( editorElement )
  120. .then( newEditor => {
  121. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  122. return newEditor.destroy();
  123. } );
  124. } );
  125. it( 'allows to pass data to the constructor', () => {
  126. return ClassicEditor.create( '<p>Hello world!</p>', {
  127. plugins: [ Paragraph ]
  128. } ).then( editor => {
  129. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  130. editor.destroy();
  131. } );
  132. } );
  133. it( 'initializes with config.initialData', () => {
  134. return ClassicEditor.create( editorElement, {
  135. initialData: '<p>Hello world!</p>',
  136. plugins: [ Paragraph ]
  137. } ).then( editor => {
  138. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  139. editor.destroy();
  140. } );
  141. } );
  142. it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => {
  143. ClassicEditor.create( '<p>Hello world!</p>', {
  144. initialData: '<p>I am evil!</p>',
  145. plugins: [ Paragraph ]
  146. } ).catch( () => {
  147. done();
  148. } );
  149. } );
  150. it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
  151. return ClassicEditor
  152. .create( '<p>Foo.</p>', {
  153. plugins: [ Paragraph, Bold ]
  154. } )
  155. .then( newEditor => {
  156. expect( newEditor.sourceElement ).to.be.undefined;
  157. return newEditor.destroy();
  158. } );
  159. } );
  160. describe( 'ui', () => {
  161. it( 'inserts editor UI next to editor element', () => {
  162. expect( editor.ui.view.element.previousSibling ).to.equal( editorElement );
  163. } );
  164. it( 'attaches editable UI as view\'s DOM root', () => {
  165. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  166. } );
  167. } );
  168. } );
  169. describe( 'create - events', () => {
  170. afterEach( () => {
  171. return editor.destroy();
  172. } );
  173. it( 'fires all events in the right order', () => {
  174. const fired = [];
  175. function spy( evt ) {
  176. fired.push( `${ evt.name }-${ evt.source.constructor.name.toLowerCase() }` );
  177. }
  178. class EventWatcher extends Plugin {
  179. init() {
  180. this.editor.ui.on( 'ready', spy );
  181. this.editor.data.on( 'ready', spy );
  182. this.editor.on( 'ready', spy );
  183. }
  184. }
  185. return ClassicEditor
  186. .create( editorElement, {
  187. plugins: [ EventWatcher ]
  188. } )
  189. .then( newEditor => {
  190. expect( fired ).to.deep.equal(
  191. [ 'ready-classiceditorui', 'ready-datacontroller', 'ready-classiceditor' ] );
  192. editor = newEditor;
  193. } );
  194. } );
  195. it( 'fires ready once UI is rendered', () => {
  196. let isReady;
  197. class EventWatcher extends Plugin {
  198. init() {
  199. this.editor.ui.on( 'ready', () => {
  200. isReady = this.editor.ui.view.isRendered;
  201. } );
  202. }
  203. }
  204. return ClassicEditor
  205. .create( editorElement, {
  206. plugins: [ EventWatcher ]
  207. } )
  208. .then( newEditor => {
  209. expect( isReady ).to.be.true;
  210. editor = newEditor;
  211. } );
  212. } );
  213. } );
  214. describe( 'destroy', () => {
  215. beforeEach( function() {
  216. return ClassicEditor
  217. .create( editorElement, { plugins: [ Paragraph ] } )
  218. .then( newEditor => {
  219. editor = newEditor;
  220. } );
  221. } );
  222. it( 'sets the data back to the editor element', () => {
  223. editor.setData( '<p>foo</p>' );
  224. return editor.destroy()
  225. .then( () => {
  226. expect( editorElement.innerHTML ).to.equal( '<p>foo</p>' );
  227. } );
  228. } );
  229. it( 'does not update the source element if editor was initialized with data', () => {
  230. return ClassicEditor
  231. .create( '<p>Foo.</p>', {
  232. plugins: [ Paragraph, Bold ]
  233. } )
  234. .then( newEditor => {
  235. const spy = sinon.stub( newEditor, 'updateSourceElement' );
  236. return newEditor.destroy()
  237. .then( () => {
  238. expect( spy.called ).to.be.false;
  239. spy.restore();
  240. } );
  241. } );
  242. } );
  243. it( 'restores the editor element', () => {
  244. expect( editor.sourceElement.style.display ).to.equal( 'none' );
  245. return editor.destroy()
  246. .then( () => {
  247. expect( editor.sourceElement.style.display ).to.equal( '' );
  248. } );
  249. } );
  250. } );
  251. describeMemoryUsage( () => {
  252. testMemoryUsage(
  253. 'should not grow on multiple create/destroy',
  254. () => ClassicEditor
  255. .create( document.querySelector( '#mem-editor' ), {
  256. plugins: [ ArticlePluginSet ],
  257. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  258. image: {
  259. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  260. }
  261. } ) );
  262. } );
  263. } );