imagestyleengine.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import ImageStyleEngine from '../../src/imagestyle/imagestyleengine';
  7. import ImageEngine from '../../src/image/imageengine';
  8. import ImageStyleCommand from '../../src/imagestyle/imagestylecommand';
  9. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  10. import log from '@ckeditor/ckeditor5-utils/src/log';
  11. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  13. import fullWidthIcon from '@ckeditor/ckeditor5-core/theme/icons/object-full-width.svg';
  14. import leftIcon from '@ckeditor/ckeditor5-core/theme/icons/object-left.svg';
  15. import centerIcon from '@ckeditor/ckeditor5-core/theme/icons/object-center.svg';
  16. import rightIcon from '@ckeditor/ckeditor5-core/theme/icons/object-right.svg';
  17. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  18. testUtils.createSinonSandbox();
  19. describe( 'ImageStyleEngine', () => {
  20. let editor, plugin, model, document, viewDocument;
  21. afterEach( () => {
  22. editor.destroy();
  23. } );
  24. describe( 'plugin', () => {
  25. beforeEach( () => {
  26. return VirtualTestEditor
  27. .create( {
  28. plugins: [ ImageStyleEngine ],
  29. } )
  30. .then( newEditor => {
  31. editor = newEditor;
  32. } );
  33. } );
  34. it( 'should be loaded', () => {
  35. expect( editor.plugins.get( ImageStyleEngine ) ).to.be.instanceOf( ImageStyleEngine );
  36. } );
  37. it( 'should load image engine', () => {
  38. expect( editor.plugins.get( ImageEngine ) ).to.be.instanceOf( ImageEngine );
  39. } );
  40. } );
  41. describe( 'init', () => {
  42. beforeEach( () => {
  43. return VirtualTestEditor
  44. .create( {
  45. plugins: [ ImageStyleEngine ],
  46. image: {
  47. styles: [
  48. { name: 'fullStyle', title: 'foo', icon: 'object-center', isDefault: true },
  49. { name: 'sideStyle', title: 'bar', icon: 'object-right', className: 'side-class' },
  50. { name: 'dummyStyle', title: 'baz', icon: 'object-dummy', className: 'dummy-class' },
  51. ]
  52. }
  53. } )
  54. .then( newEditor => {
  55. editor = newEditor;
  56. model = editor.model;
  57. document = model.document;
  58. viewDocument = editor.editing.view;
  59. } );
  60. } );
  61. it( 'should define image.styles config', () => {
  62. return VirtualTestEditor
  63. .create( {
  64. plugins: [ ImageStyleEngine ]
  65. } )
  66. .then( newEditor => {
  67. editor = newEditor;
  68. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'imageStyleFull', 'imageStyleSide' ] );
  69. } );
  70. } );
  71. it( 'should set schema rules for image style', () => {
  72. const schema = model.schema;
  73. expect( schema.checkAttribute( [ '$root', 'image' ], 'imageStyle' ) ).to.be.true;
  74. } );
  75. it( 'should register separate command for each style', () => {
  76. expect( editor.commands.get( 'fullStyle' ) ).to.be.instanceOf( ImageStyleCommand );
  77. expect( editor.commands.get( 'sideStyle' ) ).to.be.instanceOf( ImageStyleCommand );
  78. expect( editor.commands.get( 'dummyStyle' ) ).to.be.instanceOf( ImageStyleCommand );
  79. } );
  80. it( 'should convert from view to model', () => {
  81. editor.setData( '<figure class="image side-class"><img src="foo.png" /></figure>' );
  82. expect( getModelData( model, { withoutSelection: true } ) )
  83. .to.equal( '<image imageStyle="sideStyle" src="foo.png"></image>' );
  84. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  85. '<figure class="ck-widget image side-class" contenteditable="false">' +
  86. '<img src="foo.png"></img>' +
  87. '</figure>' );
  88. } );
  89. it( 'should not convert from view to model if class is not defined', () => {
  90. editor.setData( '<figure class="image foo-bar"><img src="foo.png" /></figure>' );
  91. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  92. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  93. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  94. );
  95. } );
  96. it( 'should not convert from view to model when not in image figure', () => {
  97. editor.setData( '<figure class="side-class"></figure>' );
  98. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '' );
  99. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( '' );
  100. } );
  101. it( 'should not convert from view to model if schema prevents it', () => {
  102. model.schema.on( 'checkAttribute', ( evt, args ) => {
  103. if ( args[ 0 ].endsWith( 'image' ) && args[ 1 ] == 'imageStyle' ) {
  104. evt.stop();
  105. evt.return = false;
  106. }
  107. } );
  108. editor.setData( '<figure class="image side-class"><img src="foo.png" /></figure>' );
  109. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  110. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  111. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  112. );
  113. } );
  114. it( 'should convert model to view: adding attribute', () => {
  115. setModelData( model, '<image src="foo.png"></image>' );
  116. const image = document.getRoot().getChild( 0 );
  117. model.change( writer => {
  118. writer.setAttribute( 'imageStyle', 'sideStyle', image );
  119. } );
  120. expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
  121. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  122. '<figure class="ck-widget image side-class" contenteditable="false"><img src="foo.png"></img></figure>'
  123. );
  124. } );
  125. it( 'should convert model to view: removing attribute', () => {
  126. setModelData( model, '<image src="foo.png" imageStyle="sideStyle"></image>' );
  127. const image = document.getRoot().getChild( 0 );
  128. model.change( writer => {
  129. writer.setAttribute( 'imageStyle', null, image );
  130. } );
  131. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  132. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  133. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  134. );
  135. } );
  136. it( 'should convert model to view: change attribute', () => {
  137. setModelData( model, '<image src="foo.png" imageStyle="dummy"></image>' );
  138. const image = document.getRoot().getChild( 0 );
  139. model.change( writer => {
  140. writer.setAttribute( 'imageStyle', 'sideStyle', image );
  141. } );
  142. expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
  143. // https://github.com/ckeditor/ckeditor5-image/issues/132
  144. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  145. '<figure class="ck-widget image side-class" contenteditable="false"><img src="foo.png"></img></figure>'
  146. );
  147. model.change( writer => {
  148. writer.setAttribute( 'imageStyle', 'dummyStyle', image );
  149. } );
  150. expect( editor.getData() ).to.equal( '<figure class="image dummy-class"><img src="foo.png"></figure>' );
  151. // https://github.com/ckeditor/ckeditor5-image/issues/132
  152. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  153. '<figure class="ck-widget dummy-class image" contenteditable="false"><img src="foo.png"></img></figure>'
  154. );
  155. } );
  156. it( 'should not convert from model to view if already consumed: adding attribute', () => {
  157. editor.editing.modelToView.on( 'attribute:imageStyle', ( evt, data, consumable ) => {
  158. consumable.consume( data.item, 'attribute:imageStyle' );
  159. }, { priority: 'high' } );
  160. setModelData( model, '<image src="foo.png"></image>' );
  161. const image = document.getRoot().getChild( 0 );
  162. model.change( writer => {
  163. writer.setAttribute( 'imageStyle', 'sideStyle', image );
  164. } );
  165. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  166. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  167. );
  168. } );
  169. it( 'should not set attribute if change was already consumed', () => {
  170. editor.editing.modelToView.on( 'attribute:imageStyle', ( evt, data, consumable ) => {
  171. consumable.consume( data.item, 'attribute:imageStyle' );
  172. }, { priority: 'high' } );
  173. setModelData( model, '<image src="foo.png" imageStyle="dummyStyle"></image>' );
  174. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  175. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  176. );
  177. } );
  178. it( 'should not convert from model to view if style is not present: adding attribute', () => {
  179. setModelData( model, '<image src="foo.png"></image>' );
  180. const image = document.getRoot().getChild( 0 );
  181. model.change( writer => {
  182. writer.setAttribute( 'imageStyle', 'foo', image );
  183. } );
  184. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  185. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  186. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  187. );
  188. } );
  189. it( 'should not convert from model to view if style is not present: change attribute', () => {
  190. setModelData( model, '<image src="foo.png" imageStyle="dummy"></image>' );
  191. const image = document.getRoot().getChild( 0 );
  192. model.change( writer => {
  193. writer.setAttribute( 'imageStyle', 'foo', image );
  194. } );
  195. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  196. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  197. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  198. );
  199. } );
  200. it( 'should not convert from model to view if style is not present: remove attribute', () => {
  201. setModelData( model, '<image src="foo.png" imageStyle="foo"></image>' );
  202. const image = document.getRoot().getChild( 0 );
  203. model.change( writer => {
  204. writer.setAttribute( 'imageStyle', null, image );
  205. } );
  206. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  207. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  208. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  209. );
  210. } );
  211. } );
  212. describe( 'imageStyles()', () => {
  213. it( 'should fall back to defaults when no image.styles', () => {
  214. return VirtualTestEditor
  215. .create( {
  216. plugins: [ ImageStyleEngine ]
  217. } )
  218. .then( newEditor => {
  219. editor = newEditor;
  220. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'imageStyleFull', 'imageStyleSide' ] );
  221. } );
  222. } );
  223. it( 'should not alter the image.styles config', () => {
  224. return VirtualTestEditor
  225. .create( {
  226. plugins: [ ImageStyleEngine ],
  227. image: {
  228. styles: [
  229. 'imageStyleSide'
  230. ]
  231. }
  232. } )
  233. .then( newEditor => {
  234. editor = newEditor;
  235. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'imageStyleSide' ] );
  236. } );
  237. } );
  238. it( 'should not alter object definitions in the image.styles config', () => {
  239. return VirtualTestEditor
  240. .create( {
  241. plugins: [ ImageStyleEngine ],
  242. image: {
  243. styles: [
  244. { name: 'imageStyleSide' }
  245. ]
  246. }
  247. } )
  248. .then( newEditor => {
  249. editor = newEditor;
  250. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ { name: 'imageStyleSide' } ] );
  251. } );
  252. } );
  253. it( 'should cache the styles', () => {
  254. return VirtualTestEditor
  255. .create( {
  256. plugins: [ ImageStyleEngine ]
  257. } )
  258. .then( newEditor => {
  259. editor = newEditor;
  260. plugin = editor.plugins.get( ImageStyleEngine );
  261. expect( plugin.imageStyles ).to.equal( plugin.imageStyles );
  262. } );
  263. } );
  264. describe( 'object format', () => {
  265. beforeEach( () => {
  266. class TranslationMock extends Plugin {
  267. init() {
  268. sinon.stub( this.editor, 't' ).returns( 'Default translation' );
  269. }
  270. }
  271. return VirtualTestEditor
  272. .create( {
  273. plugins: [ TranslationMock, ImageStyleEngine ],
  274. image: {
  275. styles: [
  276. // Custom user styles.
  277. { name: 'foo', title: 'foo', icon: 'custom', isDefault: true, className: 'foo-class' },
  278. { name: 'bar', title: 'bar', icon: 'right', className: 'bar-class' },
  279. { name: 'baz', title: 'Side image', icon: 'custom', className: 'baz-class' },
  280. // Customized default styles.
  281. { name: 'imageStyleFull', icon: 'left', title: 'Custom title' }
  282. ]
  283. }
  284. } )
  285. .then( newEditor => {
  286. editor = newEditor;
  287. plugin = editor.plugins.get( ImageStyleEngine );
  288. } );
  289. } );
  290. it( 'should pass through if #name not found in default styles', () => {
  291. expect( plugin.imageStyles[ 0 ] ).to.deep.equal( {
  292. name: 'foo',
  293. title: 'foo',
  294. icon: 'custom',
  295. isDefault: true,
  296. className: 'foo-class'
  297. } );
  298. } );
  299. it( 'should use one of default icons if #icon matches', () => {
  300. expect( plugin.imageStyles[ 1 ].icon ).to.equal( ImageStyleEngine.defaultIcons.right );
  301. } );
  302. it( 'should use one of default translations if #title matches', () => {
  303. expect( plugin.imageStyles[ 2 ].title ).to.deep.equal( 'Default translation' );
  304. } );
  305. it( 'should extend one of default styles if #name matches', () => {
  306. expect( plugin.imageStyles[ 3 ] ).to.deep.equal( {
  307. name: 'imageStyleFull',
  308. title: 'Custom title',
  309. icon: ImageStyleEngine.defaultIcons.left,
  310. isDefault: true
  311. } );
  312. } );
  313. } );
  314. describe( 'string format', () => {
  315. it( 'should use one of default styles if #name matches', () => {
  316. return VirtualTestEditor
  317. .create( {
  318. plugins: [ ImageStyleEngine ],
  319. image: {
  320. styles: [ 'imageStyleFull' ]
  321. }
  322. } )
  323. .then( newEditor => {
  324. editor = newEditor;
  325. plugin = editor.plugins.get( ImageStyleEngine );
  326. expect( plugin.imageStyles[ 0 ] ).to.deep.equal( ImageStyleEngine.defaultStyles.imageStyleFull );
  327. } );
  328. } );
  329. it( 'should warn if a #name not found in default styles', () => {
  330. testUtils.sinon.stub( log, 'warn' );
  331. return VirtualTestEditor
  332. .create( {
  333. plugins: [ ImageStyleEngine ],
  334. image: {
  335. styles: [ 'foo' ]
  336. }
  337. } )
  338. .then( newEditor => {
  339. editor = newEditor;
  340. plugin = editor.plugins.get( ImageStyleEngine );
  341. expect( plugin.imageStyles[ 0 ] ).to.deep.equal( {
  342. name: 'foo'
  343. } );
  344. sinon.assert.calledOnce( log.warn );
  345. sinon.assert.calledWithExactly( log.warn,
  346. sinon.match( /^image-style-not-found/ ),
  347. { name: 'foo' }
  348. );
  349. } );
  350. } );
  351. } );
  352. } );
  353. describe( 'localizedDefaultStylesTitles()', () => {
  354. it( 'should return localized titles of default styles', () => {
  355. return VirtualTestEditor
  356. .create( {
  357. plugins: [ ImageStyleEngine ]
  358. } )
  359. .then( newEditor => {
  360. editor = newEditor;
  361. plugin = editor.plugins.get( ImageStyleEngine );
  362. expect( plugin.localizedDefaultStylesTitles ).to.deep.equal( {
  363. 'Full size image': 'Full size image',
  364. 'Side image': 'Side image',
  365. 'Left aligned image': 'Left aligned image',
  366. 'Centered image': 'Centered image',
  367. 'Right aligned image': 'Right aligned image'
  368. } );
  369. } );
  370. } );
  371. } );
  372. describe( 'defaultStyles', () => {
  373. it( 'should be defined', () => {
  374. expect( ImageStyleEngine.defaultStyles ).to.deep.equal( {
  375. imageStyleFull: {
  376. name: 'imageStyleFull',
  377. title: 'Full size image',
  378. icon: fullWidthIcon,
  379. isDefault: true
  380. },
  381. imageStyleSide: {
  382. name: 'imageStyleSide',
  383. title: 'Side image',
  384. icon: rightIcon,
  385. className: 'image-style-side'
  386. },
  387. imageStyleAlignLeft: {
  388. name: 'imageStyleAlignLeft',
  389. title: 'Left aligned image',
  390. icon: leftIcon,
  391. className: 'image-style-align-left'
  392. },
  393. imageStyleAlignCenter: {
  394. name: 'imageStyleAlignCenter',
  395. title: 'Centered image',
  396. icon: centerIcon,
  397. className: 'image-style-align-center'
  398. },
  399. imageStyleAlignRight: {
  400. name: 'imageStyleAlignRight',
  401. title: 'Right aligned image',
  402. icon: rightIcon,
  403. className: 'image-style-align-right'
  404. }
  405. } );
  406. } );
  407. } );
  408. describe( 'defaultIcons', () => {
  409. it( 'should be defined', () => {
  410. expect( ImageStyleEngine.defaultIcons ).to.deep.equal( {
  411. full: fullWidthIcon,
  412. left: leftIcon,
  413. right: rightIcon,
  414. center: centerIcon,
  415. } );
  416. } );
  417. } );
  418. } );