8
0

inlineeditor.js 9.5 KB

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