imageengine.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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 ImageEngine from '../../src/image/imageengine';
  7. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  9. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  10. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  11. import { isImageWidget } from '../../src/image/utils';
  12. import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
  13. describe( 'ImageEngine', () => {
  14. let editor, document, viewDocument;
  15. beforeEach( () => {
  16. return VirtualTestEditor
  17. .create( {
  18. plugins: [ ImageEngine ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. document = editor.document;
  23. viewDocument = editor.editing.view;
  24. } );
  25. } );
  26. it( 'should be loaded', () => {
  27. expect( editor.plugins.get( ImageEngine ) ).to.be.instanceOf( ImageEngine );
  28. } );
  29. it( 'should set proper schema rules', () => {
  30. expect( document.schema.check( { name: 'image', attributes: [ 'src', 'alt' ], inside: '$root' } ) ).to.be.true;
  31. expect( document.schema.objects.has( 'image' ) ).to.be.true;
  32. } );
  33. describe( 'conversion in data pipeline', () => {
  34. describe( 'model to view', () => {
  35. it( 'should convert', () => {
  36. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  37. expect( editor.getData() ).to.equal( '<figure class="image"><img alt="alt text" src="foo.png"></figure>' );
  38. } );
  39. it( 'should convert without alt attribute', () => {
  40. setModelData( document, '<image src="foo.png"></image>' );
  41. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  42. } );
  43. it( 'should convert srcset attribute to srcset and sizes attribute', () => {
  44. setModelData( document,
  45. '<image src="foo.png" alt="alt text" srcset=\'{ "data": "small.png 148w, big.png 1024w" }\'></image>'
  46. );
  47. expect( normalizeHtml( editor.getData() ) ).to.equal(
  48. '<figure class="image">' +
  49. '<img alt="alt text" sizes="100vw" src="foo.png" srcset="small.png 148w, big.png 1024w"></img>' +
  50. '</figure>'
  51. );
  52. } );
  53. it( 'should convert srcset attribute to width, srcset and add sizes attribute', () => {
  54. setModelData( document,
  55. '<image ' +
  56. 'src="foo.png" ' +
  57. 'alt="alt text" ' +
  58. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  59. '</image>'
  60. );
  61. expect( normalizeHtml( editor.getData() ) ).to.equal(
  62. '<figure class="image">' +
  63. '<img alt="alt text" sizes="100vw" src="foo.png" srcset="small.png 148w, big.png 1024w" width="1024"></img>' +
  64. '</figure>'
  65. );
  66. } );
  67. it( 'should not convert srcset attribute if is already consumed', () => {
  68. editor.data.modelToView.on( 'addAttribute:srcset:image', ( evt, data, consumable ) => {
  69. const parts = evt.name.split( ':' );
  70. const consumableType = parts[ 0 ] + ':' + parts[ 1 ];
  71. const modelImage = data.item;
  72. consumable.consume( modelImage, consumableType );
  73. }, { priority: 'high' } );
  74. setModelData( document,
  75. '<image ' +
  76. 'src="foo.png" ' +
  77. 'alt="alt text" ' +
  78. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  79. '</image>'
  80. );
  81. expect( editor.getData() ).to.equal(
  82. '<figure class="image">' +
  83. '<img alt="alt text" src="foo.png">' +
  84. '</figure>'
  85. );
  86. } );
  87. it( 'should not convert srcset attribute if has wrong data', () => {
  88. setModelData( document,
  89. '<image ' +
  90. 'src="foo.png" ' +
  91. 'alt="alt text" ' +
  92. 'srcset=\'{ "foo":"bar" }\'>' +
  93. '</image>' );
  94. const image = document.getRoot().getChild( 0 );
  95. document.enqueueChanges( () => {
  96. const batch = document.batch();
  97. batch.removeAttribute( 'srcset', image );
  98. } );
  99. expect( editor.getData() ).to.equal(
  100. '<figure class="image">' +
  101. '<img alt="alt text" src="foo.png">' +
  102. '</figure>'
  103. );
  104. } );
  105. } );
  106. describe( 'view to model', () => {
  107. it( 'should convert image figure', () => {
  108. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  109. expect( getModelData( document, { withoutSelection: true } ) )
  110. .to.equal( '<image alt="alt text" src="foo.png"></image>' );
  111. } );
  112. it( 'should not convert if there is no image class', () => {
  113. editor.setData( '<figure class="quote">My quote</figure>' );
  114. expect( getModelData( document, { withoutSelection: true } ) )
  115. .to.equal( '' );
  116. } );
  117. it( 'should not convert if there is no img inside #1', () => {
  118. editor.setData( '<figure class="image"></figure>' );
  119. expect( getModelData( document, { withoutSelection: true } ) )
  120. .to.equal( '' );
  121. } );
  122. it( 'should not convert if there is no img inside #2', () => {
  123. editor.setData( '<figure class="image">test</figure>' );
  124. expect( getModelData( document, { withoutSelection: true } ) )
  125. .to.equal( '' );
  126. } );
  127. it( 'should convert without alt attribute', () => {
  128. editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
  129. expect( getModelData( document, { withoutSelection: true } ) )
  130. .to.equal( '<image src="foo.png"></image>' );
  131. } );
  132. it( 'should not convert without src attribute', () => {
  133. editor.setData( '<figure class="image"><img alt="alt text" /></figure>' );
  134. expect( getModelData( document, { withoutSelection: true } ) )
  135. .to.equal( '' );
  136. } );
  137. it( 'should not convert in wrong context', () => {
  138. const data = editor.data;
  139. const editing = editor.editing;
  140. document.schema.registerItem( 'div', '$block' );
  141. document.schema.disallow( { name: 'image', inside: '$root', attributes: 'src' } );
  142. buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  143. buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  144. editor.setData( '<div><figure class="image"><img src="foo.png" alt="alt text" /></figure></div>' );
  145. expect( getModelData( document, { withoutSelection: true } ) )
  146. .to.equal( '<div></div>' );
  147. } );
  148. it( 'should not convert if img is already consumed', () => {
  149. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  150. const img = data.input.getChild( 0 );
  151. consumable.consume( img, { name: true } );
  152. }, { priority: 'high' } );
  153. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  154. expect( getModelData( document, { withoutSelection: true } ) )
  155. .to.equal( '' );
  156. } );
  157. it( 'should not convert if figure is already consumed', () => {
  158. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  159. const figure = data.input;
  160. consumable.consume( figure, { name: true, class: 'image' } );
  161. }, { priority: 'high' } );
  162. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  163. expect( getModelData( document, { withoutSelection: true } ) )
  164. .to.equal( '' );
  165. } );
  166. it( 'should dispatch conversion for nested elements', () => {
  167. const conversionSpy = sinon.spy();
  168. editor.data.viewToModel.on( 'element:figcaption', conversionSpy );
  169. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /><figcaption></figcaption></figure>' );
  170. sinon.assert.calledOnce( conversionSpy );
  171. } );
  172. it( 'should convert bare img element', () => {
  173. editor.setData( '<img src="foo.png" alt="alt text" />' );
  174. expect( getModelData( document, { withoutSelection: true } ) )
  175. .to.equal( '<image alt="alt text" src="foo.png"></image>' );
  176. } );
  177. it( 'should not convert alt attribute on non-img element', () => {
  178. const data = editor.data;
  179. const editing = editor.editing;
  180. document.schema.registerItem( 'div', '$block' );
  181. document.schema.allow( { name: 'div', attributes: 'alt', inside: '$root' } );
  182. buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  183. buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  184. editor.setData( '<div alt="foo"></div>' );
  185. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<div></div>' );
  186. } );
  187. it( 'should handle figure with two images', () => {
  188. document.schema.allow( { name: '$text', inside: 'image' } );
  189. editor.setData( '<figure class="image"><img src="foo.jpg" /><img src="bar.jpg" />abc</figure>' );
  190. // The foo.jpg image is properly converted using figure converter. The other image was tried to
  191. // be added as a child of foo.jpg and then was autohoisted.
  192. expect( getModelData( document, { withoutSelection: true } ) )
  193. .to.equal( '<image src="bar.jpg"></image><image src="foo.jpg">abc</image>' );
  194. } );
  195. it( 'should convert image with srcset attribute', () => {
  196. editor.setData(
  197. '<figure class="image">' +
  198. '<img src="foo.png" alt="alt text" srcset="small.png 148w, big.png 1024w" />' +
  199. '</figure>'
  200. );
  201. expect( getModelData( document, { withoutSelection: true } ) )
  202. .to.equal( '<image alt="alt text" src="foo.png" srcset="{"data":"small.png 148w, big.png 1024w"}"></image>' );
  203. } );
  204. it( 'should convert image with srcset and width attributes', () => {
  205. editor.setData(
  206. '<figure class="image">' +
  207. '<img src="foo.png" alt="alt text" srcset="small.png 148w, big.png 1024w" width="1024" />' +
  208. '</figure>'
  209. );
  210. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  211. '<image alt="alt text" src="foo.png" srcset="{"data":"small.png 148w, big.png 1024w","width":"1024"}"></image>' );
  212. } );
  213. it( 'should ignore sizes attribute', () => {
  214. editor.setData(
  215. '<figure class="image">' +
  216. '<img src="foo.png" alt="alt text" srcset="small.png 148w, big.png 1024w" sizes="50vw" />' +
  217. '</figure>'
  218. );
  219. expect( getModelData( document, { withoutSelection: true } ) )
  220. .to.equal( '<image alt="alt text" src="foo.png" srcset="{"data":"small.png 148w, big.png 1024w"}"></image>' );
  221. } );
  222. describe( 'should autohoist images', () => {
  223. beforeEach( () => {
  224. document.schema.registerItem( 'div', '$block' );
  225. buildModelConverter()
  226. .for( editor.data.modelToView, editor.editing.modelToView )
  227. .fromElement( 'div' )
  228. .toElement( 'div' );
  229. buildViewConverter()
  230. .for( editor.data.viewToModel )
  231. .fromElement( 'div' )
  232. .toElement( 'div' );
  233. } );
  234. it( 'image between non-hoisted elements', () => {
  235. editor.setData( '<div>foo<img src="foo.jpg" alt="foo" />bar</div>' );
  236. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  237. '<div>foo</div>' +
  238. '<image alt="foo" src="foo.jpg"></image>' +
  239. '<div>bar</div>'
  240. );
  241. } );
  242. it( 'multiple images', () => {
  243. editor.setData( '<div>foo<img src="foo.jpg" alt="foo" />ba<img src="foo.jpg" alt="foo" />r</div>' );
  244. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  245. '<div>foo</div>' +
  246. '<image alt="foo" src="foo.jpg"></image>' +
  247. '<div>ba</div>' +
  248. '<image alt="foo" src="foo.jpg"></image>' +
  249. '<div>r</div>'
  250. );
  251. } );
  252. it( 'images on borders of parent', () => {
  253. editor.setData( '<div><img src="foo.jpg" alt="foo" />foobar<img src="foo.jpg" alt="foo" /></div>' );
  254. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  255. '<image alt="foo" src="foo.jpg"></image>' +
  256. '<div>foobar</div>' +
  257. '<image alt="foo" src="foo.jpg"></image>'
  258. );
  259. } );
  260. it( 'images are only content of parent', () => {
  261. editor.setData( '<div><img src="foo.jpg" alt="foo" /><img src="foo.jpg" alt="foo" /></div>' );
  262. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  263. '<image alt="foo" src="foo.jpg"></image>' +
  264. '<image alt="foo" src="foo.jpg"></image>'
  265. );
  266. } );
  267. it( 'deep autohoisting #1', () => {
  268. document.schema.allow( { name: 'div', inside: 'div' } );
  269. editor.setData( '<div>foo<div>xx<img src="foo.jpg" alt="foo" /></div>bar</div>' );
  270. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  271. '<div>' +
  272. 'foo' +
  273. '<div>' +
  274. 'xx' +
  275. '</div>' +
  276. '</div>' +
  277. '<image alt="foo" src="foo.jpg"></image>' +
  278. '<div>bar</div>'
  279. );
  280. } );
  281. it( 'deep autohoisting #2', () => {
  282. document.schema.allow( { name: 'div', inside: 'div' } );
  283. editor.setData(
  284. '<div>x</div>' +
  285. '<div><div><div><img src="foo.jpg" alt="foo" /></div></div></div>' +
  286. '<div>y</div>'
  287. );
  288. expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
  289. '<div>x</div><image alt="foo" src="foo.jpg"></image><div>y</div>'
  290. );
  291. } );
  292. it( 'should not break a limiting element', () => {
  293. document.schema.registerItem( 'limit', '$block' );
  294. document.schema.allow( { name: 'div', inside: 'limit' } );
  295. document.schema.limits.add( 'limit' );
  296. buildModelConverter()
  297. .for( editor.data.modelToView, editor.editing.modelToView ).fromElement( 'limit' ).toElement( 'limit' );
  298. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'limit' ).toElement( 'limit' );
  299. editor.setData( '<limit><div>foo<img src="foo.jpg" alt="foo" />bar</div></limit>' );
  300. // <limit> element does not have converters so it is not converted.
  301. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<limit><div>foobar</div></limit>' );
  302. } );
  303. it( 'should not convert and autohoist image element without src attribute (which is not allowed by schema)', () => {
  304. editor.setData( '<div>foo<img alt="foo" />bar</div>' );
  305. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( '<div>foobar</div>' );
  306. } );
  307. } );
  308. } );
  309. } );
  310. describe( 'conversion in editing pipeline', () => {
  311. describe( 'model to view', () => {
  312. it( 'should convert', () => {
  313. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  314. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  315. '<figure class="ck-widget image" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>'
  316. );
  317. } );
  318. it( 'converted element should be widgetized', () => {
  319. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  320. const figure = viewDocument.getRoot().getChild( 0 );
  321. expect( figure.name ).to.equal( 'figure' );
  322. expect( isImageWidget( figure ) ).to.be.true;
  323. } );
  324. it( 'should convert attribute change', () => {
  325. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  326. const image = document.getRoot().getChild( 0 );
  327. document.enqueueChanges( () => {
  328. const batch = document.batch();
  329. batch.setAttribute( 'alt', 'new text', image );
  330. } );
  331. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  332. '<figure class="ck-widget image" contenteditable="false"><img alt="new text" src="foo.png"></img></figure>'
  333. );
  334. } );
  335. it( 'should convert attribute removal', () => {
  336. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  337. const image = document.getRoot().getChild( 0 );
  338. document.enqueueChanges( () => {
  339. const batch = document.batch();
  340. batch.removeAttribute( 'alt', image );
  341. } );
  342. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  343. .to.equal( '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>' );
  344. } );
  345. it( 'should not convert change if is already consumed', () => {
  346. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  347. const image = document.getRoot().getChild( 0 );
  348. editor.editing.modelToView.on( 'removeAttribute:alt:image', ( evt, data, consumable ) => {
  349. consumable.consume( data.item, 'removeAttribute:alt' );
  350. }, { priority: 'high' } );
  351. document.enqueueChanges( () => {
  352. const batch = document.batch();
  353. batch.removeAttribute( 'alt', image );
  354. } );
  355. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  356. '<figure class="ck-widget image" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>'
  357. );
  358. } );
  359. it( 'should convert srcset attribute to srcset and sizes', () => {
  360. setModelData( document,
  361. '<image ' +
  362. 'src="foo.png" ' +
  363. 'alt="alt text" ' +
  364. 'srcset=\'{ "data":"small.png 148w, big.png 1024w" }\'>' +
  365. '</image>' );
  366. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  367. '<figure class="ck-widget image" contenteditable="false">' +
  368. '<img alt="alt text" sizes="100vw" src="foo.png" srcset="small.png 148w, big.png 1024w"></img>' +
  369. '</figure>'
  370. );
  371. } );
  372. it( 'should not convert srcset attribute if has wrong data', () => {
  373. setModelData( document,
  374. '<image ' +
  375. 'src="foo.png" ' +
  376. 'alt="alt text" ' +
  377. 'srcset=\'{ "foo":"bar" }\'>' +
  378. '</image>' );
  379. const image = document.getRoot().getChild( 0 );
  380. document.enqueueChanges( () => {
  381. const batch = document.batch();
  382. batch.removeAttribute( 'srcset', image );
  383. } );
  384. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  385. '<figure class="ck-widget image" contenteditable="false">' +
  386. '<img alt="alt text" src="foo.png"></img>' +
  387. '</figure>'
  388. );
  389. } );
  390. it( 'should convert srcset attribute to srcset, width and sizes', () => {
  391. setModelData( document,
  392. '<image ' +
  393. 'src="foo.png" ' +
  394. 'alt="alt text" ' +
  395. 'srcset=\'{ "data":"small.png 148w, big.png 1024w", "width":"1024" }\'>' +
  396. '</image>' );
  397. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  398. '<figure class="ck-widget image" contenteditable="false">' +
  399. '<img alt="alt text" sizes="100vw" src="foo.png" srcset="small.png 148w, big.png 1024w" width="1024"></img>' +
  400. '</figure>'
  401. );
  402. } );
  403. it( 'should remove sizes and srcsset attribute when srcset attribute is removed from model', () => {
  404. setModelData( document, '<image src="foo.png" srcset=\'{ "data": "small.png 148w, big.png 1024w" }\'></image>' );
  405. const image = document.getRoot().getChild( 0 );
  406. document.enqueueChanges( () => {
  407. const batch = document.batch();
  408. batch.removeAttribute( 'srcset', image );
  409. } );
  410. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  411. '<figure class="ck-widget image" contenteditable="false">' +
  412. '<img src="foo.png"></img>' +
  413. '</figure>'
  414. );
  415. } );
  416. it( 'should remove width, sizes and srcsset attribute when srcset attribute is removed from model', () => {
  417. setModelData( document,
  418. '<image ' +
  419. 'src="foo.png" ' +
  420. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  421. '</image>'
  422. );
  423. const image = document.getRoot().getChild( 0 );
  424. document.enqueueChanges( () => {
  425. const batch = document.batch();
  426. batch.removeAttribute( 'srcset', image );
  427. } );
  428. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  429. '<figure class="ck-widget image" contenteditable="false">' +
  430. '<img src="foo.png"></img>' +
  431. '</figure>'
  432. );
  433. } );
  434. it( 'should not convert srcset attribute if is already consumed', () => {
  435. editor.editing.modelToView.on( 'addAttribute:srcset:image', ( evt, data, consumable ) => {
  436. const parts = evt.name.split( ':' );
  437. const consumableType = parts[ 0 ] + ':' + parts[ 1 ];
  438. const modelImage = data.item;
  439. consumable.consume( modelImage, consumableType );
  440. }, { priority: 'high' } );
  441. setModelData( document,
  442. '<image ' +
  443. 'src="foo.png" ' +
  444. 'alt="alt text" ' +
  445. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  446. '</image>'
  447. );
  448. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  449. '<figure class="ck-widget image" contenteditable="false">' +
  450. '<img alt="alt text" src="foo.png"></img>' +
  451. '</figure>'
  452. );
  453. } );
  454. } );
  455. } );
  456. } );