ballooneditor.js 6.8 KB

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