ballooneditor.js 11 KB

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