classiceditor.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. it( 'allows to pass data to the constructor', () => {
  69. return ClassicEditor.create( '<p>Hello world!</p>', {
  70. plugins: [ Paragraph ]
  71. } ).then( editor => {
  72. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  73. } );
  74. } );
  75. it( 'editor.element should be equal to editor.ui.view.element when data is passed', () => {
  76. return ClassicEditor.create( '<p>Hello world!</p>', {
  77. plugins: [ Paragraph ]
  78. } ).then( editor => {
  79. expect( editor.element ).to.equal( editor.ui.view.element );
  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. describe( 'ui', () => {
  123. it( 'inserts editor UI next to editor element', () => {
  124. expect( editor.ui.view.element.previousSibling ).to.equal( editorElement );
  125. } );
  126. it( 'attaches editable UI as view\'s DOM root', () => {
  127. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  128. } );
  129. } );
  130. } );
  131. describe( 'create - events', () => {
  132. afterEach( () => {
  133. return editor.destroy();
  134. } );
  135. it( 'fires all events in the right order', () => {
  136. const fired = [];
  137. function spy( evt ) {
  138. fired.push( evt.name );
  139. }
  140. class EventWatcher extends Plugin {
  141. init() {
  142. this.editor.on( 'pluginsReady', spy );
  143. this.editor.on( 'uiReady', spy );
  144. this.editor.on( 'dataReady', spy );
  145. this.editor.on( 'ready', spy );
  146. }
  147. }
  148. return ClassicEditor
  149. .create( editorElement, {
  150. plugins: [ EventWatcher ]
  151. } )
  152. .then( newEditor => {
  153. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  154. editor = newEditor;
  155. } );
  156. } );
  157. it( 'fires dataReady once data is loaded', () => {
  158. let data;
  159. class EventWatcher extends Plugin {
  160. init() {
  161. this.editor.on( 'dataReady', () => {
  162. data = this.editor.getData();
  163. } );
  164. }
  165. }
  166. return ClassicEditor
  167. .create( editorElement, {
  168. plugins: [ EventWatcher, Paragraph, Bold ]
  169. } )
  170. .then( newEditor => {
  171. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  172. editor = newEditor;
  173. } );
  174. } );
  175. it( 'fires uiReady once UI is rendered', () => {
  176. let isReady;
  177. class EventWatcher extends Plugin {
  178. init() {
  179. this.editor.on( 'uiReady', () => {
  180. isReady = this.editor.ui.view.isRendered;
  181. } );
  182. }
  183. }
  184. return ClassicEditor
  185. .create( editorElement, {
  186. plugins: [ EventWatcher ]
  187. } )
  188. .then( newEditor => {
  189. expect( isReady ).to.be.true;
  190. editor = newEditor;
  191. } );
  192. } );
  193. } );
  194. describe( 'destroy', () => {
  195. beforeEach( function() {
  196. return ClassicEditor
  197. .create( editorElement, { plugins: [ Paragraph ] } )
  198. .then( newEditor => {
  199. editor = newEditor;
  200. } );
  201. } );
  202. it( 'sets the data back to the editor element', () => {
  203. editor.setData( '<p>foo</p>' );
  204. return editor.destroy()
  205. .then( () => {
  206. expect( editorElement.innerHTML ).to.equal( '<p>foo</p>' );
  207. } );
  208. } );
  209. it( 'restores the editor element', () => {
  210. expect( editor.element.style.display ).to.equal( 'none' );
  211. return editor.destroy()
  212. .then( () => {
  213. expect( editor.element.style.display ).to.equal( '' );
  214. } );
  215. } );
  216. } );
  217. } );