ballooneditor.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, Event */
  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 DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
  17. import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin';
  18. import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
  19. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  20. testUtils.createSinonSandbox();
  21. describe( 'BalloonEditor', () => {
  22. let editor, editorElement;
  23. beforeEach( () => {
  24. editorElement = document.createElement( 'div' );
  25. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  26. document.body.appendChild( editorElement );
  27. } );
  28. afterEach( () => {
  29. editorElement.remove();
  30. } );
  31. describe( 'constructor()', () => {
  32. beforeEach( () => {
  33. editor = new BalloonEditor( editorElement, {
  34. plugins: [ ContextualToolbar, Bold ],
  35. toolbar: [ 'Bold' ]
  36. } );
  37. } );
  38. it( 'pushes ContextualToolbar to the list of plugins', () => {
  39. expect( editor.config.get( 'plugins' ) ).to.include( ContextualToolbar );
  40. } );
  41. it( 'pipes config#toolbar to config#contextualToolbar', () => {
  42. expect( editor.config.get( 'contextualToolbar' ) ).to.have.members( [ 'Bold' ] );
  43. } );
  44. it( 'uses HTMLDataProcessor', () => {
  45. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  46. } );
  47. it( 'has a Data Interface', () => {
  48. testUtils.isMixed( BalloonEditor, DataApiMixin );
  49. } );
  50. it( 'has a Element Interface', () => {
  51. testUtils.isMixed( BalloonEditor, ElementApiMixin );
  52. } );
  53. it( 'creates main root element', () => {
  54. expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
  55. } );
  56. it( 'handles form element', () => {
  57. const form = document.createElement( 'form' );
  58. const textarea = document.createElement( 'textarea' );
  59. form.appendChild( textarea );
  60. document.body.appendChild( form );
  61. // Prevents page realods in Firefox ;|
  62. form.addEventListener( 'submit', evt => {
  63. evt.preventDefault();
  64. } );
  65. return BalloonEditor.create( textarea, {
  66. plugins: [ Paragraph ]
  67. } ).then( editor => {
  68. expect( textarea.value ).to.equal( '' );
  69. editor.setData( '<p>Foo</p>' );
  70. form.dispatchEvent( new Event( 'submit', {
  71. // We need to be able to do preventDefault() to prevent page reloads in Firefox.
  72. cancelable: true
  73. } ) );
  74. expect( textarea.value ).to.equal( '<p>Foo</p>' );
  75. return editor.destroy().then( () => {
  76. form.remove();
  77. } );
  78. } );
  79. } );
  80. } );
  81. describe( 'create()', () => {
  82. beforeEach( function() {
  83. return BalloonEditor
  84. .create( editorElement, {
  85. plugins: [ Paragraph, Bold ]
  86. } )
  87. .then( newEditor => {
  88. editor = newEditor;
  89. } );
  90. } );
  91. afterEach( () => {
  92. return editor.destroy();
  93. } );
  94. it( 'creates an instance which inherits from the BalloonEditor', () => {
  95. expect( editor ).to.be.instanceof( BalloonEditor );
  96. } );
  97. it( 'creates element–less UI view', () => {
  98. expect( editor.ui.view.element ).to.be.null;
  99. } );
  100. it( 'attaches editable UI as view\'s DOM root', () => {
  101. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  102. } );
  103. it( 'creates the UI using BalloonEditorUI classes', () => {
  104. expect( editor.ui ).to.be.instanceof( BalloonEditorUI );
  105. expect( editor.ui.view ).to.be.instanceof( BalloonEditorUIView );
  106. } );
  107. it( 'loads data from the editor element', () => {
  108. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  109. } );
  110. // ckeditor/ckeditor5-editor-classic#53
  111. it( 'creates an instance of a BalloonEditor child class', () => {
  112. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  113. // editor being initialized on one element.
  114. const editorElement = document.createElement( 'div' );
  115. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  116. document.body.appendChild( editorElement );
  117. class CustomBalloonEditor extends BalloonEditor {}
  118. return CustomBalloonEditor
  119. .create( editorElement, {
  120. plugins: [ Paragraph, Bold ]
  121. } )
  122. .then( newEditor => {
  123. expect( newEditor ).to.be.instanceof( CustomBalloonEditor );
  124. expect( newEditor ).to.be.instanceof( BalloonEditor );
  125. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  126. editorElement.remove();
  127. return newEditor.destroy();
  128. } );
  129. } );
  130. } );
  131. describe( 'create - events', () => {
  132. afterEach( () => {
  133. return editor.destroy();
  134. } );
  135. it( 'fires all events in the right order', () => {
  136. const fired = [];
  137. function spy( evt ) {
  138. fired.push( evt.name );
  139. }
  140. class EventWatcher extends Plugin {
  141. init() {
  142. this.editor.on( 'pluginsReady', spy );
  143. this.editor.on( 'uiReady', spy );
  144. this.editor.on( 'dataReady', spy );
  145. this.editor.on( 'ready', spy );
  146. }
  147. }
  148. return BalloonEditor
  149. .create( editorElement, {
  150. plugins: [ EventWatcher ]
  151. } )
  152. .then( newEditor => {
  153. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  154. editor = newEditor;
  155. } );
  156. } );
  157. it( 'fires dataReady once data is loaded', () => {
  158. let data;
  159. class EventWatcher extends Plugin {
  160. init() {
  161. this.editor.on( 'dataReady', () => {
  162. data = this.editor.getData();
  163. } );
  164. }
  165. }
  166. return BalloonEditor
  167. .create( editorElement, {
  168. plugins: [ EventWatcher, Paragraph, Bold ]
  169. } )
  170. .then( newEditor => {
  171. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  172. editor = newEditor;
  173. } );
  174. } );
  175. it( 'fires uiReady once UI is ready', () => {
  176. let isRendered;
  177. class EventWatcher extends Plugin {
  178. init() {
  179. this.editor.on( 'uiReady', () => {
  180. isRendered = this.editor.ui.view.isRendered;
  181. } );
  182. }
  183. }
  184. return BalloonEditor
  185. .create( editorElement, {
  186. plugins: [ EventWatcher ]
  187. } )
  188. .then( newEditor => {
  189. expect( isRendered ).to.be.true;
  190. editor = newEditor;
  191. } );
  192. } );
  193. } );
  194. describe( 'destroy()', () => {
  195. beforeEach( function() {
  196. return BalloonEditor
  197. .create( editorElement, { plugins: [ Paragraph ] } )
  198. .then( newEditor => {
  199. editor = newEditor;
  200. const schema = editor.model.schema;
  201. schema.register( 'heading' );
  202. schema.extend( 'heading', { allowIn: '$root' } );
  203. schema.extend( '$text', { allowIn: 'heading' } );
  204. buildModelConverter().for( editor.data.modelToView )
  205. .fromElement( 'heading' )
  206. .toElement( 'heading' );
  207. buildViewConverter().for( editor.data.viewToModel )
  208. .fromElement( 'heading' )
  209. .toElement( 'heading' );
  210. buildModelConverter().for( editor.editing.modelToView )
  211. .fromElement( 'heading' )
  212. .toElement( 'heading-editing-representation' );
  213. } );
  214. } );
  215. it( 'sets the data back to the editor element', () => {
  216. editor.setData( '<p>a</p><heading>b</heading>' );
  217. return editor.destroy()
  218. .then( () => {
  219. expect( editorElement.innerHTML )
  220. .to.equal( '<p>a</p><heading>b</heading>' );
  221. } );
  222. } );
  223. } );
  224. } );