8
0

inlineeditor.js 6.2 KB

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