ballooneditor.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  10. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  11. import BalloonEditor from '../src/ballooneditor';
  12. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  15. import BalloonToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/balloon/balloontoolbar';
  16. import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
  17. import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin';
  18. import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
  19. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  20. describe( 'BalloonEditor', () => {
  21. let editor, editorElement;
  22. testUtils.createSinonSandbox();
  23. beforeEach( () => {
  24. editorElement = document.createElement( 'div' );
  25. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  26. document.body.appendChild( editorElement );
  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. it( 'editor.element should contain the whole editor (with UI) element', () => {
  98. return BalloonEditor.create( '<p>Hello world!</p>', {
  99. plugins: [ Paragraph ]
  100. } ).then( editor => {
  101. expect( editor.editing.view.getDomRoot() ).to.equal( editor.element );
  102. } );
  103. } );
  104. } );
  105. describe( 'create()', () => {
  106. beforeEach( function() {
  107. return BalloonEditor
  108. .create( editorElement, {
  109. plugins: [ Paragraph, Bold ]
  110. } )
  111. .then( newEditor => {
  112. editor = newEditor;
  113. } );
  114. } );
  115. afterEach( () => {
  116. return editor.destroy();
  117. } );
  118. it( 'creates an instance which inherits from the BalloonEditor', () => {
  119. expect( editor ).to.be.instanceof( BalloonEditor );
  120. } );
  121. it( 'creates element–less UI view', () => {
  122. expect( editor.ui.view.element ).to.be.null;
  123. } );
  124. it( 'attaches editable UI as view\'s DOM root', () => {
  125. const domRoot = editor.editing.view.getDomRoot();
  126. expect( domRoot ).to.equal( editor.element );
  127. expect( domRoot ).to.equal( editor.ui.view.editable.element );
  128. } );
  129. it( 'creates the UI using BalloonEditorUI classes', () => {
  130. expect( editor.ui ).to.be.instanceof( BalloonEditorUI );
  131. expect( editor.ui.view ).to.be.instanceof( BalloonEditorUIView );
  132. } );
  133. it( 'loads data from the editor element', () => {
  134. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  135. } );
  136. // ckeditor/ckeditor5-editor-classic#53
  137. it( 'creates an instance of a BalloonEditor child class', () => {
  138. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  139. // editor being initialized on one element.
  140. const editorElement = document.createElement( 'div' );
  141. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  142. document.body.appendChild( editorElement );
  143. class CustomBalloonEditor extends BalloonEditor {}
  144. return CustomBalloonEditor
  145. .create( editorElement, {
  146. plugins: [ Paragraph, Bold ]
  147. } )
  148. .then( newEditor => {
  149. expect( newEditor ).to.be.instanceof( CustomBalloonEditor );
  150. expect( newEditor ).to.be.instanceof( BalloonEditor );
  151. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  152. editorElement.remove();
  153. return newEditor.destroy();
  154. } );
  155. } );
  156. } );
  157. describe( 'create - events', () => {
  158. afterEach( () => {
  159. return editor.destroy();
  160. } );
  161. it( 'fires all events in the right order', () => {
  162. const fired = [];
  163. function spy( evt ) {
  164. fired.push( evt.name );
  165. }
  166. class EventWatcher extends Plugin {
  167. init() {
  168. this.editor.on( 'pluginsReady', spy );
  169. this.editor.on( 'uiReady', spy );
  170. this.editor.on( 'dataReady', spy );
  171. this.editor.on( 'ready', spy );
  172. }
  173. }
  174. return BalloonEditor
  175. .create( editorElement, {
  176. plugins: [ EventWatcher ]
  177. } )
  178. .then( newEditor => {
  179. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  180. editor = newEditor;
  181. } );
  182. } );
  183. it( 'fires dataReady once data is loaded', () => {
  184. let data;
  185. class EventWatcher extends Plugin {
  186. init() {
  187. this.editor.on( 'dataReady', () => {
  188. data = this.editor.getData();
  189. } );
  190. }
  191. }
  192. return BalloonEditor
  193. .create( editorElement, {
  194. plugins: [ EventWatcher, Paragraph, Bold ]
  195. } )
  196. .then( newEditor => {
  197. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  198. editor = newEditor;
  199. } );
  200. } );
  201. it( 'fires uiReady once UI is ready', () => {
  202. let isRendered;
  203. class EventWatcher extends Plugin {
  204. init() {
  205. this.editor.on( 'uiReady', () => {
  206. isRendered = this.editor.ui.view.isRendered;
  207. } );
  208. }
  209. }
  210. return BalloonEditor
  211. .create( editorElement, {
  212. plugins: [ EventWatcher ]
  213. } )
  214. .then( newEditor => {
  215. expect( isRendered ).to.be.true;
  216. editor = newEditor;
  217. } );
  218. } );
  219. } );
  220. describe( 'destroy()', () => {
  221. beforeEach( function() {
  222. return BalloonEditor
  223. .create( editorElement, { plugins: [ Paragraph ] } )
  224. .then( newEditor => {
  225. editor = newEditor;
  226. const schema = editor.model.schema;
  227. schema.register( 'heading' );
  228. schema.extend( 'heading', { allowIn: '$root' } );
  229. schema.extend( '$text', { allowIn: 'heading' } );
  230. editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'heading', view: 'heading' } ) );
  231. editor.conversion.for( 'dataDowncast' ).add( downcastElementToElement( { model: 'heading', view: 'heading' } ) );
  232. editor.conversion.for( 'editingDowncast' ).add( downcastElementToElement( {
  233. model: 'heading',
  234. view: 'heading-editing-representation'
  235. } ) );
  236. } );
  237. } );
  238. it( 'sets the data back to the editor element', () => {
  239. editor.setData( '<p>a</p><heading>b</heading>' );
  240. return editor.destroy()
  241. .then( () => {
  242. expect( editorElement.innerHTML )
  243. .to.equal( '<p>a</p><heading>b</heading>' );
  244. } );
  245. } );
  246. it( 'should not throw an error if editor was initialized with the data', () => {
  247. return BalloonEditor
  248. .create( '<p>Foo.</p>', {
  249. plugins: [ Paragraph, Bold ]
  250. } )
  251. .then( newEditor => newEditor.destroy() );
  252. } );
  253. } );
  254. } );