8
0

imagestyleengine.js 17 KB

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