inlineeditor.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. * @license Copyright (c) 2003-2019, 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( 'should have undefined the #sourceElement if editor was initialized with data', () => {
  77. return InlineEditor.create( '<p>Hello world!</p>', {
  78. plugins: [ Paragraph ]
  79. } ).then( editor => {
  80. expect( editor.sourceElement ).to.be.undefined;
  81. } );
  82. } );
  83. it( 'editor.ui.element should contain the whole editor (with UI) element', () => {
  84. return InlineEditor.create( '<p>Hello world!</p>', {
  85. plugins: [ Paragraph ]
  86. } ).then( editor => {
  87. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.element );
  88. } );
  89. } );
  90. } );
  91. describe( 'create()', () => {
  92. beforeEach( function() {
  93. return InlineEditor
  94. .create( editorElement, {
  95. plugins: [ Paragraph, Bold ]
  96. } )
  97. .then( newEditor => {
  98. editor = newEditor;
  99. } );
  100. } );
  101. afterEach( () => {
  102. return editor.destroy();
  103. } );
  104. it( 'creates an instance which inherits from the InlineEditor', () => {
  105. expect( editor ).to.be.instanceof( InlineEditor );
  106. } );
  107. it( 'creates element–less UI view', () => {
  108. expect( editor.ui.view.element ).to.be.null;
  109. } );
  110. it( 'attaches editable UI as view\'s DOM root', () => {
  111. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  112. } );
  113. it( 'loads data from the editor element', () => {
  114. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  115. } );
  116. it( 'should not require config object', () => {
  117. // Just being safe with `builtinPlugins` static property.
  118. class CustomInlineEditor extends InlineEditor {}
  119. CustomInlineEditor.builtinPlugins = [ Paragraph, Bold ];
  120. return CustomInlineEditor.create( editorElement )
  121. .then( newEditor => {
  122. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  123. return newEditor.destroy();
  124. } );
  125. } );
  126. it( 'allows to pass data to the constructor', () => {
  127. return InlineEditor.create( '<p>Hello world!</p>', {
  128. plugins: [ Paragraph ]
  129. } ).then( editor => {
  130. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  131. editor.destroy();
  132. } );
  133. } );
  134. it( 'initializes with config.initialData', () => {
  135. return InlineEditor.create( editorElement, {
  136. initialData: '<p>Hello world!</p>',
  137. plugins: [ Paragraph ]
  138. } ).then( editor => {
  139. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  140. editor.destroy();
  141. } );
  142. } );
  143. it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => {
  144. InlineEditor.create( '<p>Hello world!</p>', {
  145. initialData: '<p>I am evil!</p>',
  146. plugins: [ Paragraph ]
  147. } ).catch( () => {
  148. done();
  149. } );
  150. } );
  151. // #25
  152. it( 'creates an instance of a InlineEditor child class', () => {
  153. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  154. // editor being initialized on one element.
  155. const editorElement = document.createElement( 'div' );
  156. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  157. document.body.appendChild( editorElement );
  158. class CustomInlineEditor extends InlineEditor {}
  159. return CustomInlineEditor
  160. .create( editorElement, {
  161. plugins: [ Paragraph, Bold ]
  162. } )
  163. .then( newEditor => {
  164. expect( newEditor ).to.be.instanceof( CustomInlineEditor );
  165. expect( newEditor ).to.be.instanceof( InlineEditor );
  166. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  167. editorElement.remove();
  168. return newEditor.destroy();
  169. } );
  170. } );
  171. } );
  172. describe( 'create - events', () => {
  173. afterEach( () => {
  174. return editor.destroy();
  175. } );
  176. it( 'fires all events in the right order', () => {
  177. const fired = [];
  178. function spy( evt ) {
  179. fired.push( `${ evt.name }-${ evt.source.constructor.name.toLowerCase() }` );
  180. }
  181. class EventWatcher extends Plugin {
  182. init() {
  183. this.editor.ui.on( 'ready', spy );
  184. this.editor.data.on( 'ready', spy );
  185. this.editor.on( 'ready', spy );
  186. }
  187. }
  188. return InlineEditor
  189. .create( editorElement, {
  190. plugins: [ EventWatcher ]
  191. } )
  192. .then( newEditor => {
  193. expect( fired ).to.deep.equal( [
  194. 'ready-inlineeditorui', 'ready-datacontroller', 'ready-inlineeditor' ] );
  195. editor = newEditor;
  196. } );
  197. } );
  198. it( 'fires ready once UI is ready', () => {
  199. let isReady;
  200. class EventWatcher extends Plugin {
  201. init() {
  202. this.editor.ui.on( 'ready', () => {
  203. isReady = this.editor.ui.view.isRendered;
  204. } );
  205. }
  206. }
  207. return InlineEditor
  208. .create( editorElement, {
  209. plugins: [ EventWatcher ]
  210. } )
  211. .then( newEditor => {
  212. expect( isReady ).to.be.true;
  213. editor = newEditor;
  214. } );
  215. } );
  216. } );
  217. describe( 'destroy', () => {
  218. beforeEach( function() {
  219. return InlineEditor
  220. .create( editorElement, { plugins: [ Paragraph ] } )
  221. .then( newEditor => {
  222. editor = newEditor;
  223. const schema = editor.model.schema;
  224. schema.register( 'heading' );
  225. schema.extend( 'heading', { allowIn: '$root' } );
  226. schema.extend( '$text', { allowIn: 'heading' } );
  227. editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading', view: 'heading' } );
  228. editor.conversion.for( 'dataDowncast' ).elementToElement( { model: 'heading', view: 'heading' } );
  229. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  230. model: 'heading',
  231. view: 'heading-editing-representation'
  232. } );
  233. } );
  234. } );
  235. it( 'sets the data back to the editor element', () => {
  236. editor.setData( '<p>a</p><heading>b</heading>' );
  237. return editor.destroy()
  238. .then( () => {
  239. expect( editorElement.innerHTML )
  240. .to.equal( '<p>a</p><heading>b</heading>' );
  241. } );
  242. } );
  243. it( 'should not throw an error if editor was initialized with the data', () => {
  244. return InlineEditor
  245. .create( '<p>Foo.</p>', {
  246. plugins: [ Paragraph, Bold ]
  247. } )
  248. .then( newEditor => newEditor.destroy() );
  249. } );
  250. } );
  251. describeMemoryUsage( () => {
  252. testMemoryUsage(
  253. 'should not grow on multiple create/destroy',
  254. () => InlineEditor
  255. .create( document.querySelector( '#mem-editor' ), {
  256. plugins: [ ArticlePluginSet ],
  257. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  258. image: {
  259. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  260. }
  261. } ) );
  262. } );
  263. } );