8
0

standardcreator.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: creator, browser-only */
  6. 'use strict';
  7. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  8. import Creator from '/ckeditor5/creator/creator.js';
  9. import StandardCreator from '/ckeditor5/creator/standardcreator.js';
  10. import Editor from '/ckeditor5/editor.js';
  11. import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
  12. import Document from '/ckeditor5/engine/model/document.js';
  13. import EditingController from '/ckeditor5/engine/editingcontroller.js';
  14. import DataController from '/ckeditor5/engine/datacontroller.js';
  15. import KeystrokeHandler from '/ckeditor5/keystrokehandler.js';
  16. testUtils.createSinonSandbox();
  17. describe( 'Creator', () => {
  18. let creator, editor;
  19. beforeEach( () => {
  20. const firstElement = document.createElement( 'div' );
  21. document.body.appendChild( firstElement );
  22. const secondElement = document.createElement( 'div' );
  23. document.body.appendChild( secondElement );
  24. editor = new Editor( { first: firstElement, second: secondElement } );
  25. creator = new StandardCreator( editor, new HtmlDataProcessor() );
  26. } );
  27. describe( 'constructor', () => {
  28. it( 'inherits from the Creator', () => {
  29. expect( creator ).to.be.instanceof( Creator );
  30. } );
  31. it( 'creates the engine', () => {
  32. expect( editor.document ).to.be.instanceof( Document );
  33. expect( editor.editing ).to.be.instanceof( EditingController );
  34. expect( editor.data ).to.be.instanceof( DataController );
  35. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  36. } );
  37. it( 'creates the keystroke handler', () => {
  38. expect( editor.keystrokes ).to.be.instanceof( KeystrokeHandler );
  39. } );
  40. it( 'uses HtmlDataProcessor if no processor is provided in constructor', () => {
  41. creator = new StandardCreator( editor );
  42. expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
  43. } );
  44. } );
  45. describe( 'create', () => {
  46. it( 'returns a promise', () => {
  47. expect( creator.create() ).to.be.instanceof( Promise );
  48. } );
  49. } );
  50. describe( 'destroy', () => {
  51. it( 'calls super.destroy', () => {
  52. const creatorSpy = testUtils.sinon.spy( Creator.prototype, 'destroy' );
  53. creator.destroy();
  54. expect( creatorSpy.called ).to.be.true;
  55. } );
  56. it( 'should destroy the engine', () => {
  57. const spy = editor.document.destroy = editor.data.destroy = editor.editing.destroy = sinon.spy();
  58. creator.destroy();
  59. expect( spy.callCount ).to.equal( 3 );
  60. } );
  61. it( 'should restore the replaced element', () => {
  62. const spy = testUtils.sinon.stub( creator, '_restoreElements' );
  63. creator.destroy();
  64. expect( spy.calledOnce ).to.be.true;
  65. } );
  66. } );
  67. describe( 'updateEditorElement', () => {
  68. it( 'should pass data to the first element when element name not specified', () => {
  69. editor.getData = ( rootName ) => {
  70. expect( rootName ).to.equal( 'first' );
  71. return 'foo';
  72. };
  73. creator.updateEditorElement();
  74. expect( editor.firstElement.innerHTML ).to.equal( 'foo' );
  75. } );
  76. it( 'should pass data to the given element', () => {
  77. editor.elements.set( 'second', document.createElement( 'div' ) );
  78. editor.getData = ( rootName ) => {
  79. expect( rootName ).to.equal( 'second' );
  80. return 'foo';
  81. };
  82. creator.updateEditorElement( 'second' );
  83. expect( editor.elements.get( 'second' ).innerHTML ).to.equal( 'foo' );
  84. } );
  85. } );
  86. describe( 'updateEditorElements', () => {
  87. it( 'updates all editor elements', () => {
  88. const spy = sinon.stub( creator, 'updateEditorElement' );
  89. creator.updateEditorElements();
  90. expect( spy.calledTwice ).to.be.true;
  91. expect( spy.calledWith( 'first' ) ).to.be.true;
  92. expect( spy.calledWith( 'second' ) ).to.be.true;
  93. } );
  94. } );
  95. describe( 'loadDataFromEditorElement', () => {
  96. it( 'should pass data to the first element', () => {
  97. editor.setData = sinon.spy();
  98. editor.elements.get( 'first' ).innerHTML = 'foo';
  99. creator.loadDataFromEditorElement();
  100. expect( editor.setData.calledWithExactly( 'foo', 'first' ) ).to.be.true;
  101. } );
  102. it( 'should pass data to the given element', () => {
  103. const element = document.createElement( 'div' );
  104. element.innerHTML = 'foo';
  105. editor.elements.set( 'second', element );
  106. editor.setData = sinon.spy();
  107. creator.loadDataFromEditorElement( 'second' );
  108. expect( editor.setData.calledWithExactly( 'foo', 'second' ) ).to.be.true;
  109. } );
  110. } );
  111. describe( 'loadDataFromEditorElements', () => {
  112. it( 'updates all editor elements', () => {
  113. const spy = sinon.stub( creator, 'loadDataFromEditorElement' );
  114. creator.loadDataFromEditorElements();
  115. expect( spy.calledTwice ).to.be.true;
  116. expect( spy.calledWith( 'first' ) ).to.be.true;
  117. expect( spy.calledWith( 'second' ) ).to.be.true;
  118. } );
  119. } );
  120. describe( 'getDataFromElement', () => {
  121. [ 'textarea', 'template', 'div' ].forEach( ( elementName ) => {
  122. it( 'should return the content of a ' + elementName, function() {
  123. const data = StandardCreator.getDataFromElement( document.getElementById( 'getData-' + elementName ) );
  124. expect( data ).to.equal( '<b>foo</b>' );
  125. } );
  126. } );
  127. } );
  128. describe( 'setDataInElement', () => {
  129. [ 'textarea', 'template', 'div' ].forEach( ( elementName ) => {
  130. it( 'should set the content of a ' + elementName, () => {
  131. const el = document.createElement( elementName );
  132. const expectedData = '<b>foo</b>';
  133. StandardCreator.setDataInElement( el, expectedData );
  134. const actualData = StandardCreator.getDataFromElement( el );
  135. expect( actualData ).to.equal( actualData );
  136. } );
  137. } );
  138. } );
  139. describe( '_replaceElement', () => {
  140. it( 'should hide the element', () => {
  141. const el = editor.elements.get( 'first' );
  142. creator._replaceElement( el );
  143. expect( el.style.display ).to.equal( 'none' );
  144. } );
  145. it( 'should inserts the replacement next to the element being hidden', () => {
  146. const el = editor.elements.get( 'first' );
  147. const replacement = document.createElement( 'div' );
  148. creator._replaceElement( el, replacement );
  149. expect( el.nextSibling ).to.equal( replacement );
  150. } );
  151. } );
  152. describe( '_restoreElements', () => {
  153. it( 'should restore all elements', () => {
  154. const el1 = editor.elements.get( 'first' );
  155. const replacement1 = document.createElement( 'div' );
  156. const el2 = editor.elements.get( 'second' );
  157. const replacement2 = document.createElement( 'div' );
  158. creator._replaceElement( el1, replacement1 );
  159. creator._replaceElement( el2, replacement2 );
  160. creator._restoreElements();
  161. expect( replacement1.parentNode ).to.be.null;
  162. expect( replacement2.parentNode ).to.be.null;
  163. expect( el2.style.display ).to.not.equal( 'none' );
  164. } );
  165. it( 'should not try to remove replacement elements', () => {
  166. const el1 = editor.elements.get( 'first' );
  167. const el2 = editor.elements.get( 'second' );
  168. creator._replaceElement( el1 );
  169. creator._replaceElement( el2 );
  170. creator._restoreElements();
  171. expect( el1.style.display ).to.not.equal( 'none' );
  172. expect( el2.style.display ).to.not.equal( 'none' );
  173. } );
  174. } );
  175. } );