imagestyleengine.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /**
  2. * @license Copyright (c) 2003-2018, 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.addAttributeCheck( ( ctx, attributeName ) => {
  103. if ( ctx.endsWith( 'image' ) && attributeName == 'imageStyle' ) {
  104. return false;
  105. }
  106. } );
  107. editor.setData( '<figure class="image side-class"><img src="foo.png" /></figure>' );
  108. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<image src="foo.png"></image>' );
  109. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  110. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  111. );
  112. } );
  113. it( 'should convert model to view: adding attribute', () => {
  114. setModelData( model, '<image src="foo.png"></image>' );
  115. const image = document.getRoot().getChild( 0 );
  116. model.change( writer => {
  117. writer.setAttribute( 'imageStyle', 'sideStyle', image );
  118. } );
  119. expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
  120. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  121. '<figure class="ck-widget image side-class" contenteditable="false"><img src="foo.png"></img></figure>'
  122. );
  123. } );
  124. it( 'should convert model to view: removing attribute', () => {
  125. setModelData( model, '<image src="foo.png" imageStyle="sideStyle"></image>' );
  126. const image = document.getRoot().getChild( 0 );
  127. model.change( writer => {
  128. writer.setAttribute( 'imageStyle', null, image );
  129. } );
  130. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  131. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  132. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  133. );
  134. } );
  135. it( 'should convert model to view: change attribute', () => {
  136. setModelData( model, '<image src="foo.png" imageStyle="dummy"></image>' );
  137. const image = document.getRoot().getChild( 0 );
  138. model.change( writer => {
  139. writer.setAttribute( 'imageStyle', 'sideStyle', image );
  140. } );
  141. expect( editor.getData() ).to.equal( '<figure class="image side-class"><img src="foo.png"></figure>' );
  142. // https://github.com/ckeditor/ckeditor5-image/issues/132
  143. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  144. '<figure class="ck-widget image side-class" contenteditable="false"><img src="foo.png"></img></figure>'
  145. );
  146. model.change( writer => {
  147. writer.setAttribute( 'imageStyle', 'dummyStyle', image );
  148. } );
  149. expect( editor.getData() ).to.equal( '<figure class="image dummy-class"><img src="foo.png"></figure>' );
  150. // https://github.com/ckeditor/ckeditor5-image/issues/132
  151. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  152. '<figure class="ck-widget dummy-class image" contenteditable="false"><img src="foo.png"></img></figure>'
  153. );
  154. } );
  155. it( 'should not convert from model to view if already consumed: adding attribute', () => {
  156. editor.editing.modelToView.on( 'attribute:imageStyle', ( evt, data, consumable ) => {
  157. consumable.consume( data.item, 'attribute:imageStyle' );
  158. }, { priority: 'high' } );
  159. setModelData( model, '<image src="foo.png"></image>' );
  160. const image = document.getRoot().getChild( 0 );
  161. model.change( writer => {
  162. writer.setAttribute( 'imageStyle', 'sideStyle', image );
  163. } );
  164. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  165. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  166. );
  167. } );
  168. it( 'should not set attribute if change was already consumed', () => {
  169. editor.editing.modelToView.on( 'attribute:imageStyle', ( evt, data, consumable ) => {
  170. consumable.consume( data.item, 'attribute:imageStyle' );
  171. }, { priority: 'high' } );
  172. setModelData( model, '<image src="foo.png" imageStyle="dummyStyle"></image>' );
  173. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  174. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  175. );
  176. } );
  177. it( 'should not convert from model to view if style is not present: adding attribute', () => {
  178. setModelData( model, '<image src="foo.png"></image>' );
  179. const image = document.getRoot().getChild( 0 );
  180. model.change( writer => {
  181. writer.setAttribute( 'imageStyle', 'foo', image );
  182. } );
  183. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  184. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  185. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  186. );
  187. } );
  188. it( 'should not convert from model to view if style is not present: change attribute', () => {
  189. setModelData( model, '<image src="foo.png" imageStyle="dummy"></image>' );
  190. const image = document.getRoot().getChild( 0 );
  191. model.change( writer => {
  192. writer.setAttribute( 'imageStyle', 'foo', image );
  193. } );
  194. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  195. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  196. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  197. );
  198. } );
  199. it( 'should not convert from model to view if style is not present: remove attribute', () => {
  200. setModelData( model, '<image src="foo.png" imageStyle="foo"></image>' );
  201. const image = document.getRoot().getChild( 0 );
  202. model.change( writer => {
  203. writer.setAttribute( 'imageStyle', null, image );
  204. } );
  205. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  206. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  207. '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>'
  208. );
  209. } );
  210. } );
  211. describe( 'imageStyles()', () => {
  212. it( 'should fall back to defaults when no image.styles', () => {
  213. return VirtualTestEditor
  214. .create( {
  215. plugins: [ ImageStyleEngine ]
  216. } )
  217. .then( newEditor => {
  218. editor = newEditor;
  219. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'imageStyleFull', 'imageStyleSide' ] );
  220. } );
  221. } );
  222. it( 'should not alter the image.styles config', () => {
  223. return VirtualTestEditor
  224. .create( {
  225. plugins: [ ImageStyleEngine ],
  226. image: {
  227. styles: [
  228. 'imageStyleSide'
  229. ]
  230. }
  231. } )
  232. .then( newEditor => {
  233. editor = newEditor;
  234. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ 'imageStyleSide' ] );
  235. } );
  236. } );
  237. it( 'should not alter object definitions in the image.styles config', () => {
  238. return VirtualTestEditor
  239. .create( {
  240. plugins: [ ImageStyleEngine ],
  241. image: {
  242. styles: [
  243. { name: 'imageStyleSide' }
  244. ]
  245. }
  246. } )
  247. .then( newEditor => {
  248. editor = newEditor;
  249. expect( newEditor.config.get( 'image.styles' ) ).to.deep.equal( [ { name: 'imageStyleSide' } ] );
  250. } );
  251. } );
  252. it( 'should cache the styles', () => {
  253. return VirtualTestEditor
  254. .create( {
  255. plugins: [ ImageStyleEngine ]
  256. } )
  257. .then( newEditor => {
  258. editor = newEditor;
  259. plugin = editor.plugins.get( ImageStyleEngine );
  260. expect( plugin.imageStyles ).to.equal( plugin.imageStyles );
  261. } );
  262. } );
  263. describe( 'object format', () => {
  264. beforeEach( () => {
  265. class TranslationMock extends Plugin {
  266. init() {
  267. sinon.stub( this.editor, 't' ).returns( 'Default translation' );
  268. }
  269. }
  270. return VirtualTestEditor
  271. .create( {
  272. plugins: [ TranslationMock, ImageStyleEngine ],
  273. image: {
  274. styles: [
  275. // Custom user styles.
  276. { name: 'foo', title: 'foo', icon: 'custom', isDefault: true, className: 'foo-class' },
  277. { name: 'bar', title: 'bar', icon: 'right', className: 'bar-class' },
  278. { name: 'baz', title: 'Side image', icon: 'custom', className: 'baz-class' },
  279. // Customized default styles.
  280. { name: 'imageStyleFull', icon: 'left', title: 'Custom title' }
  281. ]
  282. }
  283. } )
  284. .then( newEditor => {
  285. editor = newEditor;
  286. plugin = editor.plugins.get( ImageStyleEngine );
  287. } );
  288. } );
  289. it( 'should pass through if #name not found in default styles', () => {
  290. expect( plugin.imageStyles[ 0 ] ).to.deep.equal( {
  291. name: 'foo',
  292. title: 'foo',
  293. icon: 'custom',
  294. isDefault: true,
  295. className: 'foo-class'
  296. } );
  297. } );
  298. it( 'should use one of default icons if #icon matches', () => {
  299. expect( plugin.imageStyles[ 1 ].icon ).to.equal( ImageStyleEngine.defaultIcons.right );
  300. } );
  301. it( 'should use one of default translations if #title matches', () => {
  302. expect( plugin.imageStyles[ 2 ].title ).to.deep.equal( 'Default translation' );
  303. } );
  304. it( 'should extend one of default styles if #name matches', () => {
  305. expect( plugin.imageStyles[ 3 ] ).to.deep.equal( {
  306. name: 'imageStyleFull',
  307. title: 'Custom title',
  308. icon: ImageStyleEngine.defaultIcons.left,
  309. isDefault: true
  310. } );
  311. } );
  312. } );
  313. describe( 'string format', () => {
  314. it( 'should use one of default styles if #name matches', () => {
  315. return VirtualTestEditor
  316. .create( {
  317. plugins: [ ImageStyleEngine ],
  318. image: {
  319. styles: [ 'imageStyleFull' ]
  320. }
  321. } )
  322. .then( newEditor => {
  323. editor = newEditor;
  324. plugin = editor.plugins.get( ImageStyleEngine );
  325. expect( plugin.imageStyles[ 0 ] ).to.deep.equal( ImageStyleEngine.defaultStyles.imageStyleFull );
  326. } );
  327. } );
  328. it( 'should warn if a #name not found in default styles', () => {
  329. testUtils.sinon.stub( log, 'warn' );
  330. return VirtualTestEditor
  331. .create( {
  332. plugins: [ ImageStyleEngine ],
  333. image: {
  334. styles: [ 'foo' ]
  335. }
  336. } )
  337. .then( newEditor => {
  338. editor = newEditor;
  339. plugin = editor.plugins.get( ImageStyleEngine );
  340. expect( plugin.imageStyles[ 0 ] ).to.deep.equal( {
  341. name: 'foo'
  342. } );
  343. sinon.assert.calledOnce( log.warn );
  344. sinon.assert.calledWithExactly( log.warn,
  345. sinon.match( /^image-style-not-found/ ),
  346. { name: 'foo' }
  347. );
  348. } );
  349. } );
  350. } );
  351. } );
  352. describe( 'localizedDefaultStylesTitles()', () => {
  353. it( 'should return localized titles of default styles', () => {
  354. return VirtualTestEditor
  355. .create( {
  356. plugins: [ ImageStyleEngine ]
  357. } )
  358. .then( newEditor => {
  359. editor = newEditor;
  360. plugin = editor.plugins.get( ImageStyleEngine );
  361. expect( plugin.localizedDefaultStylesTitles ).to.deep.equal( {
  362. 'Full size image': 'Full size image',
  363. 'Side image': 'Side image',
  364. 'Left aligned image': 'Left aligned image',
  365. 'Centered image': 'Centered image',
  366. 'Right aligned image': 'Right aligned image'
  367. } );
  368. } );
  369. } );
  370. } );
  371. describe( 'defaultStyles', () => {
  372. it( 'should be defined', () => {
  373. expect( ImageStyleEngine.defaultStyles ).to.deep.equal( {
  374. imageStyleFull: {
  375. name: 'imageStyleFull',
  376. title: 'Full size image',
  377. icon: fullWidthIcon,
  378. isDefault: true
  379. },
  380. imageStyleSide: {
  381. name: 'imageStyleSide',
  382. title: 'Side image',
  383. icon: rightIcon,
  384. className: 'image-style-side'
  385. },
  386. imageStyleAlignLeft: {
  387. name: 'imageStyleAlignLeft',
  388. title: 'Left aligned image',
  389. icon: leftIcon,
  390. className: 'image-style-align-left'
  391. },
  392. imageStyleAlignCenter: {
  393. name: 'imageStyleAlignCenter',
  394. title: 'Centered image',
  395. icon: centerIcon,
  396. className: 'image-style-align-center'
  397. },
  398. imageStyleAlignRight: {
  399. name: 'imageStyleAlignRight',
  400. title: 'Right aligned image',
  401. icon: rightIcon,
  402. className: 'image-style-align-right'
  403. }
  404. } );
  405. } );
  406. } );
  407. describe( 'defaultIcons', () => {
  408. it( 'should be defined', () => {
  409. expect( ImageStyleEngine.defaultIcons ).to.deep.equal( {
  410. full: fullWidthIcon,
  411. left: leftIcon,
  412. right: rightIcon,
  413. center: centerIcon,
  414. } );
  415. } );
  416. } );
  417. } );