ballooneditor.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 BalloonEditor from '../src/ballooneditor';
  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 BalloonToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/balloon/balloontoolbar';
  14. import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
  15. import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin';
  16. import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
  17. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  18. import log from '@ckeditor/ckeditor5-utils/src/log';
  19. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  20. import { describeMemoryUsage, testMemoryUsage } from '@ckeditor/ckeditor5-core/tests/_utils/memory';
  21. describe( 'BalloonEditor', () => {
  22. let editor, editorElement;
  23. testUtils.createSinonSandbox();
  24. beforeEach( () => {
  25. editorElement = document.createElement( 'div' );
  26. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  27. document.body.appendChild( editorElement );
  28. testUtils.sinon.stub( log, 'warn' ).callsFake( () => {} );
  29. } );
  30. afterEach( () => {
  31. editorElement.remove();
  32. } );
  33. describe( 'constructor()', () => {
  34. beforeEach( () => {
  35. editor = new BalloonEditor( editorElement, {
  36. plugins: [ BalloonToolbar, Bold ],
  37. toolbar: [ 'Bold' ]
  38. } );
  39. } );
  40. it( 'pushes BalloonToolbar to the list of plugins', () => {
  41. expect( editor.config.get( 'plugins' ) ).to.include( BalloonToolbar );
  42. } );
  43. it( 'pipes config#toolbar to config#balloonToolbar', () => {
  44. expect( editor.config.get( 'balloonToolbar' ) ).to.have.members( [ 'Bold' ] );
  45. } );
  46. it( 'uses HTMLDataProcessor', () => {
  47. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  48. } );
  49. it( 'has a Data Interface', () => {
  50. testUtils.isMixed( BalloonEditor, DataApiMixin );
  51. } );
  52. it( 'has a Element Interface', () => {
  53. testUtils.isMixed( BalloonEditor, ElementApiMixin );
  54. } );
  55. it( 'creates main root element', () => {
  56. expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
  57. } );
  58. it( 'handles form element', () => {
  59. const form = document.createElement( 'form' );
  60. const textarea = document.createElement( 'textarea' );
  61. form.appendChild( textarea );
  62. document.body.appendChild( form );
  63. // Prevents page realods in Firefox ;|
  64. form.addEventListener( 'submit', evt => {
  65. evt.preventDefault();
  66. } );
  67. return BalloonEditor.create( textarea, {
  68. plugins: [ Paragraph ]
  69. } ).then( editor => {
  70. expect( textarea.value ).to.equal( '' );
  71. editor.setData( '<p>Foo</p>' );
  72. form.dispatchEvent( new Event( 'submit', {
  73. // We need to be able to do preventDefault() to prevent page reloads in Firefox.
  74. cancelable: true
  75. } ) );
  76. expect( textarea.value ).to.equal( '<p>Foo</p>' );
  77. return editor.destroy().then( () => {
  78. form.remove();
  79. } );
  80. } );
  81. } );
  82. it( 'allows to pass data to the constructor', () => {
  83. return BalloonEditor.create( '<p>Hello world!</p>', {
  84. plugins: [ Paragraph ]
  85. } ).then( editor => {
  86. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  87. } );
  88. } );
  89. it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
  90. return BalloonEditor
  91. .create( '<p>Foo.</p>', {
  92. plugins: [ Paragraph, Bold ]
  93. } )
  94. .then( newEditor => {
  95. expect( newEditor.sourceElement ).to.be.undefined;
  96. return newEditor.destroy();
  97. } );
  98. } );
  99. } );
  100. describe( 'create()', () => {
  101. beforeEach( function() {
  102. return BalloonEditor
  103. .create( editorElement, {
  104. plugins: [ Paragraph, Bold ]
  105. } )
  106. .then( newEditor => {
  107. editor = newEditor;
  108. } );
  109. } );
  110. afterEach( () => {
  111. return editor.destroy();
  112. } );
  113. it( 'creates an instance which inherits from the BalloonEditor', () => {
  114. expect( editor ).to.be.instanceof( BalloonEditor );
  115. } );
  116. it( 'creates element–less UI view', () => {
  117. expect( editor.ui.view.element ).to.be.null;
  118. } );
  119. it( 'attaches editable UI as view\'s DOM root', () => {
  120. const domRoot = editor.editing.view.getDomRoot();
  121. expect( domRoot ).to.equal( editor.ui.view.editable.element );
  122. } );
  123. it( 'creates the UI using BalloonEditorUI classes', () => {
  124. expect( editor.ui ).to.be.instanceof( BalloonEditorUI );
  125. expect( editor.ui.view ).to.be.instanceof( BalloonEditorUIView );
  126. } );
  127. it( 'loads data from the editor element', () => {
  128. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  129. } );
  130. // ckeditor/ckeditor5-editor-classic#53
  131. it( 'creates an instance of a BalloonEditor child class', () => {
  132. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  133. // editor being initialized on one element.
  134. const editorElement = document.createElement( 'div' );
  135. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  136. document.body.appendChild( editorElement );
  137. class CustomBalloonEditor extends BalloonEditor {}
  138. return CustomBalloonEditor
  139. .create( editorElement, {
  140. plugins: [ Paragraph, Bold ]
  141. } )
  142. .then( newEditor => {
  143. expect( newEditor ).to.be.instanceof( CustomBalloonEditor );
  144. expect( newEditor ).to.be.instanceof( BalloonEditor );
  145. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  146. editorElement.remove();
  147. return newEditor.destroy();
  148. } );
  149. } );
  150. } );
  151. describe( 'create - events', () => {
  152. afterEach( () => {
  153. return editor.destroy();
  154. } );
  155. it( 'fires all events in the right order', () => {
  156. const fired = [];
  157. function spy( evt ) {
  158. fired.push( `${ evt.name }-${ evt.source.constructor.name.toLowerCase() }` );
  159. }
  160. class EventWatcher extends Plugin {
  161. init() {
  162. this.editor.plugins.on( 'ready', spy );
  163. this.editor.ui.on( 'ready', spy );
  164. this.editor.data.on( 'ready', spy );
  165. this.editor.on( 'ready', spy );
  166. }
  167. }
  168. return BalloonEditor
  169. .create( editorElement, {
  170. plugins: [ EventWatcher ]
  171. } )
  172. .then( newEditor => {
  173. expect( fired ).to.deep.equal(
  174. [ 'ready-plugincollection', 'ready-ballooneditorui', 'ready-datacontroller', 'ready-ballooneditor' ] );
  175. editor = newEditor;
  176. } );
  177. } );
  178. it( 'fires ready once UI is ready', () => {
  179. let isRendered;
  180. class EventWatcher extends Plugin {
  181. init() {
  182. this.editor.ui.on( 'ready', () => {
  183. isRendered = this.editor.ui.view.isRendered;
  184. } );
  185. }
  186. }
  187. return BalloonEditor
  188. .create( editorElement, {
  189. plugins: [ EventWatcher ]
  190. } )
  191. .then( newEditor => {
  192. expect( isRendered ).to.be.true;
  193. editor = newEditor;
  194. } );
  195. } );
  196. } );
  197. describe( 'destroy()', () => {
  198. beforeEach( function() {
  199. return BalloonEditor
  200. .create( editorElement, { plugins: [ Paragraph ] } )
  201. .then( newEditor => {
  202. editor = newEditor;
  203. const schema = editor.model.schema;
  204. schema.register( 'heading' );
  205. schema.extend( 'heading', { allowIn: '$root' } );
  206. schema.extend( '$text', { allowIn: 'heading' } );
  207. editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading', view: 'heading' } );
  208. editor.conversion.for( 'dataDowncast' ).elementToElement( { model: 'heading', view: 'heading' } );
  209. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  210. model: 'heading',
  211. view: 'heading-editing-representation'
  212. } );
  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. it( 'should not throw an error if editor was initialized with the data', () => {
  224. return BalloonEditor
  225. .create( '<p>Foo.</p>', {
  226. plugins: [ Paragraph, Bold ]
  227. } )
  228. .then( newEditor => newEditor.destroy() );
  229. } );
  230. } );
  231. describeMemoryUsage( () => {
  232. testMemoryUsage(
  233. 'should not grow on multiple create/destroy',
  234. () => BalloonEditor
  235. .create( document.querySelector( '#mem-editor' ), {
  236. plugins: [ ArticlePluginSet ],
  237. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  238. image: {
  239. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  240. }
  241. } ) );
  242. } );
  243. } );