8
0

classiceditor.js 8.3 KB

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