baseeditor.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: editor */
  6. 'use strict';
  7. import Editor from '/ckeditor5/editor.js';
  8. import Command from '/ckeditor5/command/command.js';
  9. import Locale from '/ckeditor5/utils/locale.js';
  10. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  11. import Document from '/ckeditor5/engine/model/document.js';
  12. import DataController from '/ckeditor5/engine/datacontroller.js';
  13. import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
  14. import { getData, setData } from '/tests/engine/_utils/model.js';
  15. describe( 'Editor', () => {
  16. describe( 'locale', () => {
  17. it( 'is instantiated and t() is exposed', () => {
  18. const editor = new Editor();
  19. expect( editor.locale ).to.be.instanceof( Locale );
  20. expect( editor.t ).to.equal( editor.locale.t );
  21. } );
  22. it( 'is configured with the config.lang', () => {
  23. const editor = new Editor( null, { lang: 'pl' } );
  24. expect( editor.locale.lang ).to.equal( 'pl' );
  25. } );
  26. } );
  27. describe( 'destroy', () => {
  28. it( 'should fire "destroy"', () => {
  29. const editor = new Editor();
  30. let spy = sinon.spy();
  31. editor.on( 'destroy', spy );
  32. return editor.destroy().then( () => {
  33. expect( spy.calledOnce ).to.be.true;
  34. } );
  35. } );
  36. // Note: Tests for destroying creators are in creator/creator.js.
  37. // When destroying creator will be generalized to destroying plugins,
  38. // move that code here.
  39. } );
  40. describe( 'execute', () => {
  41. it( 'should execute specified command', () => {
  42. const editor = new Editor();
  43. let command = new Command( editor );
  44. sinon.spy( command, '_execute' );
  45. editor.commands.set( 'commandName', command );
  46. editor.execute( 'commandName' );
  47. expect( command._execute.calledOnce ).to.be.true;
  48. } );
  49. it( 'should throw an error if specified command has not been added', () => {
  50. const editor = new Editor();
  51. expect( () => {
  52. editor.execute( 'command' );
  53. } ).to.throw( CKEditorError, /^editor-command-not-found:/ );
  54. } );
  55. } );
  56. describe( 'setData', () => {
  57. let editor;
  58. beforeEach( () => {
  59. editor = new Editor();
  60. editor.document = new Document();
  61. editor.data = {
  62. set: sinon.spy()
  63. };
  64. } );
  65. it( 'should set data of the first root', () => {
  66. editor.document.createRoot( 'firstRoot', 'div' );
  67. editor.setData( 'foo' );
  68. expect( editor.data.set.calledOnce ).to.be.true;
  69. expect( editor.data.set.calledWithExactly( 'foo', 'firstRoot' ) ).to.be.true;
  70. } );
  71. it( 'should set data of the specified root', () => {
  72. editor.setData( 'foo', 'someRoot' );
  73. expect( editor.data.set.calledOnce ).to.be.true;
  74. expect( editor.data.set.calledWithExactly( 'foo', 'someRoot' ) ).to.be.true;
  75. } );
  76. it( 'should throw when no roots', () => {
  77. expect( () => {
  78. editor.setData( 'foo' );
  79. } ).to.throw( CKEditorError, /^editor-no-editable-roots:/ );
  80. } );
  81. it( 'should throw when more than one root and no root name given', () => {
  82. editor.document.createRoot( 'firstRoot', 'div' );
  83. editor.document.createRoot( 'secondRoot', 'div' );
  84. expect( () => {
  85. editor.setData( 'foo' );
  86. } ).to.throw( CKEditorError, /^editor-editable-root-name-missing:/ );
  87. } );
  88. it( 'should throw when no data controller', () => {
  89. expect( () => {
  90. editor.data = null;
  91. editor.setData( 'foo' );
  92. } ).to.throw( CKEditorError, /^editor-no-datacontroller:/ );
  93. } );
  94. describe( 'integrational tests', () => {
  95. beforeEach( () => {
  96. editor.data = new DataController( editor.document, new HtmlDataProcessor() );
  97. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  98. } );
  99. it( 'should set data of the first root', () => {
  100. editor.document.createRoot( 'firstRoot', 'div' );
  101. editor.setData( 'foo' );
  102. expect( getData( editor.document, { rootName: 'firstRoot', withoutSelection: true } ) ).to.equal( 'foo' );
  103. } );
  104. it( 'should set data of the specified root', () => {
  105. editor.document.createRoot( 'firstRoot', 'div' );
  106. editor.document.createRoot( 'secondRoot', 'div' );
  107. editor.setData( 'foo', 'secondRoot' );
  108. expect( getData( editor.document, { rootName: 'secondRoot', withoutSelection: true } ) ).to.equal( 'foo' );
  109. } );
  110. } );
  111. } );
  112. describe( 'getData', () => {
  113. let editor;
  114. beforeEach( () => {
  115. editor = new Editor();
  116. editor.document = new Document();
  117. editor.data = {
  118. get( rootName ) {
  119. return `data for ${ rootName }`;
  120. }
  121. };
  122. } );
  123. it( 'should get data from the first root', () => {
  124. editor.document.createRoot( 'firstRoot', 'div' );
  125. expect( editor.getData() ).to.equal( 'data for firstRoot' );
  126. } );
  127. it( 'should get data from the specified root', () => {
  128. expect( editor.getData( 'someRoot' ) ).to.equal( 'data for someRoot' );
  129. } );
  130. it( 'should throw when no roots', () => {
  131. expect( () => {
  132. editor.getData();
  133. } ).to.throw( CKEditorError, /^editor-no-editable-roots:/ );
  134. } );
  135. it( 'should throw when more than one root and no root name given', () => {
  136. editor.document.createRoot( 'firstRoot', 'div' );
  137. editor.document.createRoot( 'secondRoot', 'div' );
  138. expect( () => {
  139. editor.getData();
  140. } ).to.throw( CKEditorError, /^editor-editable-root-name-missing:/ );
  141. } );
  142. it( 'should throw when no data controller', () => {
  143. expect( () => {
  144. editor.data = null;
  145. editor.getData();
  146. } ).to.throw( CKEditorError, /^editor-no-datacontroller:/ );
  147. } );
  148. describe( 'integrational tests', () => {
  149. beforeEach( () => {
  150. editor.data = new DataController( editor.document, new HtmlDataProcessor() );
  151. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  152. } );
  153. it( 'should set data of the first root', () => {
  154. editor.document.createRoot( 'firstRoot', 'div' );
  155. setData( editor.document, 'foo', { rootName: 'firstRoot' } );
  156. expect( editor.getData() ).to.equal( 'foo' );
  157. } );
  158. it( 'should set data of the specified root', () => {
  159. editor.document.createRoot( 'firstRoot', 'div' );
  160. editor.document.createRoot( 'secondRoot', 'div' );
  161. setData( editor.document, 'foo', { rootName: 'secondRoot' } );
  162. expect( editor.getData( 'secondRoot' ) ).to.equal( 'foo' );
  163. } );
  164. } );
  165. } );
  166. } );