8
0

inlineeditor.js 10 KB

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