imageengine.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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.isObject( '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( 'attribute: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.register( 'div', { inheritAllFrom: '$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.register( 'div', {
  181. inheritAllFrom: '$block',
  182. allowAttributes: 'alt'
  183. } );
  184. buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  185. buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  186. editor.setData( '<div alt="foo"></div>' );
  187. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<div></div>' );
  188. } );
  189. it( 'should handle figure with two images', () => {
  190. model.schema.extend( '$text', { allowIn: 'image' } );
  191. editor.setData( '<figure class="image"><img src="foo.jpg" /><img src="bar.jpg" />abc</figure>' );
  192. // The foo.jpg image is properly converted using figure converter. The other image was tried to
  193. // be added as a child of foo.jpg and then was autohoisted.
  194. expect( getModelData( model, { withoutSelection: true } ) )
  195. .to.equal( '<image src="bar.jpg"></image><image src="foo.jpg">abc</image>' );
  196. } );
  197. it( 'should convert image with srcset attribute', () => {
  198. editor.setData(
  199. '<figure class="image">' +
  200. '<img src="foo.png" alt="alt text" srcset="small.png 148w, big.png 1024w" />' +
  201. '</figure>'
  202. );
  203. expect( getModelData( model, { withoutSelection: true } ) )
  204. .to.equal( '<image alt="alt text" src="foo.png" srcset="{"data":"small.png 148w, big.png 1024w"}"></image>' );
  205. } );
  206. it( 'should convert image with srcset and width attributes', () => {
  207. editor.setData(
  208. '<figure class="image">' +
  209. '<img src="foo.png" alt="alt text" srcset="small.png 148w, big.png 1024w" width="1024" />' +
  210. '</figure>'
  211. );
  212. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  213. '<image alt="alt text" src="foo.png" srcset="{"data":"small.png 148w, big.png 1024w","width":"1024"}"></image>' );
  214. } );
  215. it( 'should ignore sizes attribute', () => {
  216. editor.setData(
  217. '<figure class="image">' +
  218. '<img src="foo.png" alt="alt text" srcset="small.png 148w, big.png 1024w" sizes="50vw" />' +
  219. '</figure>'
  220. );
  221. expect( getModelData( model, { withoutSelection: true } ) )
  222. .to.equal( '<image alt="alt text" src="foo.png" srcset="{"data":"small.png 148w, big.png 1024w"}"></image>' );
  223. } );
  224. describe( 'should autohoist images', () => {
  225. beforeEach( () => {
  226. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  227. buildModelConverter()
  228. .for( editor.data.modelToView, editor.editing.modelToView )
  229. .fromElement( 'div' )
  230. .toElement( 'div' );
  231. buildViewConverter()
  232. .for( editor.data.viewToModel )
  233. .fromElement( 'div' )
  234. .toElement( 'div' );
  235. } );
  236. it( 'image between non-hoisted elements', () => {
  237. editor.setData( '<div>foo<img src="foo.jpg" alt="foo" />bar</div>' );
  238. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  239. '<div>foo</div>' +
  240. '<image alt="foo" src="foo.jpg"></image>' +
  241. '<div>bar</div>'
  242. );
  243. } );
  244. it( 'multiple images', () => {
  245. editor.setData( '<div>foo<img src="foo.jpg" alt="foo" />ba<img src="foo.jpg" alt="foo" />r</div>' );
  246. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  247. '<div>foo</div>' +
  248. '<image alt="foo" src="foo.jpg"></image>' +
  249. '<div>ba</div>' +
  250. '<image alt="foo" src="foo.jpg"></image>' +
  251. '<div>r</div>'
  252. );
  253. } );
  254. it( 'images on borders of parent', () => {
  255. editor.setData( '<div><img src="foo.jpg" alt="foo" />foobar<img src="foo.jpg" alt="foo" /></div>' );
  256. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  257. '<image alt="foo" src="foo.jpg"></image>' +
  258. '<div>foobar</div>' +
  259. '<image alt="foo" src="foo.jpg"></image>'
  260. );
  261. } );
  262. it( 'images are only content of parent', () => {
  263. editor.setData( '<div><img src="foo.jpg" alt="foo" /><img src="foo.jpg" alt="foo" /></div>' );
  264. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  265. '<image alt="foo" src="foo.jpg"></image>' +
  266. '<image alt="foo" src="foo.jpg"></image>'
  267. );
  268. } );
  269. it( 'deep autohoisting #1', () => {
  270. model.schema.extend( 'div', { allowIn: 'div' } );
  271. editor.setData( '<div>foo<div>xx<img src="foo.jpg" alt="foo" /></div>bar</div>' );
  272. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  273. '<div>' +
  274. 'foo' +
  275. '<div>' +
  276. 'xx' +
  277. '</div>' +
  278. '</div>' +
  279. '<image alt="foo" src="foo.jpg"></image>' +
  280. '<div>bar</div>'
  281. );
  282. } );
  283. it( 'deep autohoisting #2', () => {
  284. model.schema.extend( 'div', { allowIn: 'div' } );
  285. editor.setData(
  286. '<div>x</div>' +
  287. '<div><div><div><img src="foo.jpg" alt="foo" /></div></div></div>' +
  288. '<div>y</div>'
  289. );
  290. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  291. '<div>x</div><image alt="foo" src="foo.jpg"></image><div>y</div>'
  292. );
  293. } );
  294. it( 'should not break a limiting element', () => {
  295. model.schema.register( 'limit', {
  296. inheritAllFrom: '$block',
  297. isLimit: true
  298. } );
  299. model.schema.extend( 'div', { allowIn: 'limit' } );
  300. buildModelConverter()
  301. .for( editor.data.modelToView, editor.editing.modelToView ).fromElement( 'limit' ).toElement( 'limit' );
  302. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'limit' ).toElement( 'limit' );
  303. editor.setData( '<limit><div>foo<img src="foo.jpg" alt="foo" />bar</div></limit>' );
  304. // <limit> element does not have converters so it is not converted.
  305. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<limit><div>foobar</div></limit>' );
  306. } );
  307. it( 'should not convert and autohoist image element without src attribute (which is not allowed by schema)', () => {
  308. editor.setData( '<div>foo<img alt="foo" />bar</div>' );
  309. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<div>foobar</div>' );
  310. } );
  311. } );
  312. } );
  313. } );
  314. describe( 'conversion in editing pipeline', () => {
  315. describe( 'model to view', () => {
  316. it( 'should convert', () => {
  317. setModelData( model, '<image src="foo.png" alt="alt text"></image>' );
  318. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  319. '<figure class="ck-widget image" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>'
  320. );
  321. } );
  322. it( 'converted element should be widgetized', () => {
  323. setModelData( model, '<image src="foo.png" alt="alt text"></image>' );
  324. const figure = viewDocument.getRoot().getChild( 0 );
  325. expect( figure.name ).to.equal( 'figure' );
  326. expect( isImageWidget( figure ) ).to.be.true;
  327. } );
  328. it( 'should convert attribute change', () => {
  329. setModelData( model, '<image src="foo.png" alt="alt text"></image>' );
  330. const image = document.getRoot().getChild( 0 );
  331. model.change( writer => {
  332. writer.setAttribute( 'alt', 'new text', image );
  333. } );
  334. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  335. '<figure class="ck-widget image" contenteditable="false"><img alt="new text" src="foo.png"></img></figure>'
  336. );
  337. } );
  338. it( 'should convert attribute removal', () => {
  339. setModelData( model, '<image src="foo.png" alt="alt text"></image>' );
  340. const image = document.getRoot().getChild( 0 );
  341. model.change( writer => {
  342. writer.removeAttribute( 'alt', image );
  343. } );
  344. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  345. .to.equal( '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>' );
  346. } );
  347. it( 'should not convert change if is already consumed', () => {
  348. setModelData( model, '<image src="foo.png" alt="alt text"></image>' );
  349. const image = document.getRoot().getChild( 0 );
  350. editor.editing.modelToView.on( 'attribute:alt:image', ( evt, data, consumable ) => {
  351. consumable.consume( data.item, 'attribute:alt' );
  352. }, { priority: 'high' } );
  353. model.change( writer => {
  354. writer.removeAttribute( 'alt', image );
  355. } );
  356. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  357. '<figure class="ck-widget image" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>'
  358. );
  359. } );
  360. it( 'should convert srcset attribute to srcset and sizes', () => {
  361. setModelData( model,
  362. '<image ' +
  363. 'src="foo.png" ' +
  364. 'alt="alt text" ' +
  365. 'srcset=\'{ "data":"small.png 148w, big.png 1024w" }\'>' +
  366. '</image>' );
  367. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  368. '<figure class="ck-widget image" contenteditable="false">' +
  369. '<img alt="alt text" sizes="100vw" src="foo.png" srcset="small.png 148w, big.png 1024w"></img>' +
  370. '</figure>'
  371. );
  372. } );
  373. it( 'should not convert srcset attribute if has wrong data', () => {
  374. setModelData( model,
  375. '<image ' +
  376. 'src="foo.png" ' +
  377. 'alt="alt text" ' +
  378. 'srcset=\'{ "foo":"bar" }\'>' +
  379. '</image>' );
  380. const image = document.getRoot().getChild( 0 );
  381. model.change( writer => {
  382. writer.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( model,
  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( model, '<image src="foo.png" srcset=\'{ "data": "small.png 148w, big.png 1024w" }\'></image>' );
  405. const image = document.getRoot().getChild( 0 );
  406. model.change( writer => {
  407. writer.removeAttribute( 'srcset', image );
  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( model,
  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. model.change( writer => {
  424. writer.removeAttribute( 'srcset', image );
  425. } );
  426. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  427. '<figure class="ck-widget image" contenteditable="false">' +
  428. '<img src="foo.png"></img>' +
  429. '</figure>'
  430. );
  431. } );
  432. it( 'should not convert srcset attribute if is already consumed', () => {
  433. editor.editing.modelToView.on( 'attribute:srcset:image', ( evt, data, consumable ) => {
  434. const parts = evt.name.split( ':' );
  435. const consumableType = parts[ 0 ] + ':' + parts[ 1 ];
  436. const modelImage = data.item;
  437. consumable.consume( modelImage, consumableType );
  438. }, { priority: 'high' } );
  439. setModelData( model,
  440. '<image ' +
  441. 'src="foo.png" ' +
  442. 'alt="alt text" ' +
  443. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  444. '</image>'
  445. );
  446. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  447. '<figure class="ck-widget image" contenteditable="false">' +
  448. '<img alt="alt text" src="foo.png"></img>' +
  449. '</figure>'
  450. );
  451. } );
  452. } );
  453. } );
  454. } );