inlineeditor.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. } );
  72. describe( 'create - events', () => {
  73. afterEach( () => {
  74. return editor.destroy();
  75. } );
  76. it( 'fires all events in the right order', () => {
  77. const fired = [];
  78. function spy( evt ) {
  79. fired.push( evt.name );
  80. }
  81. class EventWatcher extends Plugin {
  82. init() {
  83. this.editor.on( 'pluginsReady', spy );
  84. this.editor.on( 'uiReady', spy );
  85. this.editor.on( 'dataReady', spy );
  86. this.editor.on( 'ready', spy );
  87. }
  88. }
  89. return InlineEditor.create( editorElement, {
  90. plugins: [ EventWatcher ]
  91. } )
  92. .then( newEditor => {
  93. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  94. editor = newEditor;
  95. } );
  96. } );
  97. it( 'fires dataReady once data is loaded', () => {
  98. let data;
  99. class EventWatcher extends Plugin {
  100. init() {
  101. this.editor.on( 'dataReady', () => {
  102. data = this.editor.getData();
  103. } );
  104. }
  105. }
  106. return InlineEditor.create( editorElement, {
  107. plugins: [ EventWatcher, Paragraph, Bold ]
  108. } )
  109. .then( newEditor => {
  110. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  111. editor = newEditor;
  112. } );
  113. } );
  114. it( 'fires uiReady once UI is ready', () => {
  115. let isReady;
  116. class EventWatcher extends Plugin {
  117. init() {
  118. this.editor.on( 'uiReady', () => {
  119. isReady = this.editor.ui.view.ready;
  120. } );
  121. }
  122. }
  123. return InlineEditor.create( editorElement, {
  124. plugins: [ EventWatcher ]
  125. } )
  126. .then( newEditor => {
  127. expect( isReady ).to.be.true;
  128. editor = newEditor;
  129. } );
  130. } );
  131. } );
  132. describe( 'destroy', () => {
  133. beforeEach( function() {
  134. return InlineEditor.create( editorElement, { plugins: [ Paragraph ] } )
  135. .then( newEditor => {
  136. editor = newEditor;
  137. const schema = editor.document.schema;
  138. schema.registerItem( 'heading' );
  139. schema.allow( { name: 'heading', inside: '$root' } );
  140. schema.allow( { name: '$text', inside: 'heading' } );
  141. buildModelConverter().for( editor.data.modelToView )
  142. .fromElement( 'heading' )
  143. .toElement( 'heading' );
  144. buildViewConverter().for( editor.data.viewToModel )
  145. .fromElement( 'heading' )
  146. .toElement( 'heading' );
  147. buildModelConverter().for( editor.editing.modelToView )
  148. .fromElement( 'heading' )
  149. .toElement( 'heading-editing-representation' );
  150. } );
  151. } );
  152. it( 'sets the data back to the editor element', () => {
  153. editor.setData( '<p>a</p><heading>b</heading>' );
  154. return editor.destroy()
  155. .then( () => {
  156. expect( editorElement.innerHTML )
  157. .to.equal( '<p>a</p><heading>b</heading>' );
  158. } );
  159. } );
  160. } );
  161. } );