ballooneditor.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, Event */
  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. describe( 'BalloonEditor', () => {
  20. let editor, editorElement;
  21. testUtils.createSinonSandbox();
  22. beforeEach( () => {
  23. editorElement = document.createElement( 'div' );
  24. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  25. document.body.appendChild( editorElement );
  26. testUtils.sinon.stub( log, 'warn' ).callsFake( () => {} );
  27. } );
  28. afterEach( () => {
  29. editorElement.remove();
  30. } );
  31. describe( 'constructor()', () => {
  32. beforeEach( () => {
  33. editor = new BalloonEditor( editorElement, {
  34. plugins: [ BalloonToolbar, Bold ],
  35. toolbar: [ 'Bold' ]
  36. } );
  37. } );
  38. it( 'pushes BalloonToolbar to the list of plugins', () => {
  39. expect( editor.config.get( 'plugins' ) ).to.include( BalloonToolbar );
  40. } );
  41. it( 'pipes config#toolbar to config#balloonToolbar', () => {
  42. expect( editor.config.get( 'balloonToolbar' ) ).to.have.members( [ 'Bold' ] );
  43. } );
  44. it( 'uses HTMLDataProcessor', () => {
  45. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  46. } );
  47. it( 'has a Data Interface', () => {
  48. testUtils.isMixed( BalloonEditor, DataApiMixin );
  49. } );
  50. it( 'has a Element Interface', () => {
  51. testUtils.isMixed( BalloonEditor, ElementApiMixin );
  52. } );
  53. it( 'creates main root element', () => {
  54. expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
  55. } );
  56. it( 'handles form element', () => {
  57. const form = document.createElement( 'form' );
  58. const textarea = document.createElement( 'textarea' );
  59. form.appendChild( textarea );
  60. document.body.appendChild( form );
  61. // Prevents page realods in Firefox ;|
  62. form.addEventListener( 'submit', evt => {
  63. evt.preventDefault();
  64. } );
  65. return BalloonEditor.create( textarea, {
  66. plugins: [ Paragraph ]
  67. } ).then( editor => {
  68. expect( textarea.value ).to.equal( '' );
  69. editor.setData( '<p>Foo</p>' );
  70. form.dispatchEvent( new Event( 'submit', {
  71. // We need to be able to do preventDefault() to prevent page reloads in Firefox.
  72. cancelable: true
  73. } ) );
  74. expect( textarea.value ).to.equal( '<p>Foo</p>' );
  75. return editor.destroy().then( () => {
  76. form.remove();
  77. } );
  78. } );
  79. } );
  80. it( 'allows to pass data to the constructor', () => {
  81. return BalloonEditor.create( '<p>Hello world!</p>', {
  82. plugins: [ Paragraph ]
  83. } ).then( editor => {
  84. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  85. } );
  86. } );
  87. it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
  88. return BalloonEditor
  89. .create( '<p>Foo.</p>', {
  90. plugins: [ Paragraph, Bold ]
  91. } )
  92. .then( newEditor => {
  93. expect( newEditor.sourceElement ).to.be.undefined;
  94. return newEditor.destroy();
  95. } );
  96. } );
  97. } );
  98. describe( 'create()', () => {
  99. beforeEach( function() {
  100. return BalloonEditor
  101. .create( editorElement, {
  102. plugins: [ Paragraph, Bold ]
  103. } )
  104. .then( newEditor => {
  105. editor = newEditor;
  106. } );
  107. } );
  108. afterEach( () => {
  109. return editor.destroy();
  110. } );
  111. it( 'creates an instance which inherits from the BalloonEditor', () => {
  112. expect( editor ).to.be.instanceof( BalloonEditor );
  113. } );
  114. it( 'creates element–less UI view', () => {
  115. expect( editor.ui.view.element ).to.be.null;
  116. } );
  117. it( 'attaches editable UI as view\'s DOM root', () => {
  118. const domRoot = editor.editing.view.getDomRoot();
  119. expect( domRoot ).to.equal( editor.ui.view.editable.element );
  120. } );
  121. it( 'creates the UI using BalloonEditorUI classes', () => {
  122. expect( editor.ui ).to.be.instanceof( BalloonEditorUI );
  123. expect( editor.ui.view ).to.be.instanceof( BalloonEditorUIView );
  124. } );
  125. it( 'loads data from the editor element', () => {
  126. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  127. } );
  128. // ckeditor/ckeditor5-editor-classic#53
  129. it( 'creates an instance of a BalloonEditor child class', () => {
  130. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  131. // editor being initialized on one element.
  132. const editorElement = document.createElement( 'div' );
  133. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  134. document.body.appendChild( editorElement );
  135. class CustomBalloonEditor extends BalloonEditor {}
  136. return CustomBalloonEditor
  137. .create( editorElement, {
  138. plugins: [ Paragraph, Bold ]
  139. } )
  140. .then( newEditor => {
  141. expect( newEditor ).to.be.instanceof( CustomBalloonEditor );
  142. expect( newEditor ).to.be.instanceof( BalloonEditor );
  143. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  144. editorElement.remove();
  145. return newEditor.destroy();
  146. } );
  147. } );
  148. } );
  149. describe( 'create - events', () => {
  150. afterEach( () => {
  151. return editor.destroy();
  152. } );
  153. it( 'fires all events in the right order', () => {
  154. const fired = [];
  155. function spy( evt ) {
  156. fired.push( evt.name );
  157. }
  158. class EventWatcher extends Plugin {
  159. init() {
  160. this.editor.on( 'pluginsReady', spy );
  161. this.editor.ui.on( 'ready', spy );
  162. this.editor.on( 'dataReady', spy );
  163. this.editor.on( 'ready', spy );
  164. }
  165. }
  166. return BalloonEditor
  167. .create( editorElement, {
  168. plugins: [ EventWatcher ]
  169. } )
  170. .then( newEditor => {
  171. expect( fired ).to.deep.equal( [ 'pluginsReady', 'ready', 'dataReady', 'ready' ] );
  172. editor = newEditor;
  173. } );
  174. } );
  175. it( 'fires dataReady once data is loaded', () => {
  176. let data;
  177. class EventWatcher extends Plugin {
  178. init() {
  179. this.editor.on( 'dataReady', () => {
  180. data = this.editor.getData();
  181. } );
  182. }
  183. }
  184. return BalloonEditor
  185. .create( editorElement, {
  186. plugins: [ EventWatcher, Paragraph, Bold ]
  187. } )
  188. .then( newEditor => {
  189. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  190. editor = newEditor;
  191. } );
  192. } );
  193. it( 'fires ready once UI is ready', () => {
  194. let isRendered;
  195. class EventWatcher extends Plugin {
  196. init() {
  197. this.editor.ui.on( 'ready', () => {
  198. isRendered = this.editor.ui.view.isRendered;
  199. } );
  200. }
  201. }
  202. return BalloonEditor
  203. .create( editorElement, {
  204. plugins: [ EventWatcher ]
  205. } )
  206. .then( newEditor => {
  207. expect( isRendered ).to.be.true;
  208. editor = newEditor;
  209. } );
  210. } );
  211. } );
  212. describe( 'destroy()', () => {
  213. beforeEach( function() {
  214. return BalloonEditor
  215. .create( editorElement, { plugins: [ Paragraph ] } )
  216. .then( newEditor => {
  217. editor = newEditor;
  218. const schema = editor.model.schema;
  219. schema.register( 'heading' );
  220. schema.extend( 'heading', { allowIn: '$root' } );
  221. schema.extend( '$text', { allowIn: 'heading' } );
  222. editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading', view: 'heading' } );
  223. editor.conversion.for( 'dataDowncast' ).elementToElement( { model: 'heading', view: 'heading' } );
  224. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  225. model: 'heading',
  226. view: 'heading-editing-representation'
  227. } );
  228. } );
  229. } );
  230. it( 'sets the data back to the editor element', () => {
  231. editor.setData( '<p>a</p><heading>b</heading>' );
  232. return editor.destroy()
  233. .then( () => {
  234. expect( editorElement.innerHTML )
  235. .to.equal( '<p>a</p><heading>b</heading>' );
  236. } );
  237. } );
  238. it( 'should not throw an error if editor was initialized with the data', () => {
  239. return BalloonEditor
  240. .create( '<p>Foo.</p>', {
  241. plugins: [ Paragraph, Bold ]
  242. } )
  243. .then( newEditor => newEditor.destroy() );
  244. } );
  245. } );
  246. } );