imageediting.js 21 KB

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