classiceditor.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 log from '@ckeditor/ckeditor5-utils/src/log';
  18. describe( 'ClassicEditor', () => {
  19. let editor, editorElement;
  20. testUtils.createSinonSandbox();
  21. beforeEach( () => {
  22. editorElement = document.createElement( 'div' );
  23. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  24. document.body.appendChild( editorElement );
  25. testUtils.sinon.stub( log, 'warn' ).callsFake( () => {} );
  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. } );
  138. } );
  139. describe( 'create - events', () => {
  140. afterEach( () => {
  141. return editor.destroy();
  142. } );
  143. it( 'fires all events in the right order', () => {
  144. const fired = [];
  145. function spy( evt ) {
  146. fired.push( evt.name );
  147. }
  148. class EventWatcher extends Plugin {
  149. init() {
  150. this.editor.on( 'pluginsReady', spy );
  151. this.editor.ui.on( 'ready', spy );
  152. this.editor.on( 'dataReady', spy );
  153. this.editor.on( 'ready', spy );
  154. }
  155. }
  156. return ClassicEditor
  157. .create( editorElement, {
  158. plugins: [ EventWatcher ]
  159. } )
  160. .then( newEditor => {
  161. expect( fired ).to.deep.equal( [ 'pluginsReady', 'ready', 'dataReady', 'ready' ] );
  162. editor = newEditor;
  163. } );
  164. } );
  165. it( 'fires dataReady once data is loaded', () => {
  166. let data;
  167. class EventWatcher extends Plugin {
  168. init() {
  169. this.editor.on( 'dataReady', () => {
  170. data = this.editor.getData();
  171. } );
  172. }
  173. }
  174. return ClassicEditor
  175. .create( editorElement, {
  176. plugins: [ EventWatcher, Paragraph, Bold ]
  177. } )
  178. .then( newEditor => {
  179. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  180. editor = newEditor;
  181. } );
  182. } );
  183. it( 'fires ready once UI is rendered', () => {
  184. let isReady;
  185. class EventWatcher extends Plugin {
  186. init() {
  187. this.editor.ui.on( 'ready', () => {
  188. isReady = this.editor.ui.view.isRendered;
  189. } );
  190. }
  191. }
  192. return ClassicEditor
  193. .create( editorElement, {
  194. plugins: [ EventWatcher ]
  195. } )
  196. .then( newEditor => {
  197. expect( isReady ).to.be.true;
  198. editor = newEditor;
  199. } );
  200. } );
  201. } );
  202. describe( 'destroy', () => {
  203. beforeEach( function() {
  204. return ClassicEditor
  205. .create( editorElement, { plugins: [ Paragraph ] } )
  206. .then( newEditor => {
  207. editor = newEditor;
  208. } );
  209. } );
  210. it( 'sets the data back to the editor element', () => {
  211. editor.setData( '<p>foo</p>' );
  212. return editor.destroy()
  213. .then( () => {
  214. expect( editorElement.innerHTML ).to.equal( '<p>foo</p>' );
  215. } );
  216. } );
  217. it( 'does not update the source element if editor was initialized with data', () => {
  218. return ClassicEditor
  219. .create( '<p>Foo.</p>', {
  220. plugins: [ Paragraph, Bold ]
  221. } )
  222. .then( newEditor => {
  223. const spy = sinon.stub( newEditor, 'updateSourceElement' );
  224. return newEditor.destroy()
  225. .then( () => {
  226. expect( spy.called ).to.be.false;
  227. spy.restore();
  228. } );
  229. } );
  230. } );
  231. it( 'restores the editor element', () => {
  232. expect( editor.sourceElement.style.display ).to.equal( 'none' );
  233. return editor.destroy()
  234. .then( () => {
  235. expect( editor.sourceElement.style.display ).to.equal( '' );
  236. } );
  237. } );
  238. } );
  239. } );