decouplededitor.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import DecoupledEditorUI from '../src/decouplededitorui';
  7. import DecoupledEditorUIView from '../src/decouplededitoruiview';
  8. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  9. import DecoupledEditor from '../src/decouplededitor';
  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 RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
  15. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. testUtils.createSinonSandbox();
  17. const editorData = '<p><strong>foo</strong> bar</p>';
  18. describe( 'DecoupledEditor', () => {
  19. let editor;
  20. describe( 'constructor()', () => {
  21. beforeEach( () => {
  22. editor = new DecoupledEditor();
  23. editor.ui.init();
  24. } );
  25. afterEach( () => {
  26. return editor.destroy();
  27. } );
  28. it( 'uses HTMLDataProcessor', () => {
  29. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  30. } );
  31. it( 'has a Data Interface', () => {
  32. testUtils.isMixed( DecoupledEditor, DataApiMixin );
  33. } );
  34. it( 'creates main root element', () => {
  35. expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
  36. } );
  37. describe( 'ui', () => {
  38. it( 'is created', () => {
  39. expect( editor.ui ).to.be.instanceof( DecoupledEditorUI );
  40. expect( editor.ui.view ).to.be.instanceof( DecoupledEditorUIView );
  41. } );
  42. } );
  43. } );
  44. describe( 'create()', () => {
  45. afterEach( () => {
  46. return editor.destroy();
  47. } );
  48. describe( 'editor with data', () => {
  49. beforeEach( () => {
  50. return DecoupledEditor
  51. .create( editorData, {
  52. plugins: [ Paragraph, Bold ]
  53. } )
  54. .then( newEditor => {
  55. editor = newEditor;
  56. } );
  57. } );
  58. test( () => editorData );
  59. } );
  60. describe( 'editor with editable element', () => {
  61. let editableElement;
  62. beforeEach( () => {
  63. editableElement = document.createElement( 'div' );
  64. editableElement.innerHTML = editorData;
  65. return DecoupledEditor
  66. .create( editableElement, {
  67. plugins: [ Paragraph, Bold ]
  68. } )
  69. .then( newEditor => {
  70. editor = newEditor;
  71. } );
  72. } );
  73. test( () => editableElement );
  74. } );
  75. function test( getElementOrData ) {
  76. it( 'creates an instance which inherits from the DecoupledEditor', () => {
  77. expect( editor ).to.be.instanceof( DecoupledEditor );
  78. } );
  79. it( 'loads the initial data', () => {
  80. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  81. } );
  82. // https://github.com/ckeditor/ckeditor5-editor-classic/issues/53
  83. it( 'creates an instance of a DecoupledEditor child class', () => {
  84. class CustomDecoupledEditor extends DecoupledEditor {}
  85. return CustomDecoupledEditor
  86. .create( getElementOrData(), {
  87. plugins: [ Paragraph, Bold ]
  88. } )
  89. .then( newEditor => {
  90. expect( newEditor ).to.be.instanceof( CustomDecoupledEditor );
  91. expect( newEditor ).to.be.instanceof( DecoupledEditor );
  92. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  93. return newEditor.destroy();
  94. } );
  95. } );
  96. // https://github.com/ckeditor/ckeditor5-editor-decoupled/issues/3
  97. it( 'initializes the data controller', () => {
  98. let dataInitSpy;
  99. class DataInitAssertPlugin extends Plugin {
  100. constructor( editor ) {
  101. super();
  102. this.editor = editor;
  103. }
  104. init() {
  105. dataInitSpy = sinon.spy( this.editor.data, 'init' );
  106. }
  107. }
  108. return DecoupledEditor
  109. .create( getElementOrData(), {
  110. plugins: [ Paragraph, Bold, DataInitAssertPlugin ]
  111. } )
  112. .then( newEditor => {
  113. sinon.assert.calledOnce( dataInitSpy );
  114. return newEditor.destroy();
  115. } );
  116. } );
  117. describe( 'events', () => {
  118. it( 'fires all events in the right order', () => {
  119. const fired = [];
  120. function spy( evt ) {
  121. fired.push( evt.name );
  122. }
  123. class EventWatcher extends Plugin {
  124. init() {
  125. this.editor.on( 'pluginsReady', spy );
  126. this.editor.on( 'uiReady', spy );
  127. this.editor.on( 'dataReady', spy );
  128. this.editor.on( 'ready', spy );
  129. }
  130. }
  131. return DecoupledEditor
  132. .create( getElementOrData(), {
  133. plugins: [ EventWatcher ]
  134. } )
  135. .then( newEditor => {
  136. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  137. return newEditor.destroy();
  138. } );
  139. } );
  140. it( 'fires dataReady once data is loaded', () => {
  141. let data;
  142. class EventWatcher extends Plugin {
  143. init() {
  144. this.editor.on( 'dataReady', () => {
  145. data = this.editor.getData();
  146. } );
  147. }
  148. }
  149. return DecoupledEditor
  150. .create( getElementOrData(), {
  151. plugins: [ EventWatcher, Paragraph, Bold ]
  152. } )
  153. .then( newEditor => {
  154. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  155. return newEditor.destroy();
  156. } );
  157. } );
  158. it( 'fires uiReady once UI is rendered', () => {
  159. let isReady;
  160. class EventWatcher extends Plugin {
  161. init() {
  162. this.editor.on( 'uiReady', () => {
  163. isReady = this.editor.ui.view.isRendered;
  164. } );
  165. }
  166. }
  167. return DecoupledEditor
  168. .create( getElementOrData(), {
  169. plugins: [ EventWatcher ]
  170. } )
  171. .then( newEditor => {
  172. expect( isReady ).to.be.true;
  173. return newEditor.destroy();
  174. } );
  175. } );
  176. } );
  177. }
  178. } );
  179. describe( 'destroy', () => {
  180. describe( 'editor with data', () => {
  181. beforeEach( function() {
  182. return DecoupledEditor
  183. .create( editorData, { plugins: [ Paragraph ] } )
  184. .then( newEditor => {
  185. editor = newEditor;
  186. } );
  187. } );
  188. test( () => editorData );
  189. } );
  190. describe( 'editor with editable element', () => {
  191. let editableElement;
  192. beforeEach( function() {
  193. editableElement = document.createElement( 'div' );
  194. editableElement.innerHTML = editorData;
  195. return DecoupledEditor
  196. .create( editableElement, { plugins: [ Paragraph ] } )
  197. .then( newEditor => {
  198. editor = newEditor;
  199. } );
  200. } );
  201. it( 'sets data back to the element', () => {
  202. editor.setData( '<p>foo</p>' );
  203. return editor.destroy()
  204. .then( () => {
  205. expect( editableElement.innerHTML ).to.equal( '<p>foo</p>' );
  206. } );
  207. } );
  208. test( () => editableElement );
  209. } );
  210. function test() {
  211. it( 'destroys the UI', () => {
  212. const spy = sinon.spy( editor.ui, 'destroy' );
  213. return editor.destroy()
  214. .then( () => {
  215. sinon.assert.calledOnce( spy );
  216. } );
  217. } );
  218. }
  219. } );
  220. } );