inlineeditor.js 7.2 KB

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