imageediting.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document, Event */
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  8. import ImageEditing from '../../src/image/imageediting';
  9. import ImageLoadObserver from '../../src/image/imageloadobserver';
  10. import ImageInsertCommand from '../../src/image/imageinsertcommand';
  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 { isImageWidget } from '../../src/image/utils';
  14. import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
  15. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. describe( 'ImageEditing', () => {
  17. let editor, model, doc, view, viewDocument;
  18. testUtils.createSinonSandbox();
  19. beforeEach( () => {
  20. return VirtualTestEditor
  21. .create( {
  22. plugins: [ ImageEditing ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. model = editor.model;
  27. doc = model.document;
  28. view = editor.editing.view;
  29. viewDocument = view.document;
  30. } );
  31. } );
  32. it( 'should have pluginName', () => {
  33. expect( ImageEditing.pluginName ).to.equal( 'ImageEditing' );
  34. } );
  35. it( 'should be loaded', () => {
  36. expect( editor.plugins.get( ImageEditing ) ).to.be.instanceOf( ImageEditing );
  37. } );
  38. it( 'should set proper schema rules', () => {
  39. expect( model.schema.checkChild( [ '$root' ], 'image' ) ).to.be.true;
  40. expect( model.schema.checkAttribute( [ '$root', 'image' ], 'src' ) ).to.be.true;
  41. expect( model.schema.checkAttribute( [ '$root', 'image' ], 'alt' ) ).to.be.true;
  42. expect( model.schema.isObject( 'image' ) ).to.be.true;
  43. expect( model.schema.checkChild( [ '$root', 'image' ], 'image' ) ).to.be.false;
  44. expect( model.schema.checkChild( [ '$root', 'image' ], '$text' ) ).to.be.false;
  45. expect( model.schema.checkChild( [ '$root', '$block' ], 'image' ) ).to.be.false;
  46. } );
  47. it( 'should register ImageLoadObserver', () => {
  48. expect( view.getObserver( ImageLoadObserver ) ).to.be.instanceOf( ImageLoadObserver );
  49. } );
  50. it( 'should register imageInsert command', () => {
  51. expect( editor.commands.get( 'imageInsert' ) ).to.be.instanceOf( ImageInsertCommand );
  52. } );
  53. // See https://github.com/ckeditor/ckeditor5-image/issues/142.
  54. it( 'should update the ui after image has been loaded in the DOM', () => {
  55. const element = document.createElement( 'div' );
  56. document.body.appendChild( element );
  57. return ClassicTestEditor.create( element, {
  58. plugins: [ ImageEditing ]
  59. } ).then( editor => {
  60. editor.data.set( '<figure class="image"><img src="/assets/sample.png" alt="bar" /></figure>' );
  61. const spy = sinon.spy();
  62. editor.ui.on( 'update', spy );
  63. const htmlImageElement = editor.ui.getEditableElement().querySelector( 'img' );
  64. htmlImageElement.dispatchEvent( new Event( 'load' ) );
  65. sinon.assert.calledOnce( spy );
  66. return editor.destroy().then( () => element.remove() );
  67. } );
  68. } );
  69. describe( 'conversion in data pipeline', () => {
  70. describe( 'model to view', () => {
  71. it( 'should convert', () => {
  72. setModelData( model, '<image src="/assets/sample.png" alt="alt text"></image>' );
  73. expect( editor.getData() ).to.equal( '<figure class="image"><img alt="alt text" src="/assets/sample.png"></figure>' );
  74. } );
  75. it( 'should convert without alt attribute', () => {
  76. setModelData( model, '<image src="/assets/sample.png"></image>' );
  77. expect( editor.getData() ).to.equal( '<figure class="image"><img src="/assets/sample.png"></figure>' );
  78. } );
  79. it( 'should convert srcset attribute to srcset and sizes attribute', () => {
  80. setModelData( model,
  81. '<image src="/assets/sample.png" alt="alt text" srcset=\'{ "data": "small.png 148w, big.png 1024w" }\'></image>'
  82. );
  83. expect( normalizeHtml( editor.getData() ) ).to.equal(
  84. '<figure class="image">' +
  85. '<img alt="alt text" sizes="100vw" src="/assets/sample.png" srcset="small.png 148w, big.png 1024w"></img>' +
  86. '</figure>'
  87. );
  88. } );
  89. it( 'should convert srcset attribute to width, srcset and add sizes attribute', () => {
  90. setModelData( model,
  91. '<image ' +
  92. 'src="/assets/sample.png" ' +
  93. 'alt="alt text" ' +
  94. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  95. '</image>'
  96. );
  97. /* eslint-disable max-len */
  98. expect( normalizeHtml( editor.getData() ) ).to.equal(
  99. '<figure class="image">' +
  100. '<img alt="alt text" sizes="100vw" src="/assets/sample.png" srcset="small.png 148w, big.png 1024w" width="1024"></img>' +
  101. '</figure>'
  102. );
  103. /* eslint-enable max-len */
  104. } );
  105. it( 'should not convert srcset attribute if is already consumed', () => {
  106. editor.data.downcastDispatcher.on( 'attribute:srcset:image', ( evt, data, conversionApi ) => {
  107. const modelImage = data.item;
  108. conversionApi.consumable.consume( modelImage, evt.name );
  109. }, { priority: 'high' } );
  110. setModelData( model,
  111. '<image ' +
  112. 'src="/assets/sample.png" ' +
  113. 'alt="alt text" ' +
  114. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  115. '</image>'
  116. );
  117. expect( editor.getData() ).to.equal(
  118. '<figure class="image">' +
  119. '<img alt="alt text" src="/assets/sample.png">' +
  120. '</figure>'
  121. );
  122. } );
  123. it( 'should not convert srcset attribute if has wrong data', () => {
  124. setModelData( model,
  125. '<image ' +
  126. 'src="/assets/sample.png" ' +
  127. 'alt="alt text" ' +
  128. 'srcset=\'{ "foo":"bar" }\'>' +
  129. '</image>' );
  130. const image = doc.getRoot().getChild( 0 );
  131. model.change( writer => {
  132. writer.removeAttribute( 'srcset', image );
  133. } );
  134. expect( editor.getData() ).to.equal(
  135. '<figure class="image">' +
  136. '<img alt="alt text" src="/assets/sample.png">' +
  137. '</figure>'
  138. );
  139. } );
  140. } );
  141. describe( 'view to model', () => {
  142. it( 'should convert image figure', () => {
  143. editor.setData( '<figure class="image"><img src="/assets/sample.png" alt="alt text" /></figure>' );
  144. expect( getModelData( model, { withoutSelection: true } ) )
  145. .to.equal( '<image alt="alt text" src="/assets/sample.png"></image>' );
  146. } );
  147. it( 'should not convert if there is no image class', () => {
  148. editor.setData( '<figure class="quote">My quote</figure>' );
  149. expect( getModelData( model, { withoutSelection: true } ) )
  150. .to.equal( '' );
  151. } );
  152. it( 'should not convert if there is no img inside #1', () => {
  153. editor.setData( '<figure class="image"></figure>' );
  154. expect( getModelData( model, { withoutSelection: true } ) )
  155. .to.equal( '' );
  156. } );
  157. it( 'should not convert if there is no img inside #2', () => {
  158. editor.setData( '<figure class="image">test</figure>' );
  159. expect( getModelData( model, { withoutSelection: true } ) )
  160. .to.equal( '' );
  161. } );
  162. it( 'should convert without alt attribute', () => {
  163. editor.setData( '<figure class="image"><img src="/assets/sample.png" /></figure>' );
  164. expect( getModelData( model, { withoutSelection: true } ) )
  165. .to.equal( '<image src="/assets/sample.png"></image>' );
  166. } );
  167. it( 'should not convert without src attribute', () => {
  168. editor.setData( '<figure class="image"><img alt="alt text" /></figure>' );
  169. expect( getModelData( model, { withoutSelection: true } ) )
  170. .to.equal( '' );
  171. } );
  172. it( 'should not convert in wrong context', () => {
  173. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  174. model.schema.addChildCheck( ( ctx, childDef ) => {
  175. if ( ctx.endsWith( '$root' ) && childDef.name == 'image' ) {
  176. return false;
  177. }
  178. } );
  179. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  180. editor.setData( '<div><figure class="image"><img src="/assets/sample.png" alt="alt text" /></figure></div>' );
  181. expect( getModelData( model, { withoutSelection: true } ) )
  182. .to.equal( '<div></div>' );
  183. } );
  184. it( 'should not convert if img is already consumed', () => {
  185. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  186. const img = data.viewItem.getChild( 0 );
  187. conversionApi.consumable.consume( img, { name: true } );
  188. }, { priority: 'high' } );
  189. editor.setData( '<figure class="image"><img src="/assets/sample.png" alt="alt text" /></figure>' );
  190. expect( getModelData( model, { withoutSelection: true } ) )
  191. .to.equal( '' );
  192. } );
  193. it( 'should not convert if figure is already consumed', () => {
  194. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  195. conversionApi.consumable.consume( data.viewItem, { name: true, class: 'image' } );
  196. }, { priority: 'high' } );
  197. editor.setData( '<figure class="image"><img src="/assets/sample.png" alt="alt text" /></figure>' );
  198. expect( getModelData( model, { withoutSelection: true } ) )
  199. .to.equal( '' );
  200. } );
  201. it( 'should dispatch conversion for nested elements', () => {
  202. const conversionSpy = sinon.spy();
  203. editor.data.upcastDispatcher.on( 'element:figcaption', conversionSpy );
  204. editor.setData( '<figure class="image"><img src="/assets/sample.png" alt="alt text" /><figcaption></figcaption></figure>' );
  205. sinon.assert.calledOnce( conversionSpy );
  206. } );
  207. it( 'should convert bare img element', () => {
  208. editor.setData( '<img src="/assets/sample.png" alt="alt text" />' );
  209. expect( getModelData( model, { withoutSelection: true } ) )
  210. .to.equal( '<image alt="alt text" src="/assets/sample.png"></image>' );
  211. } );
  212. it( 'should not convert alt attribute on non-img element', () => {
  213. model.schema.register( 'div', {
  214. inheritAllFrom: '$block',
  215. allowAttributes: 'alt'
  216. } );
  217. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  218. editor.setData( '<div alt="foo"></div>' );
  219. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<div></div>' );
  220. } );
  221. it( 'should convert image with srcset attribute', () => {
  222. editor.setData(
  223. '<figure class="image">' +
  224. '<img src="/assets/sample.png" alt="alt text" srcset="small.png 148w, big.png 1024w" />' +
  225. '</figure>'
  226. );
  227. expect( getModelData( model, { withoutSelection: true } ) )
  228. .to.equal(
  229. '<image alt="alt text" src="/assets/sample.png" srcset="{"data":"small.png 148w, big.png 1024w"}"></image>'
  230. );
  231. } );
  232. it( 'should convert image with srcset and width attributes', () => {
  233. editor.setData(
  234. '<figure class="image">' +
  235. '<img src="/assets/sample.png" alt="alt text" srcset="small.png 148w, big.png 1024w" width="1024" />' +
  236. '</figure>'
  237. );
  238. /* eslint-disable max-len */
  239. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  240. '<image alt="alt text" src="/assets/sample.png" srcset="{"data":"small.png 148w, big.png 1024w","width":"1024"}"></image>'
  241. );
  242. /* eslint-enable max-len */
  243. } );
  244. it( 'should ignore sizes attribute', () => {
  245. editor.setData(
  246. '<figure class="image">' +
  247. '<img src="/assets/sample.png" alt="alt text" srcset="small.png 148w, big.png 1024w" sizes="50vw" />' +
  248. '</figure>'
  249. );
  250. expect( getModelData( model, { withoutSelection: true } ) )
  251. .to.equal(
  252. '<image alt="alt text" src="/assets/sample.png" srcset="{"data":"small.png 148w, big.png 1024w"}"></image>'
  253. );
  254. } );
  255. describe( 'should autohoist images', () => {
  256. beforeEach( () => {
  257. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  258. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  259. } );
  260. it( 'image between non-hoisted elements', () => {
  261. editor.setData( '<div>foo<img src="foo.jpg" alt="foo" />bar</div>' );
  262. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  263. '<div>foo</div>' +
  264. '<image alt="foo" src="foo.jpg"></image>' +
  265. '<div>bar</div>'
  266. );
  267. } );
  268. it( 'multiple images', () => {
  269. editor.setData( '<div>foo<img src="foo.jpg" alt="foo" />ba<img src="foo.jpg" alt="foo" />r</div>' );
  270. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  271. '<div>foo</div>' +
  272. '<image alt="foo" src="foo.jpg"></image>' +
  273. '<div>ba</div>' +
  274. '<image alt="foo" src="foo.jpg"></image>' +
  275. '<div>r</div>'
  276. );
  277. } );
  278. it( 'images on borders of parent', () => {
  279. editor.setData( '<div><img src="foo.jpg" alt="foo" />foobar<img src="foo.jpg" alt="foo" /></div>' );
  280. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  281. '<image alt="foo" src="foo.jpg"></image>' +
  282. '<div>foobar</div>' +
  283. '<image alt="foo" src="foo.jpg"></image>'
  284. );
  285. } );
  286. it( 'images are only content of parent', () => {
  287. editor.setData( '<div><img src="foo.jpg" alt="foo" /><img src="foo.jpg" alt="foo" /></div>' );
  288. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  289. '<image alt="foo" src="foo.jpg"></image>' +
  290. '<image alt="foo" src="foo.jpg"></image>'
  291. );
  292. } );
  293. it( 'deep autohoisting #1', () => {
  294. model.schema.extend( 'div', { allowIn: 'div' } );
  295. editor.setData( '<div>foo<div>xx<img src="foo.jpg" alt="foo" /></div>bar</div>' );
  296. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  297. '<div>' +
  298. 'foo' +
  299. '<div>' +
  300. 'xx' +
  301. '</div>' +
  302. '</div>' +
  303. '<image alt="foo" src="foo.jpg"></image>' +
  304. '<div>bar</div>'
  305. );
  306. } );
  307. it( 'deep autohoisting #2', () => {
  308. model.schema.extend( 'div', { allowIn: 'div' } );
  309. editor.setData(
  310. '<div>x</div>' +
  311. '<div><div><div><img src="foo.jpg" alt="foo" /></div></div></div>' +
  312. '<div>y</div>'
  313. );
  314. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  315. '<div>x</div><image alt="foo" src="foo.jpg"></image><div>y</div>'
  316. );
  317. } );
  318. it( 'should not break a limiting element', () => {
  319. model.schema.register( 'limit', {
  320. inheritAllFrom: '$block',
  321. isLimit: true
  322. } );
  323. model.schema.extend( 'div', { allowIn: 'limit' } );
  324. editor.conversion.elementToElement( { model: 'limit', view: 'limit' } );
  325. editor.setData( '<limit><div>foo<img src="foo.jpg" alt="foo" />bar</div></limit>' );
  326. // <limit> element does not have converters so it is not converted.
  327. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<limit><div>foobar</div></limit>' );
  328. } );
  329. it( 'should not convert and autohoist image element without src attribute (which is not allowed by schema)', () => {
  330. editor.setData( '<div>foo<img alt="foo" />bar</div>' );
  331. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<div>foobar</div>' );
  332. } );
  333. } );
  334. } );
  335. } );
  336. describe( 'conversion in editing pipeline', () => {
  337. describe( 'model to view', () => {
  338. it( 'should convert', () => {
  339. setModelData( model, '<image src="/assets/sample.png" alt="alt text"></image>' );
  340. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  341. '<figure class="ck-widget image" contenteditable="false"><img alt="alt text" src="/assets/sample.png"></img></figure>'
  342. );
  343. } );
  344. it( 'converted element should be widgetized', () => {
  345. setModelData( model, '<image src="/assets/sample.png" alt="alt text"></image>' );
  346. const figure = viewDocument.getRoot().getChild( 0 );
  347. expect( figure.name ).to.equal( 'figure' );
  348. expect( isImageWidget( figure ) ).to.be.true;
  349. } );
  350. it( 'should convert attribute change', () => {
  351. setModelData( model, '<image src="/assets/sample.png" alt="alt text"></image>' );
  352. const image = doc.getRoot().getChild( 0 );
  353. model.change( writer => {
  354. writer.setAttribute( 'alt', 'new text', image );
  355. } );
  356. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  357. '<figure class="ck-widget image" contenteditable="false"><img alt="new text" src="/assets/sample.png"></img></figure>'
  358. );
  359. } );
  360. it( 'should convert attribute removal (but keeps an empty "alt" to the data)', () => {
  361. setModelData( model, '<image src="/assets/sample.png" alt="alt text"></image>' );
  362. const image = doc.getRoot().getChild( 0 );
  363. model.change( writer => {
  364. writer.removeAttribute( 'alt', image );
  365. } );
  366. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  367. '<figure class="ck-widget image" contenteditable="false"><img alt="" src="/assets/sample.png"></img></figure>'
  368. );
  369. } );
  370. it( 'should not convert change if is already consumed', () => {
  371. setModelData( model, '<image src="/assets/sample.png" alt="alt text"></image>' );
  372. const image = doc.getRoot().getChild( 0 );
  373. editor.editing.downcastDispatcher.on( 'attribute:alt:image', ( evt, data, conversionApi ) => {
  374. conversionApi.consumable.consume( data.item, 'attribute:alt' );
  375. }, { priority: 'high' } );
  376. model.change( writer => {
  377. writer.removeAttribute( 'alt', image );
  378. } );
  379. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  380. '<figure class="ck-widget image" contenteditable="false"><img alt="alt text" src="/assets/sample.png"></img></figure>'
  381. );
  382. } );
  383. it( 'should convert srcset attribute to srcset and sizes', () => {
  384. setModelData( model,
  385. '<image ' +
  386. 'src="/assets/sample.png" ' +
  387. 'alt="alt text" ' +
  388. 'srcset=\'{ "data":"small.png 148w, big.png 1024w" }\'>' +
  389. '</image>' );
  390. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  391. '<figure class="ck-widget image" contenteditable="false">' +
  392. '<img alt="alt text" sizes="100vw" src="/assets/sample.png" srcset="small.png 148w, big.png 1024w"></img>' +
  393. '</figure>'
  394. );
  395. } );
  396. it( 'should not convert srcset attribute if has wrong data', () => {
  397. setModelData( model,
  398. '<image ' +
  399. 'src="/assets/sample.png" ' +
  400. 'alt="alt text" ' +
  401. 'srcset=\'{ "foo":"bar" }\'>' +
  402. '</image>' );
  403. const image = doc.getRoot().getChild( 0 );
  404. model.change( writer => {
  405. writer.removeAttribute( 'srcset', image );
  406. } );
  407. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  408. '<figure class="ck-widget image" contenteditable="false">' +
  409. '<img alt="alt text" src="/assets/sample.png"></img>' +
  410. '</figure>'
  411. );
  412. } );
  413. it( 'should convert srcset attribute to srcset, width and sizes', () => {
  414. setModelData( model,
  415. '<image ' +
  416. 'src="/assets/sample.png" ' +
  417. 'alt="alt text" ' +
  418. 'srcset=\'{ "data":"small.png 148w, big.png 1024w", "width":"1024" }\'>' +
  419. '</image>' );
  420. /* eslint-disable max-len */
  421. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  422. '<figure class="ck-widget image" contenteditable="false">' +
  423. '<img alt="alt text" sizes="100vw" src="/assets/sample.png" srcset="small.png 148w, big.png 1024w" width="1024"></img>' +
  424. '</figure>'
  425. );
  426. /* eslint-enable max-len */
  427. } );
  428. it( 'should remove sizes and srcsset attribute when srcset attribute is removed from model', () => {
  429. setModelData( model, '<image src="/assets/sample.png" srcset=\'{ "data": "small.png 148w, big.png 1024w" }\'></image>' );
  430. const image = doc.getRoot().getChild( 0 );
  431. model.change( writer => {
  432. writer.removeAttribute( 'srcset', image );
  433. } );
  434. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  435. '<figure class="ck-widget image" contenteditable="false">' +
  436. '<img src="/assets/sample.png"></img>' +
  437. '</figure>'
  438. );
  439. } );
  440. it( 'should remove width, sizes and srcsset attribute when srcset attribute is removed from model', () => {
  441. setModelData( model,
  442. '<image ' +
  443. 'src="/assets/sample.png" ' +
  444. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  445. '</image>'
  446. );
  447. const image = doc.getRoot().getChild( 0 );
  448. model.change( writer => {
  449. writer.removeAttribute( 'srcset', image );
  450. } );
  451. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  452. '<figure class="ck-widget image" contenteditable="false">' +
  453. '<img src="/assets/sample.png"></img>' +
  454. '</figure>'
  455. );
  456. } );
  457. it( 'should not convert srcset attribute if is already consumed', () => {
  458. editor.editing.downcastDispatcher.on( 'attribute:srcset:image', ( evt, data, conversionApi ) => {
  459. const modelImage = data.item;
  460. conversionApi.consumable.consume( modelImage, evt.name );
  461. }, { priority: 'high' } );
  462. setModelData( model,
  463. '<image ' +
  464. 'src="/assets/sample.png" ' +
  465. 'alt="alt text" ' +
  466. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  467. '</image>'
  468. );
  469. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  470. '<figure class="ck-widget image" contenteditable="false">' +
  471. '<img alt="alt text" src="/assets/sample.png"></img>' +
  472. '</figure>'
  473. );
  474. } );
  475. } );
  476. } );
  477. } );