8
0

imageediting.js 21 KB

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