ballooneditor.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 BalloonEditorUI from '../src/ballooneditorui';
  7. import BalloonEditorUIView from '../src/ballooneditoruiview';
  8. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  9. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  10. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  11. import BalloonEditor from '../src/ballooneditor';
  12. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  15. import ContextualToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/contextual/contextualtoolbar';
  16. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  17. testUtils.createSinonSandbox();
  18. describe( 'BalloonEditor', () => {
  19. let editor, editorElement;
  20. beforeEach( () => {
  21. editorElement = document.createElement( 'div' );
  22. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  23. document.body.appendChild( editorElement );
  24. } );
  25. afterEach( () => {
  26. editorElement.remove();
  27. } );
  28. describe( 'constructor()', () => {
  29. beforeEach( () => {
  30. editor = new BalloonEditor( editorElement, {
  31. plugins: [ ContextualToolbar, Bold ],
  32. toolbar: [ 'Bold' ]
  33. } );
  34. } );
  35. it( 'pushes ContextualToolbar to the list of plugins', () => {
  36. expect( editor.config.get( 'plugins' ) ).to.include( ContextualToolbar );
  37. } );
  38. it( 'pipes config#toolbar to config#contextualToolbar', () => {
  39. expect( editor.config.get( 'contextualToolbar' ) ).to.have.members( [ 'Bold' ] );
  40. } );
  41. it( 'uses HTMLDataProcessor', () => {
  42. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  43. } );
  44. } );
  45. describe( 'create()', () => {
  46. beforeEach( function() {
  47. return BalloonEditor
  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 BalloonEditor', () => {
  59. expect( editor ).to.be.instanceof( BalloonEditor );
  60. } );
  61. it( 'creates element–less UI view', () => {
  62. expect( editor.ui.view.element ).to.be.null;
  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( 'creates the UI using BalloonEditorUI classes', () => {
  68. expect( editor.ui ).to.be.instanceof( BalloonEditorUI );
  69. expect( editor.ui.view ).to.be.instanceof( BalloonEditorUIView );
  70. } );
  71. it( 'loads data from the editor element', () => {
  72. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  73. } );
  74. // ckeditor/ckeditor5-editor-classic#53
  75. it( 'creates an instance of a BalloonEditor child class', () => {
  76. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  77. // editor being initialized on one element.
  78. const editorElement = document.createElement( 'div' );
  79. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  80. document.body.appendChild( editorElement );
  81. class CustomBalloonEditor extends BalloonEditor {}
  82. return CustomBalloonEditor
  83. .create( editorElement, {
  84. plugins: [ Paragraph, Bold ]
  85. } )
  86. .then( newEditor => {
  87. expect( newEditor ).to.be.instanceof( CustomBalloonEditor );
  88. expect( newEditor ).to.be.instanceof( BalloonEditor );
  89. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  90. editorElement.remove();
  91. return newEditor.destroy();
  92. } );
  93. } );
  94. } );
  95. describe( 'create - events', () => {
  96. afterEach( () => {
  97. return editor.destroy();
  98. } );
  99. it( 'fires all events in the right order', () => {
  100. const fired = [];
  101. function spy( evt ) {
  102. fired.push( evt.name );
  103. }
  104. class EventWatcher extends Plugin {
  105. init() {
  106. this.editor.on( 'pluginsReady', spy );
  107. this.editor.on( 'uiReady', spy );
  108. this.editor.on( 'dataReady', spy );
  109. this.editor.on( 'ready', spy );
  110. }
  111. }
  112. return BalloonEditor
  113. .create( editorElement, {
  114. plugins: [ EventWatcher ]
  115. } )
  116. .then( newEditor => {
  117. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  118. editor = newEditor;
  119. } );
  120. } );
  121. it( 'fires dataReady once data is loaded', () => {
  122. let data;
  123. class EventWatcher extends Plugin {
  124. init() {
  125. this.editor.on( 'dataReady', () => {
  126. data = this.editor.getData();
  127. } );
  128. }
  129. }
  130. return BalloonEditor
  131. .create( editorElement, {
  132. plugins: [ EventWatcher, Paragraph, Bold ]
  133. } )
  134. .then( newEditor => {
  135. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  136. editor = newEditor;
  137. } );
  138. } );
  139. it( 'fires uiReady once UI is ready', () => {
  140. let isRendered;
  141. class EventWatcher extends Plugin {
  142. init() {
  143. this.editor.on( 'uiReady', () => {
  144. isRendered = this.editor.ui.view.isRendered;
  145. } );
  146. }
  147. }
  148. return BalloonEditor
  149. .create( editorElement, {
  150. plugins: [ EventWatcher ]
  151. } )
  152. .then( newEditor => {
  153. expect( isRendered ).to.be.true;
  154. editor = newEditor;
  155. } );
  156. } );
  157. } );
  158. describe( 'destroy()', () => {
  159. beforeEach( function() {
  160. return BalloonEditor
  161. .create( editorElement, { plugins: [ Paragraph ] } )
  162. .then( newEditor => {
  163. editor = newEditor;
  164. const schema = editor.model.schema;
  165. schema.register( 'heading' );
  166. schema.extend( 'heading', { allowIn: '$root' } );
  167. schema.extend( '$text', { allowIn: 'heading' } );
  168. buildModelConverter().for( editor.data.modelToView )
  169. .fromElement( 'heading' )
  170. .toElement( 'heading' );
  171. buildViewConverter().for( editor.data.viewToModel )
  172. .fromElement( 'heading' )
  173. .toElement( 'heading' );
  174. buildModelConverter().for( editor.editing.modelToView )
  175. .fromElement( 'heading' )
  176. .toElement( 'heading-editing-representation' );
  177. } );
  178. } );
  179. it( 'sets the data back to the editor element', () => {
  180. editor.setData( '<p>a</p><heading>b</heading>' );
  181. return editor.destroy()
  182. .then( () => {
  183. expect( editorElement.innerHTML )
  184. .to.equal( '<p>a</p><heading>b</heading>' );
  185. } );
  186. } );
  187. } );
  188. } );