ballooneditor.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  22. describe( 'BalloonEditor', () => {
  23. let editor, editorElement;
  24. testUtils.createSinonSandbox();
  25. beforeEach( () => {
  26. editorElement = document.createElement( 'div' );
  27. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  28. document.body.appendChild( editorElement );
  29. testUtils.sinon.stub( log, 'warn' ).callsFake( () => {} );
  30. } );
  31. afterEach( () => {
  32. editorElement.remove();
  33. } );
  34. describe( 'constructor()', () => {
  35. beforeEach( () => {
  36. editor = new BalloonEditor( editorElement, {
  37. plugins: [ BalloonToolbar, Bold ],
  38. toolbar: [ 'Bold' ]
  39. } );
  40. } );
  41. it( 'pushes BalloonToolbar to the list of plugins', () => {
  42. expect( editor.config.get( 'plugins' ) ).to.include( BalloonToolbar );
  43. } );
  44. it( 'pipes config#toolbar to config#balloonToolbar', () => {
  45. expect( editor.config.get( 'balloonToolbar' ) ).to.have.members( [ 'Bold' ] );
  46. } );
  47. it( 'uses HTMLDataProcessor', () => {
  48. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  49. } );
  50. it( 'has a Data Interface', () => {
  51. testUtils.isMixed( BalloonEditor, DataApiMixin );
  52. } );
  53. it( 'has a Element Interface', () => {
  54. testUtils.isMixed( BalloonEditor, ElementApiMixin );
  55. } );
  56. it( 'creates main root element', () => {
  57. expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
  58. } );
  59. it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
  60. return BalloonEditor
  61. .create( '<p>Foo.</p>', {
  62. plugins: [ Paragraph, Bold ]
  63. } )
  64. .then( newEditor => {
  65. expect( newEditor.sourceElement ).to.be.undefined;
  66. return newEditor.destroy();
  67. } );
  68. } );
  69. } );
  70. describe( 'create()', () => {
  71. beforeEach( function() {
  72. return BalloonEditor
  73. .create( editorElement, {
  74. plugins: [ Paragraph, Bold ]
  75. } )
  76. .then( newEditor => {
  77. editor = newEditor;
  78. } );
  79. } );
  80. afterEach( () => {
  81. return editor.destroy();
  82. } );
  83. it( 'creates an instance which inherits from the BalloonEditor', () => {
  84. expect( editor ).to.be.instanceof( BalloonEditor );
  85. } );
  86. it( 'creates element–less UI view', () => {
  87. expect( editor.ui.view.element ).to.be.null;
  88. } );
  89. it( 'attaches editable UI as view\'s DOM root', () => {
  90. const domRoot = editor.editing.view.getDomRoot();
  91. expect( domRoot ).to.equal( editor.ui.view.editable.element );
  92. } );
  93. it( 'creates the UI using BalloonEditorUI classes', () => {
  94. expect( editor.ui ).to.be.instanceof( BalloonEditorUI );
  95. expect( editor.ui.view ).to.be.instanceof( BalloonEditorUIView );
  96. } );
  97. it( 'loads data from the editor element', () => {
  98. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  99. } );
  100. it( 'should not require config object', () => {
  101. // Just being safe with `builtinPlugins` static property.
  102. class CustomBalloonEditor extends BalloonEditor {}
  103. CustomBalloonEditor.builtinPlugins = [ Paragraph, Bold ];
  104. return CustomBalloonEditor.create( editorElement )
  105. .then( newEditor => {
  106. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  107. return newEditor.destroy();
  108. } );
  109. } );
  110. it( 'allows to pass data to the constructor', () => {
  111. return BalloonEditor.create( '<p>Hello world!</p>', {
  112. plugins: [ Paragraph ]
  113. } ).then( editor => {
  114. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  115. editor.destroy();
  116. } );
  117. } );
  118. it( 'initializes with config.initialData', () => {
  119. return BalloonEditor.create( editorElement, {
  120. initialData: '<p>Hello world!</p>',
  121. plugins: [ Paragraph ]
  122. } ).then( editor => {
  123. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  124. editor.destroy();
  125. } );
  126. } );
  127. it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => {
  128. BalloonEditor.create( '<p>Hello world!</p>', {
  129. initialData: '<p>I am evil!</p>',
  130. plugins: [ Paragraph ]
  131. } )
  132. .then(
  133. () => {
  134. expect.fail( 'Balloon editor should throw an error when both initial data are passed' );
  135. },
  136. err => {
  137. assertCKEditorError( err,
  138. // eslint-disable-next-line max-len
  139. /^editor-create-initial-data: The config\.initialData option cannot be used together with initial data passed in Editor\.create\(\)\./,
  140. null
  141. );
  142. }
  143. )
  144. .then( done )
  145. .catch( done );
  146. } );
  147. // ckeditor/ckeditor5-editor-classic#53
  148. it( 'creates an instance of a BalloonEditor child class', () => {
  149. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  150. // editor being initialized on one element.
  151. const editorElement = document.createElement( 'div' );
  152. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  153. document.body.appendChild( editorElement );
  154. class CustomBalloonEditor extends BalloonEditor {}
  155. return CustomBalloonEditor
  156. .create( editorElement, {
  157. plugins: [ Paragraph, Bold ]
  158. } )
  159. .then( newEditor => {
  160. expect( newEditor ).to.be.instanceof( CustomBalloonEditor );
  161. expect( newEditor ).to.be.instanceof( BalloonEditor );
  162. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  163. editorElement.remove();
  164. return newEditor.destroy();
  165. } );
  166. } );
  167. it( 'throws an error when is initialized in textarea', done => {
  168. BalloonEditor.create( document.createElement( 'textarea' ) )
  169. .then(
  170. () => {
  171. expect.fail( 'Balloon editor should throw an error when is initialized in textarea.' );
  172. },
  173. err => {
  174. assertCKEditorError( err,
  175. /^editor-wrong-element: This type of editor cannot be initialized inside <textarea> element\./,
  176. null
  177. );
  178. }
  179. )
  180. .then( done )
  181. .catch( done );
  182. } );
  183. } );
  184. describe( 'create - events', () => {
  185. afterEach( () => {
  186. return editor.destroy();
  187. } );
  188. it( 'fires all events in the right order', () => {
  189. const fired = [];
  190. function spy( evt ) {
  191. fired.push( `${ evt.name }-${ evt.source.constructor.name.toLowerCase() }` );
  192. }
  193. class EventWatcher extends Plugin {
  194. init() {
  195. this.editor.ui.on( 'ready', spy );
  196. this.editor.data.on( 'ready', spy );
  197. this.editor.on( 'ready', spy );
  198. }
  199. }
  200. return BalloonEditor
  201. .create( editorElement, {
  202. plugins: [ EventWatcher ]
  203. } )
  204. .then( newEditor => {
  205. expect( fired ).to.deep.equal(
  206. [ 'ready-ballooneditorui', 'ready-datacontroller', 'ready-ballooneditor' ] );
  207. editor = newEditor;
  208. } );
  209. } );
  210. it( 'fires ready once UI is ready', () => {
  211. let isRendered;
  212. class EventWatcher extends Plugin {
  213. init() {
  214. this.editor.ui.on( 'ready', () => {
  215. isRendered = this.editor.ui.view.isRendered;
  216. } );
  217. }
  218. }
  219. return BalloonEditor
  220. .create( editorElement, {
  221. plugins: [ EventWatcher ]
  222. } )
  223. .then( newEditor => {
  224. expect( isRendered ).to.be.true;
  225. editor = newEditor;
  226. } );
  227. } );
  228. } );
  229. describe( 'destroy()', () => {
  230. beforeEach( function() {
  231. return BalloonEditor
  232. .create( editorElement, { plugins: [ Paragraph ] } )
  233. .then( newEditor => {
  234. editor = newEditor;
  235. const schema = editor.model.schema;
  236. schema.register( 'heading' );
  237. schema.extend( 'heading', { allowIn: '$root' } );
  238. schema.extend( '$text', { allowIn: 'heading' } );
  239. editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading', view: 'heading' } );
  240. editor.conversion.for( 'dataDowncast' ).elementToElement( { model: 'heading', view: 'heading' } );
  241. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  242. model: 'heading',
  243. view: 'heading-editing-representation'
  244. } );
  245. } );
  246. } );
  247. it( 'sets the data back to the editor element', () => {
  248. editor.setData( '<p>a</p><heading>b</heading>' );
  249. return editor.destroy()
  250. .then( () => {
  251. expect( editorElement.innerHTML )
  252. .to.equal( '<p>a</p><heading>b</heading>' );
  253. } );
  254. } );
  255. it( 'should not throw an error if editor was initialized with the data', () => {
  256. return BalloonEditor
  257. .create( '<p>Foo.</p>', {
  258. plugins: [ Paragraph, Bold ]
  259. } )
  260. .then( newEditor => newEditor.destroy() );
  261. } );
  262. } );
  263. describeMemoryUsage( () => {
  264. testMemoryUsage(
  265. 'should not grow on multiple create/destroy',
  266. () => BalloonEditor
  267. .create( document.querySelector( '#mem-editor' ), {
  268. plugins: [ ArticlePluginSet ],
  269. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  270. image: {
  271. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  272. }
  273. } ) );
  274. } );
  275. } );