creator.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 BaseCreator from '/ckeditor5/creator/basecreator.js';
  9. import Creator from '/ckeditor5/creator/creator.js';
  10. import Editor from '/ckeditor5/editor.js';
  11. import HtmlDataProcessor from '/ckeditor5/core/dataprocessor/htmldataprocessor.js';
  12. import Document from '/ckeditor5/core/treemodel/document.js';
  13. import EditingController from '/ckeditor5/core/treecontroller/editingcontroller.js';
  14. import DataController from '/ckeditor5/core/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 Creator( editor, new HtmlDataProcessor() );
  25. } );
  26. describe( 'constructor', () => {
  27. it( 'inherits from the BaseCreator', () => {
  28. expect( creator ).to.be.instanceof( BaseCreator );
  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 baseCreatorSpy = testUtils.sinon.spy( BaseCreator.prototype, 'destroy' );
  45. creator.destroy();
  46. expect( baseCreatorSpy.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( 'loadDataFromEditorElement', () => {
  79. it( 'should pass data to the first element', () => {
  80. editor.setData = sinon.spy();
  81. editor.elements.get( 'first' ).innerHTML = 'foo';
  82. creator.loadDataFromEditorElement();
  83. expect( editor.setData.calledWithExactly( 'foo', 'first' ) ).to.be.true;
  84. } );
  85. it( 'should pass data to the given element', () => {
  86. const element = document.createElement( 'div' );
  87. element.innerHTML = 'foo';
  88. editor.elements.set( 'second', element );
  89. editor.setData = sinon.spy();
  90. creator.loadDataFromEditorElement( 'second' );
  91. expect( editor.setData.calledWithExactly( 'foo', 'second' ) ).to.be.true;
  92. } );
  93. } );
  94. describe( 'getDataFromElement', () => {
  95. [ 'textarea', 'template', 'div' ].forEach( ( elementName ) => {
  96. it( 'should return the content of a ' + elementName, function() {
  97. const data = Creator.getDataFromElement( document.getElementById( 'getData-' + elementName ) );
  98. expect( data ).to.equal( '<b>foo</b>' );
  99. } );
  100. } );
  101. } );
  102. describe( 'setDataInElement', () => {
  103. [ 'textarea', 'template', 'div' ].forEach( ( elementName ) => {
  104. it( 'should set the content of a ' + elementName, () => {
  105. const el = document.createElement( elementName );
  106. const expectedData = '<b>foo</b>';
  107. Creator.setDataInElement( el, expectedData );
  108. const actualData = Creator.getDataFromElement( el );
  109. expect( actualData ).to.equal( actualData );
  110. } );
  111. } );
  112. } );
  113. describe( '_replaceElement', () => {
  114. it( 'should hide the element', () => {
  115. const el = editor.elements.get( 'first' );
  116. creator._replaceElement( el );
  117. expect( el.style.display ).to.equal( 'none' );
  118. } );
  119. it( 'should inserts the replacement next to the element being hidden', () => {
  120. const el = editor.elements.get( 'first' );
  121. const replacement = document.createElement( 'div' );
  122. creator._replaceElement( el, replacement );
  123. expect( el.nextSibling ).to.equal( replacement );
  124. } );
  125. } );
  126. describe( '_restoreElement', () => {
  127. it( 'should restore all elements', () => {
  128. const el1 = editor.elements.get( 'first' );
  129. const replacement1 = document.createElement( 'div' );
  130. const el2 = editor.elements.get( 'second' );
  131. const replacement2 = document.createElement( 'div' );
  132. creator._replaceElement( el1, replacement1 );
  133. creator._replaceElement( el2, replacement2 );
  134. creator._restoreElements();
  135. expect( replacement1.parentNode ).to.be.null;
  136. expect( replacement2.parentNode ).to.be.null;
  137. expect( el2.style.display ).to.not.equal( 'none' );
  138. } );
  139. } );
  140. } );