ballooneditor.js 11 KB

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