ballooneditor.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document, console */
  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 ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  19. import { describeMemoryUsage, testMemoryUsage } from '@ckeditor/ckeditor5-core/tests/_utils/memory';
  20. import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  21. import { removeEditorBodyOrphans } from '@ckeditor/ckeditor5-core/tests/_utils/cleanup';
  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( console, '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. // See: https://github.com/ckeditor/ckeditor5/issues/746
  70. it( 'should throw when trying to create the editor using the same source element more than once', done => {
  71. BalloonEditor.create( editorElement )
  72. .then(
  73. () => {
  74. expect.fail( 'Balloon editor should not initialize on an element already used by other instance.' );
  75. },
  76. err => {
  77. assertCKEditorError( err, 'editor-source-element-already-used' );
  78. }
  79. )
  80. .then( done )
  81. .catch( done );
  82. } );
  83. } );
  84. describe( 'create()', () => {
  85. beforeEach( function() {
  86. return BalloonEditor
  87. .create( editorElement, {
  88. plugins: [ Paragraph, Bold ]
  89. } )
  90. .then( newEditor => {
  91. editor = newEditor;
  92. } );
  93. } );
  94. afterEach( () => {
  95. return editor.destroy();
  96. } );
  97. it( 'creates an instance which inherits from the BalloonEditor', () => {
  98. expect( editor ).to.be.instanceof( BalloonEditor );
  99. } );
  100. it( 'creates element–less UI view', () => {
  101. expect( editor.ui.view.element ).to.be.null;
  102. } );
  103. it( 'attaches editable UI as view\'s DOM root', () => {
  104. const domRoot = editor.editing.view.getDomRoot();
  105. expect( domRoot ).to.equal( editor.ui.view.editable.element );
  106. } );
  107. it( 'creates the UI using BalloonEditorUI classes', () => {
  108. expect( editor.ui ).to.be.instanceof( BalloonEditorUI );
  109. expect( editor.ui.view ).to.be.instanceof( BalloonEditorUIView );
  110. } );
  111. it( 'loads data from the editor element', () => {
  112. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  113. } );
  114. it( 'should not require config object', () => {
  115. const editorElement = document.createElement( 'div' );
  116. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  117. // Just being safe with `builtinPlugins` static property.
  118. class CustomBalloonEditor extends BalloonEditor {}
  119. CustomBalloonEditor.builtinPlugins = [ Paragraph, Bold ];
  120. return CustomBalloonEditor.create( editorElement )
  121. .then( newEditor => {
  122. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  123. return newEditor.destroy();
  124. } )
  125. .then( () => {
  126. editorElement.remove();
  127. } );
  128. } );
  129. it( 'allows to pass data to the constructor', () => {
  130. return BalloonEditor.create( '<p>Hello world!</p>', {
  131. plugins: [ Paragraph ]
  132. } ).then( editor => {
  133. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  134. editor.destroy();
  135. } );
  136. } );
  137. it( 'initializes with config.initialData', () => {
  138. const editorElement = document.createElement( 'div' );
  139. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  140. return BalloonEditor.create( editorElement, {
  141. initialData: '<p>Hello world!</p>',
  142. plugins: [ Paragraph ]
  143. } ).then( editor => {
  144. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  145. return editor.destroy();
  146. } ).then( () => {
  147. editorElement.remove();
  148. } );
  149. } );
  150. it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => {
  151. BalloonEditor.create( '<p>Hello world!</p>', {
  152. initialData: '<p>I am evil!</p>',
  153. plugins: [ Paragraph ]
  154. } )
  155. .then(
  156. () => {
  157. expect.fail( 'Balloon editor should throw an error when both initial data are passed' );
  158. },
  159. err => {
  160. assertCKEditorError( err, 'editor-create-initial-data', null );
  161. }
  162. )
  163. .then( () => {
  164. removeEditorBodyOrphans();
  165. } )
  166. .then( done )
  167. .catch( done );
  168. } );
  169. // ckeditor/ckeditor5-editor-classic#53
  170. it( 'creates an instance of a BalloonEditor child class', () => {
  171. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  172. // editor being initialized on one element.
  173. const editorElement = document.createElement( 'div' );
  174. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  175. document.body.appendChild( editorElement );
  176. class CustomBalloonEditor extends BalloonEditor {}
  177. return CustomBalloonEditor
  178. .create( editorElement, {
  179. plugins: [ Paragraph, Bold ]
  180. } )
  181. .then( newEditor => {
  182. expect( newEditor ).to.be.instanceof( CustomBalloonEditor );
  183. expect( newEditor ).to.be.instanceof( BalloonEditor );
  184. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  185. editorElement.remove();
  186. return newEditor.destroy();
  187. } );
  188. } );
  189. it( 'throws an error when is initialized in textarea', done => {
  190. BalloonEditor.create( document.createElement( 'textarea' ) )
  191. .then(
  192. () => {
  193. expect.fail( 'Balloon editor should throw an error when is initialized in textarea.' );
  194. },
  195. err => {
  196. assertCKEditorError( err, 'editor-wrong-element', null );
  197. }
  198. )
  199. .then( done )
  200. .catch( done );
  201. } );
  202. } );
  203. describe( 'create - events', () => {
  204. afterEach( () => {
  205. return editor.destroy();
  206. } );
  207. it( 'fires all events in the right order', () => {
  208. const fired = [];
  209. function spy( evt ) {
  210. fired.push( `${ evt.name }-${ evt.source.constructor.name.toLowerCase() }` );
  211. }
  212. class EventWatcher extends Plugin {
  213. init() {
  214. this.editor.ui.on( 'ready', spy );
  215. this.editor.data.on( 'ready', spy );
  216. this.editor.on( 'ready', spy );
  217. }
  218. }
  219. return BalloonEditor
  220. .create( editorElement, {
  221. plugins: [ EventWatcher ]
  222. } )
  223. .then( newEditor => {
  224. expect( fired ).to.deep.equal(
  225. [ 'ready-ballooneditorui', 'ready-datacontroller', 'ready-ballooneditor' ] );
  226. editor = newEditor;
  227. } );
  228. } );
  229. it( 'fires ready once UI is ready', () => {
  230. let isRendered;
  231. class EventWatcher extends Plugin {
  232. init() {
  233. this.editor.ui.on( 'ready', () => {
  234. isRendered = this.editor.ui.view.isRendered;
  235. } );
  236. }
  237. }
  238. return BalloonEditor
  239. .create( editorElement, {
  240. plugins: [ EventWatcher ]
  241. } )
  242. .then( newEditor => {
  243. expect( isRendered ).to.be.true;
  244. editor = newEditor;
  245. } );
  246. } );
  247. } );
  248. describe( 'destroy()', () => {
  249. beforeEach( function() {
  250. return BalloonEditor
  251. .create( editorElement, { plugins: [ Paragraph ] } )
  252. .then( newEditor => {
  253. editor = newEditor;
  254. const schema = editor.model.schema;
  255. schema.register( 'heading' );
  256. schema.extend( 'heading', { allowIn: '$root' } );
  257. schema.extend( '$text', { allowIn: 'heading' } );
  258. editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading', view: 'heading' } );
  259. editor.conversion.for( 'dataDowncast' ).elementToElement( { model: 'heading', view: 'heading' } );
  260. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  261. model: 'heading',
  262. view: 'heading-editing-representation'
  263. } );
  264. } );
  265. } );
  266. it( 'sets the data back to the editor element', () => {
  267. editor.setData( '<p>a</p><heading>b</heading>' );
  268. return editor.destroy()
  269. .then( () => {
  270. expect( editorElement.innerHTML )
  271. .to.equal( '<p>a</p><heading>b</heading>' );
  272. } );
  273. } );
  274. it( 'should not throw an error if editor was initialized with the data', async () => {
  275. await editor.destroy();
  276. return BalloonEditor
  277. .create( '<p>Foo.</p>', {
  278. plugins: [ Paragraph, Bold ]
  279. } )
  280. .then( newEditor => newEditor.destroy() );
  281. } );
  282. } );
  283. describeMemoryUsage( () => {
  284. testMemoryUsage(
  285. 'should not grow on multiple create/destroy',
  286. () => BalloonEditor
  287. .create( document.querySelector( '#mem-editor' ), {
  288. plugins: [ ArticlePluginSet ],
  289. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  290. image: {
  291. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  292. }
  293. } ) );
  294. } );
  295. } );