imageengine.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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, model, document, viewDocument;
  14. beforeEach( () => {
  15. return VirtualTestEditor
  16. .create( {
  17. plugins: [ ImageEngine ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. document = model.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( model.schema.check( { name: 'image', attributes: [ 'src', 'alt' ], inside: '$root' } ) ).to.be.true;
  31. expect( model.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( model, '<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( model, '<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( model,
  45. '<image src="foo.png" alt="alt text" srcset=\'{ "data": "small.png 148w, big.png 1024w" }\'></image>'
  46. );
  47. expect( editor.getData() ).to.equal(
  48. '<figure class="image">' +
  49. '<img srcset="small.png 148w, big.png 1024w" sizes="100vw" alt="alt text" src="foo.png">' +
  50. '</figure>'
  51. );
  52. } );
  53. it( 'should convert srcset attribute to width, srcset and add sizes attribute', () => {
  54. setModelData( model,
  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( editor.getData() ).to.equal(
  62. '<figure class="image">' +
  63. '<img srcset="small.png 148w, big.png 1024w" sizes="100vw" width="1024" alt="alt text" src="foo.png">' +
  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( model,
  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( model,
  89. '<image ' +
  90. 'src="foo.png" ' +
  91. 'alt="alt text" ' +
  92. 'srcset=\'{ "foo":"bar" }\'>' +
  93. '</image>' );
  94. const image = document.getRoot().getChild( 0 );
  95. model.change( writer => {
  96. writer.removeAttribute( 'srcset', image );
  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( model, { 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( model, { 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( model, { 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( model, { 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( model, { 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( model, { 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. model.schema.registerItem( 'div', '$block' );
  140. model.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( model, { 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( model, { 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( model, { 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( model, { 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. model.schema.registerItem( 'div', '$block' );
  180. model.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( model, { withoutSelection: true } ) ).to.equal( '<div></div>' );
  185. } );
  186. it( 'should handle figure with two images', () => {
  187. model.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( model, { 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( model, { 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( model, { 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( model, { 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. model.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( model, { 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( model, { 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( model, { 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( model, { 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. model.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( model, { 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. model.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( model, { 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. model.schema.registerItem( 'limit', '$block' );
  293. model.schema.allow( { name: 'div', inside: 'limit' } );
  294. model.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( model, { 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( model, { 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( model, '<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( model, '<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( model, '<image src="foo.png" alt="alt text"></image>' );
  325. const image = document.getRoot().getChild( 0 );
  326. model.change( writer => {
  327. writer.setAttribute( 'alt', 'new text', image );
  328. } );
  329. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  330. '<figure class="ck-widget image" contenteditable="false"><img alt="new text" src="foo.png"></img></figure>'
  331. );
  332. } );
  333. it( 'should convert attribute removal', () => {
  334. setModelData( model, '<image src="foo.png" alt="alt text"></image>' );
  335. const image = document.getRoot().getChild( 0 );
  336. model.change( writer => {
  337. writer.removeAttribute( 'alt', image );
  338. } );
  339. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  340. .to.equal( '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>' );
  341. } );
  342. it( 'should not convert change if is already consumed', () => {
  343. setModelData( model, '<image src="foo.png" alt="alt text"></image>' );
  344. const image = document.getRoot().getChild( 0 );
  345. editor.editing.modelToView.on( 'removeAttribute:alt:image', ( evt, data, consumable ) => {
  346. consumable.consume( data.item, 'removeAttribute:alt' );
  347. }, { priority: 'high' } );
  348. model.change( writer => {
  349. writer.removeAttribute( 'alt', image );
  350. } );
  351. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  352. '<figure class="ck-widget image" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>'
  353. );
  354. } );
  355. it( 'should convert srcset attribute to srcset and sizes', () => {
  356. setModelData( model,
  357. '<image ' +
  358. 'src="foo.png" ' +
  359. 'alt="alt text" ' +
  360. 'srcset=\'{ "data":"small.png 148w, big.png 1024w" }\'>' +
  361. '</image>' );
  362. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  363. '<figure class="ck-widget image" contenteditable="false">' +
  364. '<img alt="alt text" sizes="100vw" src="foo.png" srcset="small.png 148w, big.png 1024w"></img>' +
  365. '</figure>'
  366. );
  367. } );
  368. it( 'should not convert srcset attribute if has wrong data', () => {
  369. setModelData( model,
  370. '<image ' +
  371. 'src="foo.png" ' +
  372. 'alt="alt text" ' +
  373. 'srcset=\'{ "foo":"bar" }\'>' +
  374. '</image>' );
  375. const image = document.getRoot().getChild( 0 );
  376. model.change( writer => {
  377. writer.removeAttribute( 'srcset', image );
  378. } );
  379. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  380. '<figure class="ck-widget image" contenteditable="false">' +
  381. '<img alt="alt text" src="foo.png"></img>' +
  382. '</figure>'
  383. );
  384. } );
  385. it( 'should convert srcset attribute to srcset, width and sizes', () => {
  386. setModelData( model,
  387. '<image ' +
  388. 'src="foo.png" ' +
  389. 'alt="alt text" ' +
  390. 'srcset=\'{ "data":"small.png 148w, big.png 1024w", "width":"1024" }\'>' +
  391. '</image>' );
  392. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  393. '<figure class="ck-widget image" contenteditable="false">' +
  394. '<img alt="alt text" sizes="100vw" src="foo.png" srcset="small.png 148w, big.png 1024w" width="1024"></img>' +
  395. '</figure>'
  396. );
  397. } );
  398. it( 'should remove sizes and srcsset attribute when srcset attribute is removed from model', () => {
  399. setModelData( model, '<image src="foo.png" srcset=\'{ "data": "small.png 148w, big.png 1024w" }\'></image>' );
  400. const image = document.getRoot().getChild( 0 );
  401. model.change( writer => {
  402. writer.removeAttribute( 'srcset', image );
  403. } );
  404. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  405. '<figure class="ck-widget image" contenteditable="false">' +
  406. '<img src="foo.png"></img>' +
  407. '</figure>'
  408. );
  409. } );
  410. it( 'should remove width, sizes and srcsset attribute when srcset attribute is removed from model', () => {
  411. setModelData( model,
  412. '<image ' +
  413. 'src="foo.png" ' +
  414. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  415. '</image>'
  416. );
  417. const image = document.getRoot().getChild( 0 );
  418. model.change( writer => {
  419. writer.removeAttribute( 'srcset', image );
  420. } );
  421. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  422. '<figure class="ck-widget image" contenteditable="false">' +
  423. '<img src="foo.png"></img>' +
  424. '</figure>'
  425. );
  426. } );
  427. it( 'should not convert srcset attribute if is already consumed', () => {
  428. editor.editing.modelToView.on( 'addAttribute:srcset:image', ( evt, data, consumable ) => {
  429. const parts = evt.name.split( ':' );
  430. const consumableType = parts[ 0 ] + ':' + parts[ 1 ];
  431. const modelImage = data.item;
  432. consumable.consume( modelImage, consumableType );
  433. }, { priority: 'high' } );
  434. setModelData( model,
  435. '<image ' +
  436. 'src="foo.png" ' +
  437. 'alt="alt text" ' +
  438. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  439. '</image>'
  440. );
  441. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  442. '<figure class="ck-widget image" contenteditable="false">' +
  443. '<img alt="alt text" src="foo.png"></img>' +
  444. '</figure>'
  445. );
  446. } );
  447. } );
  448. } );
  449. } );