8
0

imagestyleengine.js 16 KB

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