8
0

classiceditor.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. expect( testUtils.isMixed( ClassicEditor, DataApiMixin ) ).to.true;
  37. } );
  38. it( 'has a Element Interface', () => {
  39. expect( testUtils.isMixed( ClassicEditor, ElementApiMixin ) ).to.true;
  40. } );
  41. it( 'creates main root element', () => {
  42. expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
  43. } );
  44. it( 'contains the source element as #sourceElement property', () => {
  45. expect( editor.sourceElement ).to.equal( editorElement );
  46. } );
  47. it( 'handles form element', () => {
  48. const form = document.createElement( 'form' );
  49. const textarea = document.createElement( 'textarea' );
  50. form.appendChild( textarea );
  51. document.body.appendChild( form );
  52. // Prevents page realods in Firefox ;|
  53. form.addEventListener( 'submit', evt => {
  54. evt.preventDefault();
  55. } );
  56. return ClassicEditor.create( textarea, {
  57. plugins: [ Paragraph ]
  58. } ).then( editor => {
  59. expect( textarea.value ).to.equal( '' );
  60. editor.setData( '<p>Foo</p>' );
  61. form.dispatchEvent( new Event( 'submit', {
  62. // We need to be able to do preventDefault() to prevent page reloads in Firefox.
  63. cancelable: true
  64. } ) );
  65. expect( textarea.value ).to.equal( '<p>Foo</p>' );
  66. return editor.destroy().then( () => {
  67. form.remove();
  68. } );
  69. } );
  70. } );
  71. it( 'allows to pass data to the constructor', () => {
  72. return ClassicEditor.create( '<p>Hello world!</p>', {
  73. plugins: [ Paragraph ]
  74. } ).then( editor => {
  75. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  76. } );
  77. } );
  78. describe( 'ui', () => {
  79. it( 'creates the UI using BoxedEditorUI classes', () => {
  80. expect( editor.ui ).to.be.instanceof( ClassicEditorUI );
  81. expect( editor.ui.view ).to.be.instanceof( ClassicEditorUIView );
  82. } );
  83. } );
  84. } );
  85. describe( 'create()', () => {
  86. beforeEach( () => {
  87. return ClassicEditor
  88. .create( editorElement, {
  89. plugins: [ Paragraph, Bold ]
  90. } )
  91. .then( newEditor => {
  92. editor = newEditor;
  93. } );
  94. } );
  95. afterEach( () => {
  96. return editor.destroy();
  97. } );
  98. it( 'creates an instance which inherits from the ClassicEditor', () => {
  99. expect( editor ).to.be.instanceof( ClassicEditor );
  100. } );
  101. it( 'loads data from the editor element', () => {
  102. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  103. } );
  104. // #53
  105. it( 'creates an instance of a ClassicEditor child class', () => {
  106. class CustomClassicEditor extends ClassicEditor {}
  107. return CustomClassicEditor
  108. .create( editorElement, {
  109. plugins: [ Paragraph, Bold ]
  110. } )
  111. .then( newEditor => {
  112. expect( newEditor ).to.be.instanceof( CustomClassicEditor );
  113. expect( newEditor ).to.be.instanceof( ClassicEditor );
  114. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  115. return newEditor.destroy();
  116. } );
  117. } );
  118. it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
  119. return ClassicEditor
  120. .create( '<p>Foo.</p>', {
  121. plugins: [ Paragraph, Bold ]
  122. } )
  123. .then( newEditor => {
  124. expect( newEditor.sourceElement ).to.be.undefined;
  125. return newEditor.destroy();
  126. } );
  127. } );
  128. describe( 'ui', () => {
  129. it( 'inserts editor UI next to editor element', () => {
  130. expect( editor.ui.view.element.previousSibling ).to.equal( editorElement );
  131. } );
  132. it( 'attaches editable UI as view\'s DOM root', () => {
  133. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  134. } );
  135. it( 'editor.element points to the editor\'s UI when editor was initialized on the DOM element', () => {
  136. expect( editor.element ).to.equal( editor.ui.view.element );
  137. } );
  138. it( 'editor.element points to the editor\'s UI when editor was initialized with data', () => {
  139. return ClassicEditor.create( '<p>Hello world!</p>', {
  140. plugins: [ Paragraph ]
  141. } ).then( editor => {
  142. expect( editor.element ).to.equal( editor.ui.view.element );
  143. return editor.destroy();
  144. } );
  145. } );
  146. } );
  147. } );
  148. describe( 'create - events', () => {
  149. afterEach( () => {
  150. return editor.destroy();
  151. } );
  152. it( 'fires all events in the right order', () => {
  153. const fired = [];
  154. function spy( evt ) {
  155. fired.push( evt.name );
  156. }
  157. class EventWatcher extends Plugin {
  158. init() {
  159. this.editor.on( 'pluginsReady', spy );
  160. this.editor.on( 'uiReady', spy );
  161. this.editor.on( 'dataReady', spy );
  162. this.editor.on( 'ready', spy );
  163. }
  164. }
  165. return ClassicEditor
  166. .create( editorElement, {
  167. plugins: [ EventWatcher ]
  168. } )
  169. .then( newEditor => {
  170. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  171. editor = newEditor;
  172. } );
  173. } );
  174. it( 'fires dataReady once data is loaded', () => {
  175. let data;
  176. class EventWatcher extends Plugin {
  177. init() {
  178. this.editor.on( 'dataReady', () => {
  179. data = this.editor.getData();
  180. } );
  181. }
  182. }
  183. return ClassicEditor
  184. .create( editorElement, {
  185. plugins: [ EventWatcher, Paragraph, Bold ]
  186. } )
  187. .then( newEditor => {
  188. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  189. editor = newEditor;
  190. } );
  191. } );
  192. it( 'fires uiReady once UI is rendered', () => {
  193. let isReady;
  194. class EventWatcher extends Plugin {
  195. init() {
  196. this.editor.on( 'uiReady', () => {
  197. isReady = this.editor.ui.view.isRendered;
  198. } );
  199. }
  200. }
  201. return ClassicEditor
  202. .create( editorElement, {
  203. plugins: [ EventWatcher ]
  204. } )
  205. .then( newEditor => {
  206. expect( isReady ).to.be.true;
  207. editor = newEditor;
  208. } );
  209. } );
  210. } );
  211. describe( 'destroy', () => {
  212. beforeEach( function() {
  213. return ClassicEditor
  214. .create( editorElement, { plugins: [ Paragraph ] } )
  215. .then( newEditor => {
  216. editor = newEditor;
  217. } );
  218. } );
  219. it( 'sets the data back to the editor element', () => {
  220. editor.setData( '<p>foo</p>' );
  221. return editor.destroy()
  222. .then( () => {
  223. expect( editorElement.innerHTML ).to.equal( '<p>foo</p>' );
  224. } );
  225. } );
  226. it( 'does not update the source element if editor was initialized with data', () => {
  227. return ClassicEditor
  228. .create( '<p>Foo.</p>', {
  229. plugins: [ Paragraph, Bold ]
  230. } )
  231. .then( newEditor => {
  232. const spy = sinon.stub( newEditor, 'updateSourceElement' );
  233. return newEditor.destroy()
  234. .then( () => {
  235. expect( spy.called ).to.be.false;
  236. spy.restore();
  237. } );
  238. } );
  239. } );
  240. it( 'restores the editor element', () => {
  241. expect( editor.sourceElement.style.display ).to.equal( 'none' );
  242. return editor.destroy()
  243. .then( () => {
  244. expect( editor.sourceElement.style.display ).to.equal( '' );
  245. } );
  246. } );
  247. } );
  248. } );