classiceditor.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 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. } );
  27. afterEach( () => {
  28. editorElement.remove();
  29. } );
  30. describe( 'constructor()', () => {
  31. beforeEach( () => {
  32. editor = new ClassicEditor( editorElement );
  33. } );
  34. it( 'uses HTMLDataProcessor', () => {
  35. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  36. } );
  37. it( 'has a Data Interface', () => {
  38. expect( testUtils.isMixed( ClassicEditor, DataApiMixin ) ).to.true;
  39. } );
  40. it( 'has a Element Interface', () => {
  41. expect( testUtils.isMixed( ClassicEditor, ElementApiMixin ) ).to.true;
  42. } );
  43. it( 'creates main root element', () => {
  44. expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
  45. } );
  46. it( 'contains the source element as #sourceElement property', () => {
  47. expect( editor.sourceElement ).to.equal( editorElement );
  48. } );
  49. it( 'handles form element', () => {
  50. const form = document.createElement( 'form' );
  51. const textarea = document.createElement( 'textarea' );
  52. form.appendChild( textarea );
  53. document.body.appendChild( form );
  54. // Prevents page realods in Firefox ;|
  55. form.addEventListener( 'submit', evt => {
  56. evt.preventDefault();
  57. } );
  58. return ClassicEditor.create( textarea, {
  59. plugins: [ Paragraph ]
  60. } ).then( editor => {
  61. expect( textarea.value ).to.equal( '' );
  62. editor.setData( '<p>Foo</p>' );
  63. form.dispatchEvent( new Event( 'submit', {
  64. // We need to be able to do preventDefault() to prevent page reloads in Firefox.
  65. cancelable: true
  66. } ) );
  67. expect( textarea.value ).to.equal( '<p>Foo</p>' );
  68. return editor.destroy().then( () => {
  69. form.remove();
  70. } );
  71. } );
  72. } );
  73. it( 'allows to pass data to the constructor', () => {
  74. return ClassicEditor.create( '<p>Hello world!</p>', {
  75. plugins: [ Paragraph ]
  76. } ).then( editor => {
  77. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  78. } );
  79. } );
  80. describe( 'ui', () => {
  81. it( 'creates the UI using BoxedEditorUI classes', () => {
  82. expect( editor.ui ).to.be.instanceof( ClassicEditorUI );
  83. expect( editor.ui.view ).to.be.instanceof( ClassicEditorUIView );
  84. } );
  85. } );
  86. } );
  87. describe( 'create()', () => {
  88. beforeEach( () => {
  89. return ClassicEditor
  90. .create( editorElement, {
  91. plugins: [ Paragraph, Bold ]
  92. } )
  93. .then( newEditor => {
  94. editor = newEditor;
  95. } );
  96. } );
  97. afterEach( () => {
  98. return editor.destroy();
  99. } );
  100. it( 'creates an instance which inherits from the ClassicEditor', () => {
  101. expect( editor ).to.be.instanceof( ClassicEditor );
  102. } );
  103. it( 'loads data from the editor element', () => {
  104. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  105. } );
  106. // #53
  107. it( 'creates an instance of a ClassicEditor child class', () => {
  108. class CustomClassicEditor extends ClassicEditor {}
  109. return CustomClassicEditor
  110. .create( editorElement, {
  111. plugins: [ Paragraph, Bold ]
  112. } )
  113. .then( newEditor => {
  114. expect( newEditor ).to.be.instanceof( CustomClassicEditor );
  115. expect( newEditor ).to.be.instanceof( ClassicEditor );
  116. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  117. return newEditor.destroy();
  118. } );
  119. } );
  120. it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
  121. return ClassicEditor
  122. .create( '<p>Foo.</p>', {
  123. plugins: [ Paragraph, Bold ]
  124. } )
  125. .then( newEditor => {
  126. expect( newEditor.sourceElement ).to.be.undefined;
  127. return newEditor.destroy();
  128. } );
  129. } );
  130. describe( 'ui', () => {
  131. it( 'inserts editor UI next to editor element', () => {
  132. expect( editor.ui.view.element.previousSibling ).to.equal( editorElement );
  133. } );
  134. it( 'attaches editable UI as view\'s DOM root', () => {
  135. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  136. } );
  137. it( 'editor.element points to the editor\'s UI when editor was initialized on the DOM element', () => {
  138. expect( editor.element ).to.equal( editor.ui.view.element );
  139. } );
  140. it( 'editor.element points to the editor\'s UI when editor was initialized with data', () => {
  141. return ClassicEditor.create( '<p>Hello world!</p>', {
  142. plugins: [ Paragraph ]
  143. } ).then( editor => {
  144. expect( editor.element ).to.equal( editor.ui.view.element );
  145. return editor.destroy();
  146. } );
  147. } );
  148. } );
  149. } );
  150. describe( 'create - events', () => {
  151. afterEach( () => {
  152. return editor.destroy();
  153. } );
  154. it( 'fires all events in the right order', () => {
  155. const fired = [];
  156. function spy( evt ) {
  157. fired.push( evt.name );
  158. }
  159. class EventWatcher extends Plugin {
  160. init() {
  161. this.editor.on( 'pluginsReady', spy );
  162. this.editor.on( 'uiReady', spy );
  163. this.editor.on( 'dataReady', spy );
  164. this.editor.on( 'ready', spy );
  165. }
  166. }
  167. return ClassicEditor
  168. .create( editorElement, {
  169. plugins: [ EventWatcher ]
  170. } )
  171. .then( newEditor => {
  172. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  173. editor = newEditor;
  174. } );
  175. } );
  176. it( 'fires dataReady once data is loaded', () => {
  177. let data;
  178. class EventWatcher extends Plugin {
  179. init() {
  180. this.editor.on( 'dataReady', () => {
  181. data = this.editor.getData();
  182. } );
  183. }
  184. }
  185. return ClassicEditor
  186. .create( editorElement, {
  187. plugins: [ EventWatcher, Paragraph, Bold ]
  188. } )
  189. .then( newEditor => {
  190. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  191. editor = newEditor;
  192. } );
  193. } );
  194. it( 'fires uiReady once UI is rendered', () => {
  195. let isReady;
  196. class EventWatcher extends Plugin {
  197. init() {
  198. this.editor.on( 'uiReady', () => {
  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. } );