standardcreator.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: creator */
  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/treemodel/document.js';
  13. import EditingController from '/ckeditor5/engine/treecontroller/editingcontroller.js';
  14. import DataController from '/ckeditor5/engine/treecontroller/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. } );
  37. describe( 'create', () => {
  38. it( 'returns a promise', () => {
  39. expect( creator.create() ).to.be.instanceof( Promise );
  40. } );
  41. } );
  42. describe( 'destroy', () => {
  43. it( 'calls super.destroy', () => {
  44. const creatorSpy = testUtils.sinon.spy( Creator.prototype, 'destroy' );
  45. creator.destroy();
  46. expect( creatorSpy.called ).to.be.true;
  47. } );
  48. it( 'should destroy the engine', () => {
  49. const spy = editor.document.destroy = editor.data.destroy = editor.editing.destroy = sinon.spy();
  50. creator.destroy();
  51. expect( spy.callCount ).to.equal( 3 );
  52. } );
  53. it( 'should restore the replaced element', () => {
  54. const spy = testUtils.sinon.stub( creator, '_restoreElements' );
  55. creator.destroy();
  56. expect( spy.calledOnce ).to.be.true;
  57. } );
  58. } );
  59. describe( 'updateEditorElement', () => {
  60. it( 'should pass data to the first element when element name not specified', () => {
  61. editor.getData = ( rootName ) => {
  62. expect( rootName ).to.equal( 'first' );
  63. return 'foo';
  64. };
  65. creator.updateEditorElement();
  66. expect( editor.firstElement.innerHTML ).to.equal( 'foo' );
  67. } );
  68. it( 'should pass data to the given element', () => {
  69. editor.elements.set( 'second', document.createElement( 'div' ) );
  70. editor.getData = ( rootName ) => {
  71. expect( rootName ).to.equal( 'second' );
  72. return 'foo';
  73. };
  74. creator.updateEditorElement( 'second' );
  75. expect( editor.elements.get( 'second' ).innerHTML ).to.equal( 'foo' );
  76. } );
  77. } );
  78. describe( 'updateEditorElements', () => {
  79. it( 'updates all editor elements', () => {
  80. const spy = sinon.stub( creator, 'updateEditorElement' );
  81. creator.updateEditorElements();
  82. expect( spy.calledTwice ).to.be.true;
  83. expect( spy.calledWith( 'first' ) ).to.be.true;
  84. expect( spy.calledWith( 'second' ) ).to.be.true;
  85. } );
  86. } );
  87. describe( 'loadDataFromEditorElement', () => {
  88. it( 'should pass data to the first element', () => {
  89. editor.setData = sinon.spy();
  90. editor.elements.get( 'first' ).innerHTML = 'foo';
  91. creator.loadDataFromEditorElement();
  92. expect( editor.setData.calledWithExactly( 'foo', 'first' ) ).to.be.true;
  93. } );
  94. it( 'should pass data to the given element', () => {
  95. const element = document.createElement( 'div' );
  96. element.innerHTML = 'foo';
  97. editor.elements.set( 'second', element );
  98. editor.setData = sinon.spy();
  99. creator.loadDataFromEditorElement( 'second' );
  100. expect( editor.setData.calledWithExactly( 'foo', 'second' ) ).to.be.true;
  101. } );
  102. } );
  103. describe( 'loadDataFromEditorElements', () => {
  104. it( 'updates all editor elements', () => {
  105. const spy = sinon.stub( creator, 'loadDataFromEditorElement' );
  106. creator.loadDataFromEditorElements();
  107. expect( spy.calledTwice ).to.be.true;
  108. expect( spy.calledWith( 'first' ) ).to.be.true;
  109. expect( spy.calledWith( 'second' ) ).to.be.true;
  110. } );
  111. } );
  112. describe( 'getDataFromElement', () => {
  113. [ 'textarea', 'template', 'div' ].forEach( ( elementName ) => {
  114. it( 'should return the content of a ' + elementName, function() {
  115. const data = StandardCreator.getDataFromElement( document.getElementById( 'getData-' + elementName ) );
  116. expect( data ).to.equal( '<b>foo</b>' );
  117. } );
  118. } );
  119. } );
  120. describe( 'setDataInElement', () => {
  121. [ 'textarea', 'template', 'div' ].forEach( ( elementName ) => {
  122. it( 'should set the content of a ' + elementName, () => {
  123. const el = document.createElement( elementName );
  124. const expectedData = '<b>foo</b>';
  125. StandardCreator.setDataInElement( el, expectedData );
  126. const actualData = StandardCreator.getDataFromElement( el );
  127. expect( actualData ).to.equal( actualData );
  128. } );
  129. } );
  130. } );
  131. describe( '_replaceElement', () => {
  132. it( 'should hide the element', () => {
  133. const el = editor.elements.get( 'first' );
  134. creator._replaceElement( el );
  135. expect( el.style.display ).to.equal( 'none' );
  136. } );
  137. it( 'should inserts the replacement next to the element being hidden', () => {
  138. const el = editor.elements.get( 'first' );
  139. const replacement = document.createElement( 'div' );
  140. creator._replaceElement( el, replacement );
  141. expect( el.nextSibling ).to.equal( replacement );
  142. } );
  143. } );
  144. describe( '_restoreElements', () => {
  145. it( 'should restore all elements', () => {
  146. const el1 = editor.elements.get( 'first' );
  147. const replacement1 = document.createElement( 'div' );
  148. const el2 = editor.elements.get( 'second' );
  149. const replacement2 = document.createElement( 'div' );
  150. creator._replaceElement( el1, replacement1 );
  151. creator._replaceElement( el2, replacement2 );
  152. creator._restoreElements();
  153. expect( replacement1.parentNode ).to.be.null;
  154. expect( replacement2.parentNode ).to.be.null;
  155. expect( el2.style.display ).to.not.equal( 'none' );
  156. } );
  157. it( 'should not try to remove replacement elements', () => {
  158. const el1 = editor.elements.get( 'first' );
  159. const el2 = editor.elements.get( 'second' );
  160. creator._replaceElement( el1 );
  161. creator._replaceElement( el2 );
  162. creator._restoreElements();
  163. expect( el1.style.display ).to.not.equal( 'none' );
  164. expect( el2.style.display ).to.not.equal( 'none' );
  165. } );
  166. } );
  167. } );