8
0

classiceditor.js 5.4 KB

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