8
0

classiceditor.js 5.1 KB

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