classiceditor.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  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 testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. import count from '@ckeditor/ckeditor5-utils/src/count';
  15. testUtils.createSinonSandbox();
  16. describe( 'ClassicEditor', () => {
  17. let editor, editorElement;
  18. beforeEach( () => {
  19. editorElement = document.createElement( 'div' );
  20. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  21. document.body.appendChild( editorElement );
  22. } );
  23. afterEach( () => {
  24. editorElement.remove();
  25. } );
  26. describe( 'constructor()', () => {
  27. beforeEach( () => {
  28. editor = new ClassicEditor( editorElement );
  29. } );
  30. it( 'creates a single document root', () => {
  31. expect( count( editor.model.document.getRootNames() ) ).to.equal( 1 );
  32. expect( editor.model.document.getRoot() ).to.have.property( 'name', '$root' );
  33. } );
  34. it( 'uses HTMLDataProcessor', () => {
  35. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  36. } );
  37. describe( 'ui', () => {
  38. it( 'creates a single div editable root in the view', () => {
  39. editor.ui.init();
  40. expect( editor.editing.view.getRoot() ).to.have.property( 'name', 'div' );
  41. editor.ui.destroy();
  42. } );
  43. it( 'creates the UI using BoxedEditorUI classes', () => {
  44. expect( editor.ui ).to.be.instanceof( ClassicEditorUI );
  45. expect( editor.ui.view ).to.be.instanceof( ClassicEditorUIView );
  46. } );
  47. } );
  48. } );
  49. describe( 'create()', () => {
  50. beforeEach( () => {
  51. return ClassicEditor
  52. .create( editorElement, {
  53. plugins: [ Paragraph, Bold ]
  54. } )
  55. .then( newEditor => {
  56. editor = newEditor;
  57. } );
  58. } );
  59. afterEach( () => {
  60. return editor.destroy();
  61. } );
  62. it( 'creates an instance which inherits from the ClassicEditor', () => {
  63. expect( editor ).to.be.instanceof( ClassicEditor );
  64. } );
  65. it( 'loads data from the editor element', () => {
  66. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  67. } );
  68. // #53
  69. it( 'creates an instance of a ClassicEditor child class', () => {
  70. class CustomClassicEditor extends ClassicEditor {}
  71. return CustomClassicEditor
  72. .create( editorElement, {
  73. plugins: [ Paragraph, Bold ]
  74. } )
  75. .then( newEditor => {
  76. expect( newEditor ).to.be.instanceof( CustomClassicEditor );
  77. expect( newEditor ).to.be.instanceof( ClassicEditor );
  78. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  79. return newEditor.destroy();
  80. } );
  81. } );
  82. describe( 'ui', () => {
  83. it( 'inserts editor UI next to editor element', () => {
  84. expect( editor.ui.view.element.previousSibling ).to.equal( editorElement );
  85. } );
  86. it( 'attaches editable UI as view\'s DOM root', () => {
  87. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  88. } );
  89. } );
  90. } );
  91. describe( 'create - events', () => {
  92. afterEach( () => {
  93. return editor.destroy();
  94. } );
  95. it( 'fires all events in the right order', () => {
  96. const fired = [];
  97. function spy( evt ) {
  98. fired.push( evt.name );
  99. }
  100. class EventWatcher extends Plugin {
  101. init() {
  102. this.editor.on( 'pluginsReady', spy );
  103. this.editor.on( 'uiReady', spy );
  104. this.editor.on( 'dataReady', spy );
  105. this.editor.on( 'ready', spy );
  106. }
  107. }
  108. return ClassicEditor
  109. .create( editorElement, {
  110. plugins: [ EventWatcher ]
  111. } )
  112. .then( newEditor => {
  113. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  114. editor = newEditor;
  115. } );
  116. } );
  117. it( 'fires dataReady once data is loaded', () => {
  118. let data;
  119. class EventWatcher extends Plugin {
  120. init() {
  121. this.editor.on( 'dataReady', () => {
  122. data = this.editor.getData();
  123. } );
  124. }
  125. }
  126. return ClassicEditor
  127. .create( editorElement, {
  128. plugins: [ EventWatcher, Paragraph, Bold ]
  129. } )
  130. .then( newEditor => {
  131. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  132. editor = newEditor;
  133. } );
  134. } );
  135. it( 'fires uiReady once UI is rendered', () => {
  136. let isReady;
  137. class EventWatcher extends Plugin {
  138. init() {
  139. this.editor.on( 'uiReady', () => {
  140. isReady = this.editor.ui.view.isRendered;
  141. } );
  142. }
  143. }
  144. return ClassicEditor
  145. .create( editorElement, {
  146. plugins: [ EventWatcher ]
  147. } )
  148. .then( newEditor => {
  149. expect( isReady ).to.be.true;
  150. editor = newEditor;
  151. } );
  152. } );
  153. } );
  154. describe( 'destroy', () => {
  155. beforeEach( function() {
  156. return ClassicEditor
  157. .create( editorElement, { plugins: [ Paragraph ] } )
  158. .then( newEditor => {
  159. editor = newEditor;
  160. } );
  161. } );
  162. it( 'sets the data back to the editor element', () => {
  163. editor.setData( '<p>foo</p>' );
  164. return editor.destroy()
  165. .then( () => {
  166. expect( editorElement.innerHTML ).to.equal( '<p>foo</p>' );
  167. } );
  168. } );
  169. it( 'restores the editor element', () => {
  170. expect( editor.element.style.display ).to.equal( 'none' );
  171. return editor.destroy()
  172. .then( () => {
  173. expect( editor.element.style.display ).to.equal( '' );
  174. } );
  175. } );
  176. } );
  177. } );