8
0

inlineeditor.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => {
  148. InlineEditor.create( '<p>Hello world!</p>', {
  149. initialData: '<p>I am evil!</p>',
  150. plugins: [ Paragraph ]
  151. } )
  152. .then(
  153. () => {
  154. expect.fail( 'Inline editor should throw an error when both initial data are passed' );
  155. },
  156. err => {
  157. assertCKEditorError( err,
  158. /* eslint-disable-next-line max-len */
  159. /^editor-create-initial-data: The config\.initialData option cannot be used together with initial data passed in Editor\.create\(\)\./,
  160. null
  161. );
  162. }
  163. )
  164. .then( () => {
  165. removeEditorBodyOrphans();
  166. } )
  167. .then( done )
  168. .catch( done );
  169. } );
  170. // #25
  171. it( 'creates an instance of a InlineEditor child class', () => {
  172. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  173. // editor being initialized on one element.
  174. const editorElement = document.createElement( 'div' );
  175. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  176. document.body.appendChild( editorElement );
  177. class CustomInlineEditor extends InlineEditor {}
  178. return CustomInlineEditor
  179. .create( editorElement, {
  180. plugins: [ Paragraph, Bold ]
  181. } )
  182. .then( newEditor => {
  183. expect( newEditor ).to.be.instanceof( CustomInlineEditor );
  184. expect( newEditor ).to.be.instanceof( InlineEditor );
  185. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  186. editorElement.remove();
  187. return newEditor.destroy();
  188. } );
  189. } );
  190. it( 'throws an error when is initialized in textarea', done => {
  191. InlineEditor.create( document.createElement( 'textarea' ) )
  192. .then(
  193. () => {
  194. expect.fail( 'Inline editor should throw an error when is initialized in textarea.' );
  195. },
  196. err => {
  197. assertCKEditorError( err,
  198. /^editor-wrong-element: This type of editor cannot be initialized inside <textarea> element\./,
  199. null
  200. );
  201. }
  202. )
  203. .then( done )
  204. .catch( done );
  205. } );
  206. } );
  207. describe( 'create - events', () => {
  208. afterEach( () => {
  209. return editor.destroy();
  210. } );
  211. it( 'fires all events in the right order', () => {
  212. const fired = [];
  213. function spy( evt ) {
  214. fired.push( `${ evt.name }-${ evt.source.constructor.name.toLowerCase() }` );
  215. }
  216. class EventWatcher extends Plugin {
  217. init() {
  218. this.editor.ui.on( 'ready', spy );
  219. this.editor.data.on( 'ready', spy );
  220. this.editor.on( 'ready', spy );
  221. }
  222. }
  223. return InlineEditor
  224. .create( editorElement, {
  225. plugins: [ EventWatcher ]
  226. } )
  227. .then( newEditor => {
  228. expect( fired ).to.deep.equal( [
  229. 'ready-inlineeditorui', 'ready-datacontroller', 'ready-inlineeditor' ] );
  230. editor = newEditor;
  231. } );
  232. } );
  233. it( 'fires ready once UI is ready', () => {
  234. let isReady;
  235. class EventWatcher extends Plugin {
  236. init() {
  237. this.editor.ui.on( 'ready', () => {
  238. isReady = this.editor.ui.view.isRendered;
  239. } );
  240. }
  241. }
  242. return InlineEditor
  243. .create( editorElement, {
  244. plugins: [ EventWatcher ]
  245. } )
  246. .then( newEditor => {
  247. expect( isReady ).to.be.true;
  248. editor = newEditor;
  249. } );
  250. } );
  251. } );
  252. describe( 'destroy', () => {
  253. beforeEach( function() {
  254. return InlineEditor
  255. .create( editorElement, { plugins: [ Paragraph ] } )
  256. .then( newEditor => {
  257. editor = newEditor;
  258. const schema = editor.model.schema;
  259. schema.register( 'heading' );
  260. schema.extend( 'heading', { allowIn: '$root' } );
  261. schema.extend( '$text', { allowIn: 'heading' } );
  262. editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading', view: 'heading' } );
  263. editor.conversion.for( 'dataDowncast' ).elementToElement( { model: 'heading', view: 'heading' } );
  264. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  265. model: 'heading',
  266. view: 'heading-editing-representation'
  267. } );
  268. } );
  269. } );
  270. it( 'sets the data back to the editor element', () => {
  271. editor.setData( '<p>a</p><heading>b</heading>' );
  272. return editor.destroy()
  273. .then( () => {
  274. expect( editorElement.innerHTML )
  275. .to.equal( '<p>a</p><heading>b</heading>' );
  276. } );
  277. } );
  278. it( 'should not throw an error if editor was initialized with the data', async () => {
  279. await editor.destroy();
  280. return InlineEditor
  281. .create( '<p>Foo.</p>', {
  282. plugins: [ Paragraph, Bold ]
  283. } )
  284. .then( newEditor => newEditor.destroy() );
  285. } );
  286. } );
  287. describeMemoryUsage( () => {
  288. testMemoryUsage(
  289. 'should not grow on multiple create/destroy',
  290. () => InlineEditor
  291. .create( document.querySelector( '#mem-editor' ), {
  292. plugins: [ ArticlePluginSet ],
  293. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  294. image: {
  295. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  296. }
  297. } ) );
  298. } );
  299. } );