inlineeditor.js 10 KB

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