inlineeditor.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  10. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  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 testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. import count from '@ckeditor/ckeditor5-utils/src/count';
  17. testUtils.createSinonSandbox();
  18. describe( 'InlineEditor', () => {
  19. let editor, editorElement;
  20. beforeEach( () => {
  21. editorElement = document.createElement( 'div' );
  22. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  23. document.body.appendChild( editorElement );
  24. } );
  25. afterEach( () => {
  26. editorElement.remove();
  27. } );
  28. describe( 'constructor()', () => {
  29. beforeEach( () => {
  30. editor = new InlineEditor( editorElement );
  31. } );
  32. it( 'creates a single div editable root in the view', () => {
  33. editor.ui.init();
  34. expect( editor.editing.view.getRoot() ).to.have.property( 'name', 'div' );
  35. editor.ui.destroy();
  36. } );
  37. it( 'creates a single document root', () => {
  38. expect( count( editor.model.document.getRootNames() ) ).to.equal( 1 );
  39. expect( editor.model.document.getRoot() ).to.have.property( 'name', '$root' );
  40. } );
  41. it( 'creates the UI using BoxedEditorUI classes', () => {
  42. expect( editor.ui ).to.be.instanceof( InlineEditorUI );
  43. expect( editor.ui.view ).to.be.instanceof( InlineEditorUIView );
  44. } );
  45. it( 'uses HTMLDataProcessor', () => {
  46. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  47. } );
  48. } );
  49. describe( 'create()', () => {
  50. beforeEach( function() {
  51. return InlineEditor
  52. .create( editorElement, {
  53. plugins: [ Paragraph, Bold ]
  54. } )
  55. .then( newEditor => {
  56. editor = newEditor;
  57. } );
  58. } );
  59. afterEach( () => {
  60. return editor.destroy();
  61. } );
  62. it( 'creates an instance which inherits from the InlineEditor', () => {
  63. expect( editor ).to.be.instanceof( InlineEditor );
  64. } );
  65. it( 'creates element–less UI view', () => {
  66. expect( editor.ui.view.element ).to.be.null;
  67. } );
  68. it( 'attaches editable UI as view\'s DOM root', () => {
  69. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  70. } );
  71. it( 'loads data from the editor element', () => {
  72. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  73. } );
  74. // #25
  75. it( 'creates an instance of a InlineEditor child class', () => {
  76. // Fun fact: Remove the next 3 lines and you'll get a lovely inf loop due to two
  77. // editor being initialized on one element.
  78. const editorElement = document.createElement( 'div' );
  79. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  80. document.body.appendChild( editorElement );
  81. class CustomInlineEditor extends InlineEditor {}
  82. return CustomInlineEditor
  83. .create( editorElement, {
  84. plugins: [ Paragraph, Bold ]
  85. } )
  86. .then( newEditor => {
  87. expect( newEditor ).to.be.instanceof( CustomInlineEditor );
  88. expect( newEditor ).to.be.instanceof( InlineEditor );
  89. expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  90. editorElement.remove();
  91. return newEditor.destroy();
  92. } );
  93. } );
  94. } );
  95. describe( 'create - events', () => {
  96. afterEach( () => {
  97. return editor.destroy();
  98. } );
  99. it( 'fires all events in the right order', () => {
  100. const fired = [];
  101. function spy( evt ) {
  102. fired.push( evt.name );
  103. }
  104. class EventWatcher extends Plugin {
  105. init() {
  106. this.editor.on( 'pluginsReady', spy );
  107. this.editor.on( 'uiReady', spy );
  108. this.editor.on( 'dataReady', spy );
  109. this.editor.on( 'ready', spy );
  110. }
  111. }
  112. return InlineEditor
  113. .create( editorElement, {
  114. plugins: [ EventWatcher ]
  115. } )
  116. .then( newEditor => {
  117. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  118. editor = newEditor;
  119. } );
  120. } );
  121. it( 'fires dataReady once data is loaded', () => {
  122. let data;
  123. class EventWatcher extends Plugin {
  124. init() {
  125. this.editor.on( 'dataReady', () => {
  126. data = this.editor.getData();
  127. } );
  128. }
  129. }
  130. return InlineEditor
  131. .create( editorElement, {
  132. plugins: [ EventWatcher, Paragraph, Bold ]
  133. } )
  134. .then( newEditor => {
  135. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  136. editor = newEditor;
  137. } );
  138. } );
  139. it( 'fires uiReady once UI is ready', () => {
  140. let isReady;
  141. class EventWatcher extends Plugin {
  142. init() {
  143. this.editor.on( 'uiReady', () => {
  144. isReady = this.editor.ui.view.isRendered;
  145. } );
  146. }
  147. }
  148. return InlineEditor
  149. .create( editorElement, {
  150. plugins: [ EventWatcher ]
  151. } )
  152. .then( newEditor => {
  153. expect( isReady ).to.be.true;
  154. editor = newEditor;
  155. } );
  156. } );
  157. } );
  158. describe( 'destroy', () => {
  159. beforeEach( function() {
  160. return InlineEditor
  161. .create( editorElement, { plugins: [ Paragraph ] } )
  162. .then( newEditor => {
  163. editor = newEditor;
  164. const schema = editor.model.schema;
  165. schema.registerItem( 'heading' );
  166. schema.allow( { name: 'heading', inside: '$root' } );
  167. schema.allow( { name: '$text', inside: 'heading' } );
  168. buildModelConverter().for( editor.data.modelToView )
  169. .fromElement( 'heading' )
  170. .toElement( 'heading' );
  171. buildViewConverter().for( editor.data.viewToModel )
  172. .fromElement( 'heading' )
  173. .toElement( 'heading' );
  174. buildModelConverter().for( editor.editing.modelToView )
  175. .fromElement( 'heading' )
  176. .toElement( 'heading-editing-representation' );
  177. } );
  178. } );
  179. it( 'sets the data back to the editor element', () => {
  180. editor.setData( '<p>a</p><heading>b</heading>' );
  181. return editor.destroy()
  182. .then( () => {
  183. expect( editorElement.innerHTML )
  184. .to.equal( '<p>a</p><heading>b</heading>' );
  185. } );
  186. } );
  187. } );
  188. } );