ballooneditor.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 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( 'should have undefined the #sourceElement if editor was initialized with data', () => {
  59. return BalloonEditor
  60. .create( '<p>Foo.</p>', {
  61. plugins: [ Paragraph, Bold ]
  62. } )
  63. .then( newEditor => {
  64. expect( newEditor.sourceElement ).to.be.undefined;
  65. return newEditor.destroy();
  66. } );
  67. } );
  68. } );
  69. describe( 'create()', () => {
  70. beforeEach( function() {
  71. return BalloonEditor
  72. .create( editorElement, {
  73. plugins: [ Paragraph, Bold ]
  74. } )
  75. .then( newEditor => {
  76. editor = newEditor;
  77. } );
  78. } );
  79. afterEach( () => {
  80. return editor.destroy();
  81. } );
  82. it( 'creates an instance which inherits from the BalloonEditor', () => {
  83. expect( editor ).to.be.instanceof( BalloonEditor );
  84. } );
  85. it( 'creates element–less UI view', () => {
  86. expect( editor.ui.view.element ).to.be.null;
  87. } );
  88. it( 'attaches editable UI as view\'s DOM root', () => {
  89. const domRoot = editor.editing.view.getDomRoot();
  90. expect( domRoot ).to.equal( editor.ui.view.editable.element );
  91. } );
  92. it( 'creates the UI using BalloonEditorUI classes', () => {
  93. expect( editor.ui ).to.be.instanceof( BalloonEditorUI );
  94. expect( editor.ui.view ).to.be.instanceof( BalloonEditorUIView );
  95. } );
  96. it( 'loads data from the editor element', () => {
  97. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  98. } );
  99. it( 'should not require config object', () => {
  100. // Just being safe with `builtinPlugins` static property.
  101. class CustomBalloonEditor extends BalloonEditor {}
  102. CustomBalloonEditor.builtinPlugins = [ Paragraph, Bold ];
  103. return CustomBalloonEditor.create( editorElement )
  104. .then( newEditor => {
  105. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  106. return newEditor.destroy();
  107. } );
  108. } );
  109. it( 'allows to pass data to the constructor', () => {
  110. return BalloonEditor.create( '<p>Hello world!</p>', {
  111. plugins: [ Paragraph ]
  112. } ).then( editor => {
  113. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  114. editor.destroy();
  115. } );
  116. } );
  117. it( 'initializes with config.initialData', () => {
  118. return BalloonEditor.create( editorElement, {
  119. initialData: '<p>Hello world!</p>',
  120. plugins: [ Paragraph ]
  121. } ).then( editor => {
  122. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  123. editor.destroy();
  124. } );
  125. } );
  126. it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => {
  127. BalloonEditor.create( '<p>Hello world!</p>', {
  128. initialData: '<p>I am evil!</p>',
  129. plugins: [ Paragraph ]
  130. } ).catch( () => {
  131. done();
  132. } );
  133. } );
  134. // ckeditor/ckeditor5-editor-classic#53
  135. it( 'creates an instance of a BalloonEditor child class', () => {
  136. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  137. // editor being initialized on one element.
  138. const editorElement = document.createElement( 'div' );
  139. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  140. document.body.appendChild( editorElement );
  141. class CustomBalloonEditor extends BalloonEditor {}
  142. return CustomBalloonEditor
  143. .create( editorElement, {
  144. plugins: [ Paragraph, Bold ]
  145. } )
  146. .then( newEditor => {
  147. expect( newEditor ).to.be.instanceof( CustomBalloonEditor );
  148. expect( newEditor ).to.be.instanceof( BalloonEditor );
  149. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  150. editorElement.remove();
  151. return newEditor.destroy();
  152. } );
  153. } );
  154. } );
  155. describe( 'create - events', () => {
  156. afterEach( () => {
  157. return editor.destroy();
  158. } );
  159. it( 'fires all events in the right order', () => {
  160. const fired = [];
  161. function spy( evt ) {
  162. fired.push( `${ evt.name }-${ evt.source.constructor.name.toLowerCase() }` );
  163. }
  164. class EventWatcher extends Plugin {
  165. init() {
  166. this.editor.ui.on( 'ready', spy );
  167. this.editor.data.on( 'ready', spy );
  168. this.editor.on( 'ready', spy );
  169. }
  170. }
  171. return BalloonEditor
  172. .create( editorElement, {
  173. plugins: [ EventWatcher ]
  174. } )
  175. .then( newEditor => {
  176. expect( fired ).to.deep.equal(
  177. [ 'ready-ballooneditorui', 'ready-datacontroller', 'ready-ballooneditor' ] );
  178. editor = newEditor;
  179. } );
  180. } );
  181. it( 'fires ready once UI is ready', () => {
  182. let isRendered;
  183. class EventWatcher extends Plugin {
  184. init() {
  185. this.editor.ui.on( 'ready', () => {
  186. isRendered = this.editor.ui.view.isRendered;
  187. } );
  188. }
  189. }
  190. return BalloonEditor
  191. .create( editorElement, {
  192. plugins: [ EventWatcher ]
  193. } )
  194. .then( newEditor => {
  195. expect( isRendered ).to.be.true;
  196. editor = newEditor;
  197. } );
  198. } );
  199. } );
  200. describe( 'destroy()', () => {
  201. beforeEach( function() {
  202. return BalloonEditor
  203. .create( editorElement, { plugins: [ Paragraph ] } )
  204. .then( newEditor => {
  205. editor = newEditor;
  206. const schema = editor.model.schema;
  207. schema.register( 'heading' );
  208. schema.extend( 'heading', { allowIn: '$root' } );
  209. schema.extend( '$text', { allowIn: 'heading' } );
  210. editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading', view: 'heading' } );
  211. editor.conversion.for( 'dataDowncast' ).elementToElement( { model: 'heading', view: 'heading' } );
  212. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  213. model: 'heading',
  214. view: 'heading-editing-representation'
  215. } );
  216. } );
  217. } );
  218. it( 'sets the data back to the editor element', () => {
  219. editor.setData( '<p>a</p><heading>b</heading>' );
  220. return editor.destroy()
  221. .then( () => {
  222. expect( editorElement.innerHTML )
  223. .to.equal( '<p>a</p><heading>b</heading>' );
  224. } );
  225. } );
  226. it( 'should not throw an error if editor was initialized with the data', () => {
  227. return BalloonEditor
  228. .create( '<p>Foo.</p>', {
  229. plugins: [ Paragraph, Bold ]
  230. } )
  231. .then( newEditor => newEditor.destroy() );
  232. } );
  233. } );
  234. describeMemoryUsage( () => {
  235. testMemoryUsage(
  236. 'should not grow on multiple create/destroy',
  237. () => BalloonEditor
  238. .create( document.querySelector( '#mem-editor' ), {
  239. plugins: [ ArticlePluginSet ],
  240. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  241. image: {
  242. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  243. }
  244. } ) );
  245. } );
  246. } );