8
0

editor.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /* bender-tags: editor */
  7. import moduleUtils from '/tests/ckeditor5/_utils/module.js';
  8. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  9. import Editor from '/ckeditor5/editor.js';
  10. import Config from '/ckeditor5/utils/config.js';
  11. import PluginCollection from '/ckeditor5/plugincollection.js';
  12. import EditableCollection from '/ckeditor5/editablecollection.js';
  13. import Plugin from '/ckeditor5/plugin.js';
  14. import Command from '/ckeditor5/command/command.js';
  15. import Locale from '/ckeditor5/utils/locale.js';
  16. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  17. import Document from '/ckeditor5/engine/treemodel/document.js';
  18. const pluginClasses = {};
  19. before( () => {
  20. // Define fake plugins to be used in tests.
  21. testUtils.defineEditorCreatorMock( 'test', {
  22. init: sinon.spy().named( 'creator-test' )
  23. } );
  24. pluginDefinition( 'A/A' );
  25. pluginDefinition( 'B/B' );
  26. pluginDefinition( 'C/C', [ 'B/B' ] );
  27. pluginDefinition( 'D/D', [ 'C/C' ] );
  28. } );
  29. ///////////////////
  30. describe( 'Editor', () => {
  31. describe( 'constructor', () => {
  32. it( 'should create a new editor instance', () => {
  33. const editor = new Editor();
  34. expect( editor ).to.have.property( 'elements', null );
  35. expect( editor.config ).to.be.an.instanceof( Config );
  36. expect( editor.editables ).to.be.an.instanceof( EditableCollection );
  37. expect( editor.commands ).to.be.an.instanceof( Map );
  38. expect( editor.plugins ).to.be.an.instanceof( PluginCollection );
  39. expect( getPlugins( editor ) ).to.be.empty;
  40. } );
  41. } );
  42. describe( 'locale', () => {
  43. it( 'is instantiated and t() is exposed', () => {
  44. const editor = new Editor();
  45. expect( editor.locale ).to.be.instanceof( Locale );
  46. expect( editor.t ).to.equal( editor.locale.t );
  47. } );
  48. it( 'is configured with the config.lang', () => {
  49. const editor = new Editor( null, { lang: 'pl' } );
  50. expect( editor.locale.lang ).to.equal( 'pl' );
  51. } );
  52. } );
  53. describe( 'plugins', () => {
  54. it( 'should be empty on new editor', () => {
  55. const editor = new Editor();
  56. expect( getPlugins( editor ) ).to.be.empty;
  57. } );
  58. } );
  59. describe( 'firstElement', () => {
  60. it( 'should be set to first element', () => {
  61. const editor = new Editor( { foo: 'a', bar: 'b' } );
  62. expect( editor.firstElement ).to.equal( 'a' );
  63. } );
  64. it( 'should be set to null if there are no elements', () => {
  65. const editor = new Editor();
  66. expect( editor.firstElement ).to.be.null;
  67. } );
  68. } );
  69. describe( 'firstElementName', () => {
  70. it( 'should be set to first element name', () => {
  71. const editor = new Editor( { foo: 'a', bar: 'b' } );
  72. expect( editor.firstElementName ).to.equal( 'foo' );
  73. } );
  74. it( 'should be set to null if there are no elements', () => {
  75. const editor = new Editor();
  76. expect( editor.firstElementName ).to.be.null;
  77. } );
  78. } );
  79. describe( 'init', () => {
  80. it( 'should return a promise that resolves properly', () => {
  81. const editor = new Editor( null, {
  82. creator: 'creator-test'
  83. } );
  84. let promise = editor.init();
  85. expect( promise ).to.be.an.instanceof( Promise );
  86. return promise;
  87. } );
  88. it( 'should load features and creator', () => {
  89. const editor = new Editor( null, {
  90. features: [ 'A', 'B' ],
  91. creator: 'creator-test'
  92. } );
  93. expect( getPlugins( editor ) ).to.be.empty;
  94. return editor.init().then( () => {
  95. expect( getPlugins( editor ).length ).to.equal( 3 );
  96. expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
  97. expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
  98. expect( editor.plugins.get( 'creator-test' ) ).to.be.an.instanceof( Plugin );
  99. } );
  100. } );
  101. it( 'should load features passed as a string', () => {
  102. const editor = new Editor( null, {
  103. features: 'A,B',
  104. creator: 'creator-test'
  105. } );
  106. expect( getPlugins( editor ) ).to.be.empty;
  107. return editor.init().then( () => {
  108. expect( getPlugins( editor ).length ).to.equal( 3 );
  109. expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
  110. expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
  111. } );
  112. } );
  113. it( 'should initialize plugins in the right order', () => {
  114. const editor = new Editor( null, {
  115. features: [ 'A', 'D' ],
  116. creator: 'creator-test'
  117. } );
  118. return editor.init().then( () => {
  119. sinon.assert.callOrder(
  120. editor.plugins.get( 'creator-test' ).init,
  121. editor.plugins.get( pluginClasses[ 'A/A' ] ).init,
  122. editor.plugins.get( pluginClasses[ 'B/B' ] ).init,
  123. editor.plugins.get( pluginClasses[ 'C/C' ] ).init,
  124. editor.plugins.get( pluginClasses[ 'D/D' ] ).init
  125. );
  126. } );
  127. } );
  128. it( 'should initialize plugins in the right order, waiting for asynchronous ones', () => {
  129. class PluginAsync extends Plugin {}
  130. const asyncSpy = sinon.spy().named( 'async-call-spy' );
  131. // Synchronous plugin that depends on an asynchronous one.
  132. pluginDefinition( 'sync/sync', [ 'async/async' ] );
  133. moduleUtils.define( 'async/async', () => {
  134. PluginAsync.prototype.init = sinon.spy( () => {
  135. return new Promise( ( resolve ) => {
  136. setTimeout( () => {
  137. asyncSpy();
  138. resolve();
  139. }, 0 );
  140. } );
  141. } );
  142. return PluginAsync;
  143. } );
  144. const editor = new Editor( null, {
  145. features: [ 'A', 'sync' ],
  146. creator: 'creator-test'
  147. } );
  148. return editor.init().then( () => {
  149. sinon.assert.callOrder(
  150. editor.plugins.get( 'creator-test' ).init,
  151. editor.plugins.get( pluginClasses[ 'A/A' ] ).init,
  152. editor.plugins.get( PluginAsync ).init,
  153. // This one is called with delay by the async init.
  154. asyncSpy,
  155. editor.plugins.get( pluginClasses[ 'sync/sync' ] ).init
  156. );
  157. } );
  158. } );
  159. } );
  160. describe( 'destroy', () => {
  161. it( 'should fire "destroy"', () => {
  162. const editor = new Editor();
  163. let spy = sinon.spy();
  164. editor.on( 'destroy', spy );
  165. return editor.destroy().then( () => {
  166. expect( spy.calledOnce ).to.be.true;
  167. } );
  168. } );
  169. // Note: Tests for destroying creators are in creator/creator.js.
  170. // When destroying creator will be generalized to destroying plugins,
  171. // move that code here.
  172. } );
  173. describe( 'execute', () => {
  174. it( 'should execute specified command', () => {
  175. const editor = new Editor();
  176. let command = new Command( editor );
  177. sinon.spy( command, '_execute' );
  178. editor.commands.set( 'commandName', command );
  179. editor.execute( 'commandName' );
  180. expect( command._execute.calledOnce ).to.be.true;
  181. } );
  182. it( 'should throw an error if specified command has not been added', () => {
  183. const editor = new Editor();
  184. expect( () => {
  185. editor.execute( 'command' );
  186. } ).to.throw( CKEditorError, /^editor-command-not-found:/ );
  187. } );
  188. } );
  189. describe( 'setData', () => {
  190. let editor;
  191. beforeEach( () => {
  192. editor = new Editor();
  193. editor.document = new Document();
  194. editor.data = {
  195. set: sinon.spy()
  196. };
  197. } );
  198. it( 'should set data of the first root', () => {
  199. editor.document.createRoot( 'firstRoot', 'div' );
  200. editor.setData( 'foo' );
  201. expect( editor.data.set.calledOnce ).to.be.true;
  202. expect( editor.data.set.calledWithExactly( 'firstRoot', 'foo' ) ).to.be.true;
  203. } );
  204. it( 'should set data of the specified root', () => {
  205. editor.setData( 'foo', 'someRoot' );
  206. expect( editor.data.set.calledOnce ).to.be.true;
  207. expect( editor.data.set.calledWithExactly( 'someRoot', 'foo' ) ).to.be.true;
  208. } );
  209. it( 'should throw when no roots', () => {
  210. expect( () => {
  211. editor.setData( 'foo' );
  212. } ).to.throw( CKEditorError, /^editor-no-editable-roots:/ );
  213. } );
  214. it( 'should throw when more than one root and no root name given', () => {
  215. editor.document.createRoot( 'firstRoot', 'div' );
  216. editor.document.createRoot( 'secondRoot', 'div' );
  217. expect( () => {
  218. editor.setData( 'foo' );
  219. } ).to.throw( CKEditorError, /^editor-editable-root-name-missing:/ );
  220. } );
  221. it( 'should throw when no data controller', () => {
  222. expect( () => {
  223. editor.data = null;
  224. editor.setData( 'foo' );
  225. } ).to.throw( CKEditorError, /^editor-no-datacontroller:/ );
  226. } );
  227. } );
  228. describe( 'getData', () => {
  229. let editor;
  230. beforeEach( () => {
  231. editor = new Editor();
  232. editor.document = new Document();
  233. editor.data = {
  234. get( rootName ) {
  235. return `data for ${ rootName }`;
  236. }
  237. };
  238. } );
  239. it( 'should get data from the first root', () => {
  240. editor.document.createRoot( 'firstRoot', 'div' );
  241. expect( editor.getData() ).to.equal( 'data for firstRoot' );
  242. } );
  243. it( 'should get data from the specified root', () => {
  244. expect( editor.getData( 'someRoot' ) ).to.equal( 'data for someRoot' );
  245. } );
  246. it( 'should throw when no roots', () => {
  247. expect( () => {
  248. editor.getData();
  249. } ).to.throw( CKEditorError, /^editor-no-editable-roots:/ );
  250. } );
  251. it( 'should throw when more than one root and no root name given', () => {
  252. editor.document.createRoot( 'firstRoot', 'div' );
  253. editor.document.createRoot( 'secondRoot', 'div' );
  254. expect( () => {
  255. editor.getData();
  256. } ).to.throw( CKEditorError, /^editor-editable-root-name-missing:/ );
  257. } );
  258. it( 'should throw when no data controller', () => {
  259. expect( () => {
  260. editor.data = null;
  261. editor.getData();
  262. } ).to.throw( CKEditorError, /^editor-no-datacontroller:/ );
  263. } );
  264. } );
  265. } );
  266. /**
  267. * @param {String} name Name of the plugin.
  268. * @param {String[]} deps Dependencies of the plugin (only other plugins).
  269. */
  270. function pluginDefinition( name, deps ) {
  271. moduleUtils.define( name, deps || [], function() {
  272. class NewPlugin extends Plugin {}
  273. NewPlugin.prototype.init = sinon.spy().named( name );
  274. NewPlugin.requires = Array.from( arguments );
  275. pluginClasses[ name ] = NewPlugin;
  276. return NewPlugin;
  277. } );
  278. }
  279. /**
  280. * Returns an array of loaded plugins.
  281. */
  282. function getPlugins( editor ) {
  283. const plugins = [];
  284. for ( let entry of editor.plugins ) {
  285. // Keep only plugins kept under their classes.
  286. if ( typeof entry[ 0 ] == 'function' ) {
  287. plugins.push( entry[ 1 ] );
  288. }
  289. }
  290. return plugins;
  291. }