8
0

editor.js 10 KB

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