imagestyleengine.js 17 KB

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