inlineeditor.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /**
  2. * @license Copyright (c) 2003-2020, 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. import { removeEditorBodyOrphans } from '@ckeditor/ckeditor5-core/tests/_utils/cleanup';
  21. describe( 'InlineEditor', () => {
  22. let editor, editorElement;
  23. testUtils.createSinonSandbox();
  24. beforeEach( () => {
  25. editorElement = document.createElement( 'div' );
  26. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  27. document.body.appendChild( editorElement );
  28. testUtils.sinon.stub( console, 'warn' ).callsFake( () => {} );
  29. } );
  30. afterEach( () => {
  31. editorElement.remove();
  32. } );
  33. describe( 'constructor()', () => {
  34. beforeEach( () => {
  35. editor = new InlineEditor( editorElement );
  36. } );
  37. it( 'creates the UI using BoxedEditorUI classes', () => {
  38. expect( editor.ui ).to.be.instanceof( InlineEditorUI );
  39. expect( editor.ui.view ).to.be.instanceof( InlineEditorUIView );
  40. } );
  41. it( 'uses HTMLDataProcessor', () => {
  42. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  43. } );
  44. it( 'has a Data Interface', () => {
  45. expect( testUtils.isMixed( InlineEditor, DataApiMixin ) ).to.be.true;
  46. } );
  47. it( 'has a Element Interface', () => {
  48. expect( testUtils.isMixed( InlineEditor, ElementApiMixin ) ).to.be.true;
  49. } );
  50. it( 'creates main root element', () => {
  51. expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
  52. } );
  53. it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
  54. return InlineEditor.create( '<p>Hello world!</p>', {
  55. plugins: [ Paragraph ]
  56. } ).then( editor => {
  57. expect( editor.sourceElement ).to.be.undefined;
  58. return editor.destroy();
  59. } );
  60. } );
  61. it( 'editor.ui.element should contain the whole editor (with UI) element', () => {
  62. return InlineEditor.create( '<p>Hello world!</p>', {
  63. plugins: [ Paragraph ]
  64. } ).then( editor => {
  65. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.element );
  66. return editor.destroy();
  67. } );
  68. } );
  69. // See: https://github.com/ckeditor/ckeditor5/issues/746
  70. it( 'should throw when trying to create the editor using the same source element more than once', done => {
  71. InlineEditor.create( editorElement )
  72. .then(
  73. () => {
  74. expect.fail( 'Inline editor should not initialize on an element already used by other instance.' );
  75. },
  76. err => {
  77. assertCKEditorError( err, 'editor-source-element-already-used' );
  78. }
  79. )
  80. .then( done )
  81. .catch( done );
  82. } );
  83. } );
  84. describe( 'create()', () => {
  85. beforeEach( function() {
  86. return InlineEditor
  87. .create( editorElement, {
  88. plugins: [ Paragraph, Bold ]
  89. } )
  90. .then( newEditor => {
  91. editor = newEditor;
  92. } );
  93. } );
  94. afterEach( () => {
  95. return editor.destroy();
  96. } );
  97. it( 'creates an instance which inherits from the InlineEditor', () => {
  98. expect( editor ).to.be.instanceof( InlineEditor );
  99. } );
  100. it( 'creates element–less UI view', () => {
  101. expect( editor.ui.view.element ).to.be.null;
  102. } );
  103. it( 'attaches editable UI as view\'s DOM root', () => {
  104. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  105. } );
  106. it( 'loads data from the editor element', () => {
  107. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  108. } );
  109. it( 'should not require config object', () => {
  110. const editorElement = document.createElement( 'div' );
  111. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  112. // Just being safe with `builtinPlugins` static property.
  113. class CustomInlineEditor extends InlineEditor {}
  114. CustomInlineEditor.builtinPlugins = [ Paragraph, Bold ];
  115. return CustomInlineEditor.create( editorElement )
  116. .then( newEditor => {
  117. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  118. return newEditor.destroy();
  119. } )
  120. .then( () => {
  121. editorElement.remove();
  122. } );
  123. } );
  124. it( 'allows to pass data to the constructor', () => {
  125. return InlineEditor.create( '<p>Hello world!</p>', {
  126. plugins: [ Paragraph ]
  127. } ).then( editor => {
  128. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  129. editor.destroy();
  130. } );
  131. } );
  132. it( 'initializes with config.initialData', () => {
  133. const editorElement = document.createElement( 'div' );
  134. editorElement.innerHTML = '<p>Hello world!</p>';
  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. return editor.destroy();
  141. } ).then( () => {
  142. editorElement.remove();
  143. } );
  144. } );
  145. it( 'should pass the config.toolbar.shouldNotGroupWhenFull configuration to the view', () => {
  146. const editorElement = document.createElement( 'div' );
  147. return InlineEditor.create( editorElement, {
  148. toolbar: {
  149. shouldNotGroupWhenFull: true
  150. }
  151. } ).then( editor => {
  152. expect( editor.ui.view.toolbar.options.shouldGroupWhenFull ).to.be.false;
  153. return editor.destroy();
  154. } ).then( () => {
  155. editorElement.remove();
  156. } );
  157. } );
  158. it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => {
  159. InlineEditor.create( '<p>Hello world!</p>', {
  160. initialData: '<p>I am evil!</p>',
  161. plugins: [ Paragraph ]
  162. } )
  163. .then(
  164. () => {
  165. expect.fail( 'Inline editor should throw an error when both initial data are passed' );
  166. },
  167. err => {
  168. assertCKEditorError( err, 'editor-create-initial-data', null );
  169. }
  170. )
  171. .then( () => {
  172. removeEditorBodyOrphans();
  173. } )
  174. .then( done )
  175. .catch( done );
  176. } );
  177. // #25
  178. it( 'creates an instance of a InlineEditor child class', () => {
  179. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  180. // editor being initialized on one element.
  181. const editorElement = document.createElement( 'div' );
  182. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  183. document.body.appendChild( editorElement );
  184. class CustomInlineEditor extends InlineEditor {}
  185. return CustomInlineEditor
  186. .create( editorElement, {
  187. plugins: [ Paragraph, Bold ]
  188. } )
  189. .then( newEditor => {
  190. expect( newEditor ).to.be.instanceof( CustomInlineEditor );
  191. expect( newEditor ).to.be.instanceof( InlineEditor );
  192. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  193. editorElement.remove();
  194. return newEditor.destroy();
  195. } );
  196. } );
  197. it( 'throws an error when is initialized in textarea', done => {
  198. InlineEditor.create( document.createElement( 'textarea' ) )
  199. .then(
  200. () => {
  201. expect.fail( 'Inline editor should throw an error when is initialized in textarea.' );
  202. },
  203. err => {
  204. assertCKEditorError( err, 'editor-wrong-element', null );
  205. }
  206. )
  207. .then( done )
  208. .catch( done );
  209. } );
  210. } );
  211. describe( 'create - events', () => {
  212. afterEach( () => {
  213. return editor.destroy();
  214. } );
  215. it( 'fires all events in the right order', () => {
  216. const fired = [];
  217. function spy( evt ) {
  218. fired.push( `${ evt.name }-${ evt.source.constructor.name.toLowerCase() }` );
  219. }
  220. class EventWatcher extends Plugin {
  221. init() {
  222. this.editor.ui.on( 'ready', spy );
  223. this.editor.data.on( 'ready', spy );
  224. this.editor.on( 'ready', spy );
  225. }
  226. }
  227. return InlineEditor
  228. .create( editorElement, {
  229. plugins: [ EventWatcher ]
  230. } )
  231. .then( newEditor => {
  232. expect( fired ).to.deep.equal( [
  233. 'ready-inlineeditorui', 'ready-datacontroller', 'ready-inlineeditor' ] );
  234. editor = newEditor;
  235. } );
  236. } );
  237. it( 'fires ready once UI is ready', () => {
  238. let isReady;
  239. class EventWatcher extends Plugin {
  240. init() {
  241. this.editor.ui.on( 'ready', () => {
  242. isReady = this.editor.ui.view.isRendered;
  243. } );
  244. }
  245. }
  246. return InlineEditor
  247. .create( editorElement, {
  248. plugins: [ EventWatcher ]
  249. } )
  250. .then( newEditor => {
  251. expect( isReady ).to.be.true;
  252. editor = newEditor;
  253. } );
  254. } );
  255. } );
  256. describe( 'destroy', () => {
  257. beforeEach( function() {
  258. return InlineEditor
  259. .create( editorElement, { plugins: [ Paragraph ] } )
  260. .then( newEditor => {
  261. editor = newEditor;
  262. const schema = editor.model.schema;
  263. schema.register( 'heading' );
  264. schema.extend( 'heading', { allowIn: '$root' } );
  265. schema.extend( '$text', { allowIn: 'heading' } );
  266. editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading', view: 'heading' } );
  267. editor.conversion.for( 'dataDowncast' ).elementToElement( { model: 'heading', view: 'heading' } );
  268. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  269. model: 'heading',
  270. view: 'heading-editing-representation'
  271. } );
  272. } );
  273. } );
  274. it( 'sets the data back to the editor element', () => {
  275. editor.setData( '<p>a</p><heading>b</heading>' );
  276. return editor.destroy()
  277. .then( () => {
  278. expect( editorElement.innerHTML )
  279. .to.equal( '<p>a</p><heading>b</heading>' );
  280. } );
  281. } );
  282. it( 'should not throw an error if editor was initialized with the data', async () => {
  283. await editor.destroy();
  284. return InlineEditor
  285. .create( '<p>Foo.</p>', {
  286. plugins: [ Paragraph, Bold ]
  287. } )
  288. .then( newEditor => newEditor.destroy() );
  289. } );
  290. } );
  291. describeMemoryUsage( () => {
  292. testMemoryUsage(
  293. 'should not grow on multiple create/destroy',
  294. () => InlineEditor
  295. .create( document.querySelector( '#mem-editor' ), {
  296. plugins: [ ArticlePluginSet ],
  297. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  298. image: {
  299. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  300. }
  301. } ) );
  302. } );
  303. } );