classic.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-tags: editor, browser-only */
  7. import ClassicEditorUI from 'ckeditor5-editor-classic/src/classiceditorui';
  8. import ClassicEditorUIView from 'ckeditor5-editor-classic/src/classiceditoruiview';
  9. import HtmlDataProcessor from 'ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  10. import ClassicEditor from 'ckeditor5-editor-classic/src/classic';
  11. import Plugin from 'ckeditor5-core/src/plugin';
  12. import Paragraph from 'ckeditor5-paragraph/src/paragraph';
  13. import Bold from 'ckeditor5-basic-styles/src/bold';
  14. import testUtils from 'ckeditor5-core/tests/_utils/utils';
  15. import count from 'ckeditor5-utils/src/count';
  16. testUtils.createSinonSandbox();
  17. describe( 'ClassicEditor', () => {
  18. let editor, editorElement;
  19. beforeEach( () => {
  20. editorElement = document.createElement( 'div' );
  21. editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
  22. document.body.appendChild( editorElement );
  23. } );
  24. afterEach( () => {
  25. editorElement.remove();
  26. } );
  27. describe( 'constructor()', () => {
  28. beforeEach( () => {
  29. editor = new ClassicEditor( editorElement );
  30. } );
  31. it( 'creates a single div editable root in the view', () => {
  32. expect( editor.editing.view.getRoot() ).to.have.property( 'name', 'div' );
  33. } );
  34. it( 'creates a single document root', () => {
  35. expect( count( editor.document.getRootNames() ) ).to.equal( 1 );
  36. expect( editor.document.getRoot() ).to.have.property( 'name', '$root' );
  37. } );
  38. it( 'creates the UI using BoxedEditorUI classes', () => {
  39. expect( editor.ui ).to.be.instanceof( ClassicEditorUI );
  40. expect( editor.ui.view ).to.be.instanceof( ClassicEditorUIView );
  41. } );
  42. it( 'uses HTMLDataProcessor', () => {
  43. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  44. } );
  45. } );
  46. describe( 'create()', () => {
  47. beforeEach( function() {
  48. return ClassicEditor.create( editorElement, {
  49. plugins: [ Paragraph, Bold ]
  50. } )
  51. .then( newEditor => {
  52. editor = newEditor;
  53. } );
  54. } );
  55. afterEach( () => {
  56. return editor.destroy();
  57. } );
  58. it( 'creates an instance which inherits from the ClassicEditor', () => {
  59. expect( editor ).to.be.instanceof( ClassicEditor );
  60. } );
  61. it( 'inserts editor UI next to editor element', () => {
  62. expect( editor.ui.view.element.previousSibling ).to.equal( editorElement );
  63. } );
  64. it( 'attaches editable UI as view\'s DOM root', () => {
  65. expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
  66. } );
  67. it( 'loads data from the editor element', () => {
  68. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  69. } );
  70. } );
  71. describe( 'create - events', () => {
  72. afterEach( () => {
  73. return editor.destroy();
  74. } );
  75. it( 'fires all events in the right order', () => {
  76. const fired = [];
  77. function spy( evt ) {
  78. fired.push( evt.name );
  79. }
  80. class EventWatcher extends Plugin {
  81. init() {
  82. this.editor.on( 'pluginsReady', spy );
  83. this.editor.on( 'uiReady', spy );
  84. this.editor.on( 'dataReady', spy );
  85. this.editor.on( 'ready', spy );
  86. }
  87. }
  88. return ClassicEditor.create( editorElement, {
  89. plugins: [ EventWatcher ]
  90. } )
  91. .then( ( newEditor ) => {
  92. expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
  93. editor = newEditor;
  94. } );
  95. } );
  96. it( 'fires dataReady once data is loaded', () => {
  97. let data;
  98. class EventWatcher extends Plugin {
  99. init() {
  100. this.editor.on( 'dataReady', () => {
  101. data = this.editor.getData();
  102. } );
  103. }
  104. }
  105. return ClassicEditor.create( editorElement, {
  106. plugins: [ EventWatcher, Paragraph, Bold ]
  107. } )
  108. .then( ( newEditor ) => {
  109. expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
  110. editor = newEditor;
  111. } );
  112. } );
  113. it( 'fires uiReady once UI is ready', () => {
  114. let isReady;
  115. class EventWatcher extends Plugin {
  116. init() {
  117. this.editor.on( 'uiReady', () => {
  118. isReady = this.editor.ui.view.ready;
  119. } );
  120. }
  121. }
  122. return ClassicEditor.create( editorElement, {
  123. plugins: [ EventWatcher ]
  124. } )
  125. .then( ( newEditor ) => {
  126. expect( isReady ).to.be.true;
  127. editor = newEditor;
  128. } );
  129. } );
  130. } );
  131. describe( 'destroy', () => {
  132. beforeEach( function() {
  133. return ClassicEditor.create( editorElement, { plugins: [ Paragraph ] } )
  134. .then( newEditor => {
  135. editor = newEditor;
  136. } );
  137. } );
  138. it( 'sets the data back to the editor element', () => {
  139. editor.setData( '<p>foo</p>' );
  140. return editor.destroy()
  141. .then( () => {
  142. expect( editorElement.innerHTML ).to.equal( '<p>foo</p>' );
  143. } );
  144. } );
  145. it( 'restores the editor element', () => {
  146. expect( editor.element.style.display ).to.equal( 'none' );
  147. return editor.destroy()
  148. .then( () => {
  149. expect( editor.element.style.display ).to.equal( '' );
  150. } );
  151. } );
  152. } );
  153. } );