8
0

imagestyleengine.js 17 KB

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