8
0

imageengine.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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, model, document, viewDocument;
  15. beforeEach( () => {
  16. return VirtualTestEditor
  17. .create( {
  18. plugins: [ ImageEngine ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. model = editor.model;
  23. document = model.document;
  24. viewDocument = editor.editing.view;
  25. } );
  26. } );
  27. it( 'should be loaded', () => {
  28. expect( editor.plugins.get( ImageEngine ) ).to.be.instanceOf( ImageEngine );
  29. } );
  30. it( 'should set proper schema rules', () => {
  31. expect( model.schema.check( { name: 'image', attributes: [ 'src', 'alt' ], inside: '$root' } ) ).to.be.true;
  32. expect( model.schema.objects.has( 'image' ) ).to.be.true;
  33. } );
  34. describe( 'conversion in data pipeline', () => {
  35. describe( 'model to view', () => {
  36. it( 'should convert', () => {
  37. setModelData( model, '<image src="foo.png" alt="alt text"></image>' );
  38. expect( editor.getData() ).to.equal( '<figure class="image"><img alt="alt text" src="foo.png"></figure>' );
  39. } );
  40. it( 'should convert without alt attribute', () => {
  41. setModelData( model, '<image src="foo.png"></image>' );
  42. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  43. } );
  44. it( 'should convert srcset attribute to srcset and sizes attribute', () => {
  45. setModelData( model,
  46. '<image src="foo.png" alt="alt text" srcset=\'{ "data": "small.png 148w, big.png 1024w" }\'></image>'
  47. );
  48. expect( normalizeHtml( editor.getData() ) ).to.equal(
  49. '<figure class="image">' +
  50. '<img alt="alt text" sizes="100vw" src="foo.png" srcset="small.png 148w, big.png 1024w"></img>' +
  51. '</figure>'
  52. );
  53. } );
  54. it( 'should convert srcset attribute to width, srcset and add sizes attribute', () => {
  55. setModelData( model,
  56. '<image ' +
  57. 'src="foo.png" ' +
  58. 'alt="alt text" ' +
  59. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  60. '</image>'
  61. );
  62. expect( normalizeHtml( editor.getData() ) ).to.equal(
  63. '<figure class="image">' +
  64. '<img alt="alt text" sizes="100vw" src="foo.png" srcset="small.png 148w, big.png 1024w" width="1024"></img>' +
  65. '</figure>'
  66. );
  67. } );
  68. it( 'should not convert srcset attribute if is already consumed', () => {
  69. editor.data.modelToView.on( 'addAttribute:srcset:image', ( evt, data, consumable ) => {
  70. const parts = evt.name.split( ':' );
  71. const consumableType = parts[ 0 ] + ':' + parts[ 1 ];
  72. const modelImage = data.item;
  73. consumable.consume( modelImage, consumableType );
  74. }, { priority: 'high' } );
  75. setModelData( model,
  76. '<image ' +
  77. 'src="foo.png" ' +
  78. 'alt="alt text" ' +
  79. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  80. '</image>'
  81. );
  82. expect( editor.getData() ).to.equal(
  83. '<figure class="image">' +
  84. '<img alt="alt text" src="foo.png">' +
  85. '</figure>'
  86. );
  87. } );
  88. it( 'should not convert srcset attribute if has wrong data', () => {
  89. setModelData( model,
  90. '<image ' +
  91. 'src="foo.png" ' +
  92. 'alt="alt text" ' +
  93. 'srcset=\'{ "foo":"bar" }\'>' +
  94. '</image>' );
  95. const image = document.getRoot().getChild( 0 );
  96. model.change( writer => {
  97. writer.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( model, { 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( model, { 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( model, { 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( model, { 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( model, { 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( model, { 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. model.schema.registerItem( 'div', '$block' );
  141. model.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( model, { 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( model, { 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( model, { 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( model, { 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. model.schema.registerItem( 'div', '$block' );
  181. model.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( model, { withoutSelection: true } ) ).to.equal( '<div></div>' );
  186. } );
  187. it( 'should handle figure with two images', () => {
  188. model.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( model, { 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( model, { 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( model, { 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( model, { 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. model.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( model, { 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( model, { 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( model, { 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( model, { 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. model.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( model, { 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. model.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( model, { 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. model.schema.registerItem( 'limit', '$block' );
  294. model.schema.allow( { name: 'div', inside: 'limit' } );
  295. model.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( model, { 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( model, { 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( model, '<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( model, '<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( model, '<image src="foo.png" alt="alt text"></image>' );
  326. const image = document.getRoot().getChild( 0 );
  327. model.change( writer => {
  328. writer.setAttribute( 'alt', 'new text', image );
  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( model, '<image src="foo.png" alt="alt text"></image>' );
  336. const image = document.getRoot().getChild( 0 );
  337. model.change( writer => {
  338. writer.removeAttribute( 'alt', image );
  339. } );
  340. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  341. .to.equal( '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>' );
  342. } );
  343. it( 'should not convert change if is already consumed', () => {
  344. setModelData( model, '<image src="foo.png" alt="alt text"></image>' );
  345. const image = document.getRoot().getChild( 0 );
  346. editor.editing.modelToView.on( 'removeAttribute:alt:image', ( evt, data, consumable ) => {
  347. consumable.consume( data.item, 'removeAttribute:alt' );
  348. }, { priority: 'high' } );
  349. model.change( writer => {
  350. writer.removeAttribute( 'alt', image );
  351. } );
  352. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  353. '<figure class="ck-widget image" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>'
  354. );
  355. } );
  356. it( 'should convert srcset attribute to srcset and sizes', () => {
  357. setModelData( model,
  358. '<image ' +
  359. 'src="foo.png" ' +
  360. 'alt="alt text" ' +
  361. 'srcset=\'{ "data":"small.png 148w, big.png 1024w" }\'>' +
  362. '</image>' );
  363. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  364. '<figure class="ck-widget image" contenteditable="false">' +
  365. '<img alt="alt text" sizes="100vw" src="foo.png" srcset="small.png 148w, big.png 1024w"></img>' +
  366. '</figure>'
  367. );
  368. } );
  369. it( 'should not convert srcset attribute if has wrong data', () => {
  370. setModelData( model,
  371. '<image ' +
  372. 'src="foo.png" ' +
  373. 'alt="alt text" ' +
  374. 'srcset=\'{ "foo":"bar" }\'>' +
  375. '</image>' );
  376. const image = document.getRoot().getChild( 0 );
  377. model.change( writer => {
  378. writer.removeAttribute( 'srcset', image );
  379. } );
  380. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  381. '<figure class="ck-widget image" contenteditable="false">' +
  382. '<img alt="alt text" src="foo.png"></img>' +
  383. '</figure>'
  384. );
  385. } );
  386. it( 'should convert srcset attribute to srcset, width and sizes', () => {
  387. setModelData( model,
  388. '<image ' +
  389. 'src="foo.png" ' +
  390. 'alt="alt text" ' +
  391. 'srcset=\'{ "data":"small.png 148w, big.png 1024w", "width":"1024" }\'>' +
  392. '</image>' );
  393. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  394. '<figure class="ck-widget image" contenteditable="false">' +
  395. '<img alt="alt text" sizes="100vw" src="foo.png" srcset="small.png 148w, big.png 1024w" width="1024"></img>' +
  396. '</figure>'
  397. );
  398. } );
  399. it( 'should remove sizes and srcsset attribute when srcset attribute is removed from model', () => {
  400. setModelData( model, '<image src="foo.png" srcset=\'{ "data": "small.png 148w, big.png 1024w" }\'></image>' );
  401. const image = document.getRoot().getChild( 0 );
  402. model.change( writer => {
  403. writer.removeAttribute( 'srcset', image );
  404. } );
  405. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  406. '<figure class="ck-widget image" contenteditable="false">' +
  407. '<img src="foo.png"></img>' +
  408. '</figure>'
  409. );
  410. } );
  411. it( 'should remove width, sizes and srcsset attribute when srcset attribute is removed from model', () => {
  412. setModelData( model,
  413. '<image ' +
  414. 'src="foo.png" ' +
  415. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  416. '</image>'
  417. );
  418. const image = document.getRoot().getChild( 0 );
  419. model.change( writer => {
  420. writer.removeAttribute( 'srcset', image );
  421. } );
  422. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  423. '<figure class="ck-widget image" contenteditable="false">' +
  424. '<img src="foo.png"></img>' +
  425. '</figure>'
  426. );
  427. } );
  428. it( 'should not convert srcset attribute if is already consumed', () => {
  429. editor.editing.modelToView.on( 'addAttribute:srcset:image', ( evt, data, consumable ) => {
  430. const parts = evt.name.split( ':' );
  431. const consumableType = parts[ 0 ] + ':' + parts[ 1 ];
  432. const modelImage = data.item;
  433. consumable.consume( modelImage, consumableType );
  434. }, { priority: 'high' } );
  435. setModelData( model,
  436. '<image ' +
  437. 'src="foo.png" ' +
  438. 'alt="alt text" ' +
  439. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  440. '</image>'
  441. );
  442. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  443. '<figure class="ck-widget image" contenteditable="false">' +
  444. '<img alt="alt text" src="foo.png"></img>' +
  445. '</figure>'
  446. );
  447. } );
  448. } );
  449. } );
  450. } );