classiceditor.js 6.5 KB

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