imageengine.js 20 KB

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