inlineeditor.js 8.0 KB

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