classiceditor.js 8.9 KB

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