8
0

classiceditor.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. 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.ui.on( 'ready', spy );
  163. this.editor.on( 'uiReady', spy );
  164. this.editor.on( 'dataReady', spy );
  165. this.editor.on( 'ready', spy );
  166. }
  167. }
  168. return ClassicEditor
  169. .create( editorElement, {
  170. plugins: [ EventWatcher ]
  171. } )
  172. .then( newEditor => {
  173. expect( fired ).to.deep.equal( [ 'pluginsReady', 'ready', 'uiReady', 'dataReady', 'ready' ] );
  174. editor = newEditor;
  175. } );
  176. } );
  177. it( 'fires dataReady once data is loaded', () => {
  178. let data;
  179. class EventWatcher extends Plugin {
  180. init() {
  181. this.editor.on( 'dataReady', () => {
  182. data = this.editor.getData();
  183. } );
  184. }
  185. }
  186. return ClassicEditor
  187. .create( editorElement, {
  188. plugins: [ EventWatcher, Paragraph, Bold ]
  189. } )
  190. .then( newEditor => {
  191. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  192. editor = newEditor;
  193. } );
  194. } );
  195. it( 'fires uiReady once UI is rendered', () => {
  196. let isReady;
  197. class EventWatcher extends Plugin {
  198. init() {
  199. this.editor.on( 'uiReady', () => {
  200. isReady = this.editor.ui.view.isRendered;
  201. } );
  202. }
  203. }
  204. return ClassicEditor
  205. .create( editorElement, {
  206. plugins: [ EventWatcher ]
  207. } )
  208. .then( newEditor => {
  209. expect( isReady ).to.be.true;
  210. editor = newEditor;
  211. } );
  212. } );
  213. it( 'fires ready once UI is rendered', () => {
  214. let isReady;
  215. class EventWatcher extends Plugin {
  216. init() {
  217. this.editor.ui.on( 'ready', () => {
  218. isReady = this.editor.ui.view.isRendered;
  219. } );
  220. }
  221. }
  222. return ClassicEditor
  223. .create( editorElement, {
  224. plugins: [ EventWatcher ]
  225. } )
  226. .then( newEditor => {
  227. expect( isReady ).to.be.true;
  228. editor = newEditor;
  229. } );
  230. } );
  231. } );
  232. describe( 'destroy', () => {
  233. beforeEach( function() {
  234. return ClassicEditor
  235. .create( editorElement, { plugins: [ Paragraph ] } )
  236. .then( newEditor => {
  237. editor = newEditor;
  238. } );
  239. } );
  240. it( 'sets the data back to the editor element', () => {
  241. editor.setData( '<p>foo</p>' );
  242. return editor.destroy()
  243. .then( () => {
  244. expect( editorElement.innerHTML ).to.equal( '<p>foo</p>' );
  245. } );
  246. } );
  247. it( 'does not update the source element if editor was initialized with data', () => {
  248. return ClassicEditor
  249. .create( '<p>Foo.</p>', {
  250. plugins: [ Paragraph, Bold ]
  251. } )
  252. .then( newEditor => {
  253. const spy = sinon.stub( newEditor, 'updateSourceElement' );
  254. return newEditor.destroy()
  255. .then( () => {
  256. expect( spy.called ).to.be.false;
  257. spy.restore();
  258. } );
  259. } );
  260. } );
  261. it( 'restores the editor element', () => {
  262. expect( editor.sourceElement.style.display ).to.equal( 'none' );
  263. return editor.destroy()
  264. .then( () => {
  265. expect( editor.sourceElement.style.display ).to.equal( '' );
  266. } );
  267. } );
  268. } );
  269. } );