inlineeditor.js 6.2 KB

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