inlineeditor.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. expect( testUtils.isMixed( InlineEditor, DataApiMixin ) ).to.be.true;
  43. } );
  44. it( 'has a Element Interface', () => {
  45. expect( testUtils.isMixed( InlineEditor, ElementApiMixin ) ).to.be.true;
  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. it( 'allows to pass data to the constructor', () => {
  75. return InlineEditor.create( '<p>Hello world!</p>', {
  76. plugins: [ Paragraph ]
  77. } ).then( editor => {
  78. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  79. } );
  80. } );
  81. it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
  82. return InlineEditor.create( '<p>Hello world!</p>', {
  83. plugins: [ Paragraph ]
  84. } ).then( editor => {
  85. expect( editor.sourceElement ).to.be.undefined;
  86. } );
  87. } );
  88. it( 'editor.element should contain the whole editor (with UI) element', () => {
  89. return InlineEditor.create( '<p>Hello world!</p>', {
  90. plugins: [ Paragraph ]
  91. } ).then( editor => {
  92. expect( editor.editing.view.getDomRoot() ).to.equal( editor.element );
  93. } );
  94. } );
  95. } );
  96. describe( 'create()', () => {
  97. beforeEach( function() {
  98. return InlineEditor
  99. .create( editorElement, {
  100. plugins: [ Paragraph, Bold ]
  101. } )
  102. .then( newEditor => {
  103. editor = newEditor;
  104. } );
  105. } );
  106. afterEach( () => {
  107. return editor.destroy();
  108. } );
  109. it( 'creates an instance which inherits from the InlineEditor', () => {
  110. expect( editor ).to.be.instanceof( InlineEditor );
  111. } );
  112. it( 'creates element–less UI view', () => {
  113. expect( editor.ui.view.element ).to.be.null;
  114. } );
  115. it( 'attaches editable UI as view\'s DOM root', () => {
  116. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  117. } );
  118. it( 'loads data from the editor element', () => {
  119. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  120. } );
  121. // #25
  122. it( 'creates an instance of a InlineEditor child class', () => {
  123. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  124. // editor being initialized on one element.
  125. const editorElement = document.createElement( 'div' );
  126. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  127. document.body.appendChild( editorElement );
  128. class CustomInlineEditor extends InlineEditor {}
  129. return CustomInlineEditor
  130. .create( editorElement, {
  131. plugins: [ Paragraph, Bold ]
  132. } )
  133. .then( newEditor => {
  134. expect( newEditor ).to.be.instanceof( CustomInlineEditor );
  135. expect( newEditor ).to.be.instanceof( InlineEditor );
  136. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  137. editorElement.remove();
  138. return newEditor.destroy();
  139. } );
  140. } );
  141. } );
  142. describe( 'create - events', () => {
  143. afterEach( () => {
  144. return editor.destroy();
  145. } );
  146. it( 'fires all events in the right order', () => {
  147. const fired = [];
  148. function spy( evt ) {
  149. fired.push( evt.name );
  150. }
  151. class EventWatcher extends Plugin {
  152. init() {
  153. this.editor.on( 'pluginsReady', spy );
  154. this.editor.on( 'uiReady', spy );
  155. this.editor.on( 'dataReady', spy );
  156. this.editor.on( 'ready', spy );
  157. }
  158. }
  159. return InlineEditor
  160. .create( editorElement, {
  161. plugins: [ EventWatcher ]
  162. } )
  163. .then( newEditor => {
  164. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  165. editor = newEditor;
  166. } );
  167. } );
  168. it( 'fires dataReady once data is loaded', () => {
  169. let data;
  170. class EventWatcher extends Plugin {
  171. init() {
  172. this.editor.on( 'dataReady', () => {
  173. data = this.editor.getData();
  174. } );
  175. }
  176. }
  177. return InlineEditor
  178. .create( editorElement, {
  179. plugins: [ EventWatcher, Paragraph, Bold ]
  180. } )
  181. .then( newEditor => {
  182. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  183. editor = newEditor;
  184. } );
  185. } );
  186. it( 'fires uiReady once UI is ready', () => {
  187. let isReady;
  188. class EventWatcher extends Plugin {
  189. init() {
  190. this.editor.on( 'uiReady', () => {
  191. isReady = this.editor.ui.view.isRendered;
  192. } );
  193. }
  194. }
  195. return InlineEditor
  196. .create( editorElement, {
  197. plugins: [ EventWatcher ]
  198. } )
  199. .then( newEditor => {
  200. expect( isReady ).to.be.true;
  201. editor = newEditor;
  202. } );
  203. } );
  204. } );
  205. describe( 'destroy', () => {
  206. beforeEach( function() {
  207. return InlineEditor
  208. .create( editorElement, { plugins: [ Paragraph ] } )
  209. .then( newEditor => {
  210. editor = newEditor;
  211. const schema = editor.model.schema;
  212. schema.register( 'heading' );
  213. schema.extend( 'heading', { allowIn: '$root' } );
  214. schema.extend( '$text', { allowIn: 'heading' } );
  215. editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'heading', view: 'heading' } ) );
  216. editor.conversion.for( 'dataDowncast' ).add( downcastElementToElement( { model: 'heading', view: 'heading' } ) );
  217. editor.conversion.for( 'editingDowncast' ).add( downcastElementToElement( {
  218. model: 'heading',
  219. view: 'heading-editing-representation'
  220. } ) );
  221. } );
  222. } );
  223. it( 'sets the data back to the editor element', () => {
  224. editor.setData( '<p>a</p><heading>b</heading>' );
  225. return editor.destroy()
  226. .then( () => {
  227. expect( editorElement.innerHTML )
  228. .to.equal( '<p>a</p><heading>b</heading>' );
  229. } );
  230. } );
  231. it( 'should not throw an error if editor was initialized with the data', () => {
  232. return InlineEditor
  233. .create( '<p>Foo.</p>', {
  234. plugins: [ Paragraph, Bold ]
  235. } )
  236. .then( newEditor => newEditor.destroy() );
  237. } );
  238. } );
  239. } );