ballooneditor.js 8.6 KB

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