inlineeditor.js 8.9 KB

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