8
0

standardcreator.js 6.8 KB

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