8
0

ballooneditor.js 11 KB

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