imagestyleengine.js 17 KB

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