classiceditor.js 9.6 KB

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