inlineeditor.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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,
  78. /^editor-source-element-already-used/
  79. );
  80. }
  81. )
  82. .then( done )
  83. .catch( done );
  84. } );
  85. } );
  86. describe( 'create()', () => {
  87. beforeEach( function() {
  88. return InlineEditor
  89. .create( editorElement, {
  90. plugins: [ Paragraph, Bold ]
  91. } )
  92. .then( newEditor => {
  93. editor = newEditor;
  94. } );
  95. } );
  96. afterEach( () => {
  97. return editor.destroy();
  98. } );
  99. it( 'creates an instance which inherits from the InlineEditor', () => {
  100. expect( editor ).to.be.instanceof( InlineEditor );
  101. } );
  102. it( 'creates element–less UI view', () => {
  103. expect( editor.ui.view.element ).to.be.null;
  104. } );
  105. it( 'attaches editable UI as view\'s DOM root', () => {
  106. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  107. } );
  108. it( 'loads data from the editor element', () => {
  109. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  110. } );
  111. it( 'should not require config object', () => {
  112. const editorElement = document.createElement( 'div' );
  113. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  114. // Just being safe with `builtinPlugins` static property.
  115. class CustomInlineEditor extends InlineEditor {}
  116. CustomInlineEditor.builtinPlugins = [ Paragraph, Bold ];
  117. return CustomInlineEditor.create( editorElement )
  118. .then( newEditor => {
  119. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  120. return newEditor.destroy();
  121. } )
  122. .then( () => {
  123. editorElement.remove();
  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. const editorElement = document.createElement( 'div' );
  136. editorElement.innerHTML = '<p>Hello world!</p>';
  137. return InlineEditor.create( editorElement, {
  138. initialData: '<p>Hello world!</p>',
  139. plugins: [ Paragraph ]
  140. } ).then( editor => {
  141. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  142. return editor.destroy();
  143. } ).then( () => {
  144. editorElement.remove();
  145. } );
  146. } );
  147. it( 'should pass the config.toolbar.shouldNotGroupWhenFull configuration to the view', () => {
  148. const editorElement = document.createElement( 'div' );
  149. return InlineEditor.create( editorElement, {
  150. toolbar: {
  151. shouldNotGroupWhenFull: true
  152. }
  153. } ).then( editor => {
  154. expect( editor.ui.view.toolbar.options.shouldGroupWhenFull ).to.be.false;
  155. return editor.destroy();
  156. } ).then( () => {
  157. editorElement.remove();
  158. } );
  159. } );
  160. it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => {
  161. InlineEditor.create( '<p>Hello world!</p>', {
  162. initialData: '<p>I am evil!</p>',
  163. plugins: [ Paragraph ]
  164. } )
  165. .then(
  166. () => {
  167. expect.fail( 'Inline editor should throw an error when both initial data are passed' );
  168. },
  169. err => {
  170. assertCKEditorError( err,
  171. /* eslint-disable-next-line max-len */
  172. /^editor-create-initial-data: The config\.initialData option cannot be used together with initial data passed in Editor\.create\(\)\./,
  173. null
  174. );
  175. }
  176. )
  177. .then( () => {
  178. removeEditorBodyOrphans();
  179. } )
  180. .then( done )
  181. .catch( done );
  182. } );
  183. // #25
  184. it( 'creates an instance of a InlineEditor child class', () => {
  185. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  186. // editor being initialized on one element.
  187. const editorElement = document.createElement( 'div' );
  188. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  189. document.body.appendChild( editorElement );
  190. class CustomInlineEditor extends InlineEditor {}
  191. return CustomInlineEditor
  192. .create( editorElement, {
  193. plugins: [ Paragraph, Bold ]
  194. } )
  195. .then( newEditor => {
  196. expect( newEditor ).to.be.instanceof( CustomInlineEditor );
  197. expect( newEditor ).to.be.instanceof( InlineEditor );
  198. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  199. editorElement.remove();
  200. return newEditor.destroy();
  201. } );
  202. } );
  203. it( 'throws an error when is initialized in textarea', done => {
  204. InlineEditor.create( document.createElement( 'textarea' ) )
  205. .then(
  206. () => {
  207. expect.fail( 'Inline editor should throw an error when is initialized in textarea.' );
  208. },
  209. err => {
  210. assertCKEditorError( err,
  211. /^editor-wrong-element: This type of editor cannot be initialized inside <textarea> element\./,
  212. null
  213. );
  214. }
  215. )
  216. .then( done )
  217. .catch( done );
  218. } );
  219. } );
  220. describe( 'create - events', () => {
  221. afterEach( () => {
  222. return editor.destroy();
  223. } );
  224. it( 'fires all events in the right order', () => {
  225. const fired = [];
  226. function spy( evt ) {
  227. fired.push( `${ evt.name }-${ evt.source.constructor.name.toLowerCase() }` );
  228. }
  229. class EventWatcher extends Plugin {
  230. init() {
  231. this.editor.ui.on( 'ready', spy );
  232. this.editor.data.on( 'ready', spy );
  233. this.editor.on( 'ready', spy );
  234. }
  235. }
  236. return InlineEditor
  237. .create( editorElement, {
  238. plugins: [ EventWatcher ]
  239. } )
  240. .then( newEditor => {
  241. expect( fired ).to.deep.equal( [
  242. 'ready-inlineeditorui', 'ready-datacontroller', 'ready-inlineeditor' ] );
  243. editor = newEditor;
  244. } );
  245. } );
  246. it( 'fires ready once UI is ready', () => {
  247. let isReady;
  248. class EventWatcher extends Plugin {
  249. init() {
  250. this.editor.ui.on( 'ready', () => {
  251. isReady = this.editor.ui.view.isRendered;
  252. } );
  253. }
  254. }
  255. return InlineEditor
  256. .create( editorElement, {
  257. plugins: [ EventWatcher ]
  258. } )
  259. .then( newEditor => {
  260. expect( isReady ).to.be.true;
  261. editor = newEditor;
  262. } );
  263. } );
  264. } );
  265. describe( 'destroy', () => {
  266. beforeEach( function() {
  267. return InlineEditor
  268. .create( editorElement, { plugins: [ Paragraph ] } )
  269. .then( newEditor => {
  270. editor = newEditor;
  271. const schema = editor.model.schema;
  272. schema.register( 'heading' );
  273. schema.extend( 'heading', { allowIn: '$root' } );
  274. schema.extend( '$text', { allowIn: 'heading' } );
  275. editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading', view: 'heading' } );
  276. editor.conversion.for( 'dataDowncast' ).elementToElement( { model: 'heading', view: 'heading' } );
  277. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  278. model: 'heading',
  279. view: 'heading-editing-representation'
  280. } );
  281. } );
  282. } );
  283. it( 'sets the data back to the editor element', () => {
  284. editor.setData( '<p>a</p><heading>b</heading>' );
  285. return editor.destroy()
  286. .then( () => {
  287. expect( editorElement.innerHTML )
  288. .to.equal( '<p>a</p><heading>b</heading>' );
  289. } );
  290. } );
  291. it( 'should not throw an error if editor was initialized with the data', async () => {
  292. await editor.destroy();
  293. return InlineEditor
  294. .create( '<p>Foo.</p>', {
  295. plugins: [ Paragraph, Bold ]
  296. } )
  297. .then( newEditor => newEditor.destroy() );
  298. } );
  299. } );
  300. describeMemoryUsage( () => {
  301. testMemoryUsage(
  302. 'should not grow on multiple create/destroy',
  303. () => InlineEditor
  304. .create( document.querySelector( '#mem-editor' ), {
  305. plugins: [ ArticlePluginSet ],
  306. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  307. image: {
  308. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  309. }
  310. } ) );
  311. } );
  312. } );