classiceditor.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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, 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. import { removeEditorBodyOrphans } from '@ckeditor/ckeditor5-core/tests/_utils/cleanup';
  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( console, '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. describe( 'automatic toolbar items groupping', () => {
  81. it( 'should be on by default', () => {
  82. const editorElement = document.createElement( 'div' );
  83. const editor = new ClassicEditor( editorElement );
  84. expect( editor.ui.view.toolbar.options.shouldGroupWhenFull ).to.be.true;
  85. editorElement.remove();
  86. } );
  87. it( 'can be disabled using config.toolbar.shouldNotGroupWhenFull', () => {
  88. const editorElement = document.createElement( 'div' );
  89. const editor = new ClassicEditor( editorElement, {
  90. toolbar: {
  91. shouldNotGroupWhenFull: true
  92. }
  93. } );
  94. expect( editor.ui.view.toolbar.options.shouldGroupWhenFull ).to.be.false;
  95. editorElement.remove();
  96. } );
  97. } );
  98. } );
  99. } );
  100. describe( 'create()', () => {
  101. beforeEach( () => {
  102. return ClassicEditor
  103. .create( editorElement, {
  104. plugins: [ Paragraph, Bold ]
  105. } )
  106. .then( newEditor => {
  107. editor = newEditor;
  108. } );
  109. } );
  110. afterEach( () => {
  111. return editor.destroy();
  112. } );
  113. it( 'creates an instance which inherits from the ClassicEditor', () => {
  114. expect( editor ).to.be.instanceof( ClassicEditor );
  115. } );
  116. it( 'loads data from the editor element', () => {
  117. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  118. } );
  119. // #53
  120. it( 'creates an instance of a ClassicEditor child class', () => {
  121. class CustomClassicEditor extends ClassicEditor {}
  122. return CustomClassicEditor
  123. .create( editorElement, {
  124. plugins: [ Paragraph, Bold ]
  125. } )
  126. .then( newEditor => {
  127. expect( newEditor ).to.be.instanceof( CustomClassicEditor );
  128. expect( newEditor ).to.be.instanceof( ClassicEditor );
  129. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  130. return newEditor.destroy();
  131. } );
  132. } );
  133. it( 'should not require config object', () => {
  134. // Just being safe with `builtinPlugins` static property.
  135. class CustomClassicEditor extends ClassicEditor {}
  136. CustomClassicEditor.builtinPlugins = [ Paragraph, Bold ];
  137. return CustomClassicEditor.create( editorElement )
  138. .then( newEditor => {
  139. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  140. return newEditor.destroy();
  141. } );
  142. } );
  143. it( 'allows to pass data to the constructor', () => {
  144. return ClassicEditor.create( '<p>Hello world!</p>', {
  145. plugins: [ Paragraph ]
  146. } ).then( editor => {
  147. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  148. editor.destroy();
  149. } );
  150. } );
  151. it( 'initializes with config.initialData', () => {
  152. return ClassicEditor.create( editorElement, {
  153. initialData: '<p>Hello world!</p>',
  154. plugins: [ Paragraph ]
  155. } ).then( editor => {
  156. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  157. editor.destroy();
  158. } );
  159. } );
  160. it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => {
  161. ClassicEditor.create( '<p>Hello world!</p>', {
  162. initialData: '<p>I am evil!</p>',
  163. plugins: [ Paragraph ]
  164. } ).catch( () => {
  165. removeEditorBodyOrphans();
  166. done();
  167. } );
  168. } );
  169. it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
  170. return ClassicEditor
  171. .create( '<p>Foo.</p>', {
  172. plugins: [ Paragraph, Bold ]
  173. } )
  174. .then( newEditor => {
  175. expect( newEditor.sourceElement ).to.be.undefined;
  176. return newEditor.destroy();
  177. } );
  178. } );
  179. describe( 'ui', () => {
  180. it( 'inserts editor UI next to editor element', () => {
  181. expect( editor.ui.view.element.previousSibling ).to.equal( editorElement );
  182. } );
  183. it( 'attaches editable UI as view\'s DOM root', () => {
  184. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  185. } );
  186. } );
  187. } );
  188. describe( 'create - events', () => {
  189. afterEach( () => {
  190. return editor.destroy();
  191. } );
  192. it( 'fires all events in the right order', () => {
  193. const fired = [];
  194. function spy( evt ) {
  195. fired.push( `${ evt.name }-${ evt.source.constructor.name.toLowerCase() }` );
  196. }
  197. class EventWatcher extends Plugin {
  198. init() {
  199. this.editor.ui.on( 'ready', spy );
  200. this.editor.data.on( 'ready', spy );
  201. this.editor.on( 'ready', spy );
  202. }
  203. }
  204. return ClassicEditor
  205. .create( editorElement, {
  206. plugins: [ EventWatcher ]
  207. } )
  208. .then( newEditor => {
  209. expect( fired ).to.deep.equal(
  210. [ 'ready-classiceditorui', 'ready-datacontroller', 'ready-classiceditor' ] );
  211. editor = newEditor;
  212. } );
  213. } );
  214. it( 'fires ready once UI is rendered', () => {
  215. let isReady;
  216. class EventWatcher extends Plugin {
  217. init() {
  218. this.editor.ui.on( 'ready', () => {
  219. isReady = this.editor.ui.view.isRendered;
  220. } );
  221. }
  222. }
  223. return ClassicEditor
  224. .create( editorElement, {
  225. plugins: [ EventWatcher ]
  226. } )
  227. .then( newEditor => {
  228. expect( isReady ).to.be.true;
  229. editor = newEditor;
  230. } );
  231. } );
  232. } );
  233. describe( 'destroy', () => {
  234. beforeEach( function() {
  235. return ClassicEditor
  236. .create( editorElement, { plugins: [ Paragraph ] } )
  237. .then( newEditor => {
  238. editor = newEditor;
  239. } );
  240. } );
  241. it( 'sets the data back to the editor element', () => {
  242. editor.setData( '<p>foo</p>' );
  243. return editor.destroy()
  244. .then( () => {
  245. expect( editorElement.innerHTML ).to.equal( '<p>foo</p>' );
  246. } );
  247. } );
  248. it( 'does not update the source element if editor was initialized with data', async () => {
  249. await editor.destroy();
  250. return ClassicEditor
  251. .create( '<p>Foo.</p>', {
  252. plugins: [ Paragraph, Bold ]
  253. } )
  254. .then( newEditor => {
  255. const spy = sinon.stub( newEditor, 'updateSourceElement' );
  256. return newEditor.destroy()
  257. .then( () => {
  258. expect( spy.called ).to.be.false;
  259. spy.restore();
  260. } );
  261. } );
  262. } );
  263. it( 'restores the editor element', () => {
  264. expect( editor.sourceElement.style.display ).to.equal( 'none' );
  265. return editor.destroy()
  266. .then( () => {
  267. expect( editor.sourceElement.style.display ).to.equal( '' );
  268. } );
  269. } );
  270. } );
  271. describeMemoryUsage( () => {
  272. testMemoryUsage(
  273. 'should not grow on multiple create/destroy',
  274. () => ClassicEditor
  275. .create( document.querySelector( '#mem-editor' ), {
  276. plugins: [ ArticlePluginSet ],
  277. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  278. image: {
  279. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  280. }
  281. } ) );
  282. } );
  283. } );