8
0

inlineeditor.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, Event, HTMLElement */
  6. import InlineEditorUI from '../src/inlineeditorui';
  7. import InlineEditorUIView from '../src/inlineeditoruiview';
  8. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  9. import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  10. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  11. import InlineEditor from '../src/inlineeditor';
  12. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  15. import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
  16. import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin';
  17. import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
  18. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  19. testUtils.createSinonSandbox();
  20. describe( 'InlineEditor', () => {
  21. let editor, editorElement;
  22. beforeEach( () => {
  23. editorElement = document.createElement( 'div' );
  24. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  25. document.body.appendChild( editorElement );
  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( 'editor.sourceElement should contain created div element', () => {
  82. return InlineEditor.create( '<p>Hello world!</p>', {
  83. plugins: [ Paragraph ]
  84. } ).then( editor => {
  85. expect( editor.sourceElement ).to.be.instanceOf( HTMLElement );
  86. expect( editor.sourceElement.tagName ).to.equal( 'DIV' );
  87. } );
  88. } );
  89. it( 'editor.element should contain created div element', () => {
  90. return InlineEditor.create( '<p>Hello world!</p>', {
  91. plugins: [ Paragraph ]
  92. } ).then( editor => {
  93. expect( editor.editing.view.getDomRoot() ).to.equal( editor.element );
  94. } );
  95. } );
  96. } );
  97. describe( 'create()', () => {
  98. beforeEach( function() {
  99. return InlineEditor
  100. .create( editorElement, {
  101. plugins: [ Paragraph, Bold ]
  102. } )
  103. .then( newEditor => {
  104. editor = newEditor;
  105. } );
  106. } );
  107. afterEach( () => {
  108. return editor.destroy();
  109. } );
  110. it( 'creates an instance which inherits from the InlineEditor', () => {
  111. expect( editor ).to.be.instanceof( InlineEditor );
  112. } );
  113. it( 'creates element–less UI view', () => {
  114. expect( editor.ui.view.element ).to.be.null;
  115. } );
  116. it( 'attaches editable UI as view\'s DOM root', () => {
  117. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  118. } );
  119. it( 'loads data from the editor element', () => {
  120. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  121. } );
  122. // #25
  123. it( 'creates an instance of a InlineEditor child class', () => {
  124. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  125. // editor being initialized on one element.
  126. const editorElement = document.createElement( 'div' );
  127. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  128. document.body.appendChild( editorElement );
  129. class CustomInlineEditor extends InlineEditor {}
  130. return CustomInlineEditor
  131. .create( editorElement, {
  132. plugins: [ Paragraph, Bold ]
  133. } )
  134. .then( newEditor => {
  135. expect( newEditor ).to.be.instanceof( CustomInlineEditor );
  136. expect( newEditor ).to.be.instanceof( InlineEditor );
  137. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  138. editorElement.remove();
  139. return newEditor.destroy();
  140. } );
  141. } );
  142. } );
  143. describe( 'create - events', () => {
  144. afterEach( () => {
  145. return editor.destroy();
  146. } );
  147. it( 'fires all events in the right order', () => {
  148. const fired = [];
  149. function spy( evt ) {
  150. fired.push( evt.name );
  151. }
  152. class EventWatcher extends Plugin {
  153. init() {
  154. this.editor.on( 'pluginsReady', spy );
  155. this.editor.on( 'uiReady', spy );
  156. this.editor.on( 'dataReady', spy );
  157. this.editor.on( 'ready', spy );
  158. }
  159. }
  160. return InlineEditor
  161. .create( editorElement, {
  162. plugins: [ EventWatcher ]
  163. } )
  164. .then( newEditor => {
  165. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  166. editor = newEditor;
  167. } );
  168. } );
  169. it( 'fires dataReady once data is loaded', () => {
  170. let data;
  171. class EventWatcher extends Plugin {
  172. init() {
  173. this.editor.on( 'dataReady', () => {
  174. data = this.editor.getData();
  175. } );
  176. }
  177. }
  178. return InlineEditor
  179. .create( editorElement, {
  180. plugins: [ EventWatcher, Paragraph, Bold ]
  181. } )
  182. .then( newEditor => {
  183. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  184. editor = newEditor;
  185. } );
  186. } );
  187. it( 'fires uiReady once UI is ready', () => {
  188. let isReady;
  189. class EventWatcher extends Plugin {
  190. init() {
  191. this.editor.on( 'uiReady', () => {
  192. isReady = this.editor.ui.view.isRendered;
  193. } );
  194. }
  195. }
  196. return InlineEditor
  197. .create( editorElement, {
  198. plugins: [ EventWatcher ]
  199. } )
  200. .then( newEditor => {
  201. expect( isReady ).to.be.true;
  202. editor = newEditor;
  203. } );
  204. } );
  205. } );
  206. describe( 'destroy', () => {
  207. beforeEach( function() {
  208. return InlineEditor
  209. .create( editorElement, { plugins: [ Paragraph ] } )
  210. .then( newEditor => {
  211. editor = newEditor;
  212. const schema = editor.model.schema;
  213. schema.register( 'heading' );
  214. schema.extend( 'heading', { allowIn: '$root' } );
  215. schema.extend( '$text', { allowIn: 'heading' } );
  216. editor.conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'heading', view: 'heading' } ) );
  217. editor.conversion.for( 'dataDowncast' ).add( downcastElementToElement( { model: 'heading', view: 'heading' } ) );
  218. editor.conversion.for( 'editingDowncast' ).add( downcastElementToElement( {
  219. model: 'heading',
  220. view: 'heading-editing-representation'
  221. } ) );
  222. } );
  223. } );
  224. it( 'sets the data back to the editor element', () => {
  225. editor.setData( '<p>a</p><heading>b</heading>' );
  226. return editor.destroy()
  227. .then( () => {
  228. expect( editorElement.innerHTML )
  229. .to.equal( '<p>a</p><heading>b</heading>' );
  230. } );
  231. } );
  232. } );
  233. } );