imageengine.js 19 KB

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