imageengine.js 19 KB

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