inlineeditor.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, Event */
  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. describe( 'InlineEditor', () => {
  19. let editor, editorElement;
  20. testUtils.createSinonSandbox();
  21. beforeEach( () => {
  22. editorElement = document.createElement( 'div' );
  23. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  24. document.body.appendChild( editorElement );
  25. testUtils.sinon.stub( log, 'warn' ).callsFake( () => {} );
  26. } );
  27. afterEach( () => {
  28. editorElement.remove();
  29. } );
  30. describe( 'constructor()', () => {
  31. beforeEach( () => {
  32. editor = new InlineEditor( editorElement );
  33. } );
  34. it( 'creates the UI using BoxedEditorUI classes', () => {
  35. expect( editor.ui ).to.be.instanceof( InlineEditorUI );
  36. expect( editor.ui.view ).to.be.instanceof( InlineEditorUIView );
  37. } );
  38. it( 'uses HTMLDataProcessor', () => {
  39. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  40. } );
  41. it( 'has a Data Interface', () => {
  42. expect( testUtils.isMixed( InlineEditor, DataApiMixin ) ).to.be.true;
  43. } );
  44. it( 'has a Element Interface', () => {
  45. expect( testUtils.isMixed( InlineEditor, ElementApiMixin ) ).to.be.true;
  46. } );
  47. it( 'creates main root element', () => {
  48. expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
  49. } );
  50. it( 'handles form element', () => {
  51. const form = document.createElement( 'form' );
  52. const textarea = document.createElement( 'textarea' );
  53. form.appendChild( textarea );
  54. document.body.appendChild( form );
  55. // Prevents page realods in Firefox ;|
  56. form.addEventListener( 'submit', evt => {
  57. evt.preventDefault();
  58. } );
  59. return InlineEditor.create( textarea, {
  60. plugins: [ Paragraph ]
  61. } ).then( editor => {
  62. expect( textarea.value ).to.equal( '' );
  63. editor.setData( '<p>Foo</p>' );
  64. form.dispatchEvent( new Event( 'submit', {
  65. // We need to be able to do preventDefault() to prevent page reloads in Firefox.
  66. cancelable: true
  67. } ) );
  68. expect( textarea.value ).to.equal( '<p>Foo</p>' );
  69. return editor.destroy().then( () => {
  70. form.remove();
  71. } );
  72. } );
  73. } );
  74. it( 'allows to pass data to the constructor', () => {
  75. return InlineEditor.create( '<p>Hello world!</p>', {
  76. plugins: [ Paragraph ]
  77. } ).then( editor => {
  78. expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
  79. } );
  80. } );
  81. it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
  82. return InlineEditor.create( '<p>Hello world!</p>', {
  83. plugins: [ Paragraph ]
  84. } ).then( editor => {
  85. expect( editor.sourceElement ).to.be.undefined;
  86. } );
  87. } );
  88. it( 'editor.element should contain the whole editor (with UI) element', () => {
  89. return InlineEditor.create( '<p>Hello world!</p>', {
  90. plugins: [ Paragraph ]
  91. } ).then( editor => {
  92. expect( editor.editing.view.getDomRoot() ).to.equal( editor.element );
  93. } );
  94. } );
  95. it( 'editor.ui.element should contain the whole editor (with UI) element', () => {
  96. return InlineEditor.create( '<p>Hello world!</p>', {
  97. plugins: [ Paragraph ]
  98. } ).then( editor => {
  99. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.element );
  100. } );
  101. } );
  102. } );
  103. describe( 'create()', () => {
  104. beforeEach( function() {
  105. return InlineEditor
  106. .create( editorElement, {
  107. plugins: [ Paragraph, Bold ]
  108. } )
  109. .then( newEditor => {
  110. editor = newEditor;
  111. } );
  112. } );
  113. afterEach( () => {
  114. return editor.destroy();
  115. } );
  116. it( 'creates an instance which inherits from the InlineEditor', () => {
  117. expect( editor ).to.be.instanceof( InlineEditor );
  118. } );
  119. it( 'creates element–less UI view', () => {
  120. expect( editor.ui.view.element ).to.be.null;
  121. } );
  122. it( 'attaches editable UI as view\'s DOM root', () => {
  123. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  124. } );
  125. it( 'loads data from the editor element', () => {
  126. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  127. } );
  128. // #25
  129. it( 'creates an instance of a InlineEditor child class', () => {
  130. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  131. // editor being initialized on one element.
  132. const editorElement = document.createElement( 'div' );
  133. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  134. document.body.appendChild( editorElement );
  135. class CustomInlineEditor extends InlineEditor {}
  136. return CustomInlineEditor
  137. .create( editorElement, {
  138. plugins: [ Paragraph, Bold ]
  139. } )
  140. .then( newEditor => {
  141. expect( newEditor ).to.be.instanceof( CustomInlineEditor );
  142. expect( newEditor ).to.be.instanceof( InlineEditor );
  143. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  144. editorElement.remove();
  145. return newEditor.destroy();
  146. } );
  147. } );
  148. } );
  149. describe( 'create - events', () => {
  150. afterEach( () => {
  151. return editor.destroy();
  152. } );
  153. it( 'fires all events in the right order', () => {
  154. const fired = [];
  155. function spy( evt ) {
  156. fired.push( evt.name );
  157. }
  158. class EventWatcher extends Plugin {
  159. init() {
  160. this.editor.on( 'pluginsReady', spy );
  161. this.editor.ui.on( 'ready', spy );
  162. this.editor.on( 'uiReady', spy );
  163. this.editor.on( 'dataReady', spy );
  164. this.editor.on( 'ready', spy );
  165. }
  166. }
  167. return InlineEditor
  168. .create( editorElement, {
  169. plugins: [ EventWatcher ]
  170. } )
  171. .then( newEditor => {
  172. expect( fired ).to.deep.equal( [ 'pluginsReady', 'ready', 'uiReady', 'dataReady', 'ready' ] );
  173. editor = newEditor;
  174. } );
  175. } );
  176. it( 'fires dataReady once data is loaded', () => {
  177. let data;
  178. class EventWatcher extends Plugin {
  179. init() {
  180. this.editor.on( 'dataReady', () => {
  181. data = this.editor.getData();
  182. } );
  183. }
  184. }
  185. return InlineEditor
  186. .create( editorElement, {
  187. plugins: [ EventWatcher, Paragraph, Bold ]
  188. } )
  189. .then( newEditor => {
  190. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  191. editor = newEditor;
  192. } );
  193. } );
  194. it( 'fires uiReady once UI is ready', () => {
  195. let isReady;
  196. class EventWatcher extends Plugin {
  197. init() {
  198. this.editor.on( 'uiReady', () => {
  199. isReady = this.editor.ui.view.isRendered;
  200. } );
  201. }
  202. }
  203. return InlineEditor
  204. .create( editorElement, {
  205. plugins: [ EventWatcher ]
  206. } )
  207. .then( newEditor => {
  208. expect( isReady ).to.be.true;
  209. editor = newEditor;
  210. } );
  211. } );
  212. it( 'fires ready once UI is ready', () => {
  213. let isReady;
  214. class EventWatcher extends Plugin {
  215. init() {
  216. this.editor.ui.on( 'ready', () => {
  217. isReady = this.editor.ui.view.isRendered;
  218. } );
  219. }
  220. }
  221. return InlineEditor
  222. .create( editorElement, {
  223. plugins: [ EventWatcher ]
  224. } )
  225. .then( newEditor => {
  226. expect( isReady ).to.be.true;
  227. editor = newEditor;
  228. } );
  229. } );
  230. } );
  231. describe( 'destroy', () => {
  232. beforeEach( function() {
  233. return InlineEditor
  234. .create( editorElement, { plugins: [ Paragraph ] } )
  235. .then( newEditor => {
  236. editor = newEditor;
  237. const schema = editor.model.schema;
  238. schema.register( 'heading' );
  239. schema.extend( 'heading', { allowIn: '$root' } );
  240. schema.extend( '$text', { allowIn: 'heading' } );
  241. editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading', view: 'heading' } );
  242. editor.conversion.for( 'dataDowncast' ).elementToElement( { model: 'heading', view: 'heading' } );
  243. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  244. model: 'heading',
  245. view: 'heading-editing-representation'
  246. } );
  247. } );
  248. } );
  249. it( 'sets the data back to the editor element', () => {
  250. editor.setData( '<p>a</p><heading>b</heading>' );
  251. return editor.destroy()
  252. .then( () => {
  253. expect( editorElement.innerHTML )
  254. .to.equal( '<p>a</p><heading>b</heading>' );
  255. } );
  256. } );
  257. it( 'should not throw an error if editor was initialized with the data', () => {
  258. return InlineEditor
  259. .create( '<p>Foo.</p>', {
  260. plugins: [ Paragraph, Bold ]
  261. } )
  262. .then( newEditor => newEditor.destroy() );
  263. } );
  264. } );
  265. } );