8
0

ballooneditor.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. 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.ui.on( 'ready', spy );
  170. this.editor.on( 'uiReady', spy );
  171. this.editor.on( 'dataReady', spy );
  172. this.editor.on( 'ready', spy );
  173. }
  174. }
  175. return BalloonEditor
  176. .create( editorElement, {
  177. plugins: [ EventWatcher ]
  178. } )
  179. .then( newEditor => {
  180. expect( fired ).to.deep.equal( [ 'pluginsReady', 'ready', 'uiReady', 'dataReady', 'ready' ] );
  181. editor = newEditor;
  182. } );
  183. } );
  184. it( 'fires dataReady once data is loaded', () => {
  185. let data;
  186. class EventWatcher extends Plugin {
  187. init() {
  188. this.editor.on( 'dataReady', () => {
  189. data = this.editor.getData();
  190. } );
  191. }
  192. }
  193. return BalloonEditor
  194. .create( editorElement, {
  195. plugins: [ EventWatcher, Paragraph, Bold ]
  196. } )
  197. .then( newEditor => {
  198. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  199. editor = newEditor;
  200. } );
  201. } );
  202. it( 'fires uiReady once UI is ready', () => {
  203. let isRendered;
  204. class EventWatcher extends Plugin {
  205. init() {
  206. this.editor.on( 'uiReady', () => {
  207. isRendered = this.editor.ui.view.isRendered;
  208. } );
  209. }
  210. }
  211. return BalloonEditor
  212. .create( editorElement, {
  213. plugins: [ EventWatcher ]
  214. } )
  215. .then( newEditor => {
  216. expect( isRendered ).to.be.true;
  217. editor = newEditor;
  218. } );
  219. } );
  220. it( 'fires ready once UI is ready', () => {
  221. let isRendered;
  222. class EventWatcher extends Plugin {
  223. init() {
  224. this.editor.ui.on( 'ready', () => {
  225. isRendered = this.editor.ui.view.isRendered;
  226. } );
  227. }
  228. }
  229. return BalloonEditor
  230. .create( editorElement, {
  231. plugins: [ EventWatcher ]
  232. } )
  233. .then( newEditor => {
  234. expect( isRendered ).to.be.true;
  235. editor = newEditor;
  236. } );
  237. } );
  238. } );
  239. describe( 'destroy()', () => {
  240. beforeEach( function() {
  241. return BalloonEditor
  242. .create( editorElement, { plugins: [ Paragraph ] } )
  243. .then( newEditor => {
  244. editor = newEditor;
  245. const schema = editor.model.schema;
  246. schema.register( 'heading' );
  247. schema.extend( 'heading', { allowIn: '$root' } );
  248. schema.extend( '$text', { allowIn: 'heading' } );
  249. editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading', view: 'heading' } );
  250. editor.conversion.for( 'dataDowncast' ).elementToElement( { model: 'heading', view: 'heading' } );
  251. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  252. model: 'heading',
  253. view: 'heading-editing-representation'
  254. } );
  255. } );
  256. } );
  257. it( 'sets the data back to the editor element', () => {
  258. editor.setData( '<p>a</p><heading>b</heading>' );
  259. return editor.destroy()
  260. .then( () => {
  261. expect( editorElement.innerHTML )
  262. .to.equal( '<p>a</p><heading>b</heading>' );
  263. } );
  264. } );
  265. it( 'should not throw an error if editor was initialized with the data', () => {
  266. return BalloonEditor
  267. .create( '<p>Foo.</p>', {
  268. plugins: [ Paragraph, Bold ]
  269. } )
  270. .then( newEditor => newEditor.destroy() );
  271. } );
  272. } );
  273. } );