inlineeditor.js 9.5 KB

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