inlineeditor.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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.ui.on( 'ready', spy );
  156. this.editor.data.on( 'ready', spy );
  157. this.editor.on( 'ready', spy );
  158. }
  159. }
  160. return InlineEditor
  161. .create( editorElement, {
  162. plugins: [ EventWatcher ]
  163. } )
  164. .then( newEditor => {
  165. expect( fired ).to.deep.equal( [
  166. 'ready-inlineeditorui', 'ready-datacontroller', 'ready-inlineeditor' ] );
  167. editor = newEditor;
  168. } );
  169. } );
  170. it( 'fires ready once UI is ready', () => {
  171. let isReady;
  172. class EventWatcher extends Plugin {
  173. init() {
  174. this.editor.ui.on( 'ready', () => {
  175. isReady = this.editor.ui.view.isRendered;
  176. } );
  177. }
  178. }
  179. return InlineEditor
  180. .create( editorElement, {
  181. plugins: [ EventWatcher ]
  182. } )
  183. .then( newEditor => {
  184. expect( isReady ).to.be.true;
  185. editor = newEditor;
  186. } );
  187. } );
  188. } );
  189. describe( 'destroy', () => {
  190. beforeEach( function() {
  191. return InlineEditor
  192. .create( editorElement, { plugins: [ Paragraph ] } )
  193. .then( newEditor => {
  194. editor = newEditor;
  195. const schema = editor.model.schema;
  196. schema.register( 'heading' );
  197. schema.extend( 'heading', { allowIn: '$root' } );
  198. schema.extend( '$text', { allowIn: 'heading' } );
  199. editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading', view: 'heading' } );
  200. editor.conversion.for( 'dataDowncast' ).elementToElement( { model: 'heading', view: 'heading' } );
  201. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  202. model: 'heading',
  203. view: 'heading-editing-representation'
  204. } );
  205. } );
  206. } );
  207. it( 'sets the data back to the editor element', () => {
  208. editor.setData( '<p>a</p><heading>b</heading>' );
  209. return editor.destroy()
  210. .then( () => {
  211. expect( editorElement.innerHTML )
  212. .to.equal( '<p>a</p><heading>b</heading>' );
  213. } );
  214. } );
  215. it( 'should not throw an error if editor was initialized with the data', () => {
  216. return InlineEditor
  217. .create( '<p>Foo.</p>', {
  218. plugins: [ Paragraph, Bold ]
  219. } )
  220. .then( newEditor => newEditor.destroy() );
  221. } );
  222. } );
  223. describeMemoryUsage( () => {
  224. testMemoryUsage(
  225. 'should not grow on multiple create/destroy',
  226. () => InlineEditor
  227. .create( document.querySelector( '#mem-editor' ), {
  228. plugins: [ ArticlePluginSet ],
  229. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  230. image: {
  231. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  232. }
  233. } ) );
  234. } );
  235. } );