8
0

imageengine.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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, 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.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.modelToView.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. const data = editor.data;
  142. const editing = editor.editing;
  143. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  144. model.schema.addChildCheck( ( ctx, childDef ) => {
  145. if ( ctx.endsWith( '$root' ) && childDef.name == 'image' ) {
  146. return false;
  147. }
  148. } );
  149. buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  150. buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  151. editor.setData( '<div><figure class="image"><img src="foo.png" alt="alt text" /></figure></div>' );
  152. expect( getModelData( model, { withoutSelection: true } ) )
  153. .to.equal( '<div></div>' );
  154. } );
  155. it( 'should not convert if img is already consumed', () => {
  156. editor.data.viewToModel.on( 'element:figure', ( evt, data, conversionApi ) => {
  157. const img = data.viewItem.getChild( 0 );
  158. conversionApi.consumable.consume( img, { name: true } );
  159. }, { priority: 'high' } );
  160. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  161. expect( getModelData( model, { withoutSelection: true } ) )
  162. .to.equal( '' );
  163. } );
  164. it( 'should not convert if figure is already consumed', () => {
  165. editor.data.viewToModel.on( 'element:figure', ( evt, data, conversionApi ) => {
  166. conversionApi.consumable.consume( data.viewItem, { name: true, class: 'image' } );
  167. }, { priority: 'high' } );
  168. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  169. expect( getModelData( model, { withoutSelection: true } ) )
  170. .to.equal( '' );
  171. } );
  172. it( 'should dispatch conversion for nested elements', () => {
  173. const conversionSpy = sinon.spy();
  174. editor.data.viewToModel.on( 'element:figcaption', conversionSpy );
  175. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /><figcaption></figcaption></figure>' );
  176. sinon.assert.calledOnce( conversionSpy );
  177. } );
  178. it( 'should convert bare img element', () => {
  179. editor.setData( '<img src="foo.png" alt="alt text" />' );
  180. expect( getModelData( model, { withoutSelection: true } ) )
  181. .to.equal( '<image alt="alt text" src="foo.png"></image>' );
  182. } );
  183. it( 'should not convert alt attribute on non-img element', () => {
  184. const data = editor.data;
  185. const editing = editor.editing;
  186. model.schema.register( 'div', {
  187. inheritAllFrom: '$block',
  188. allowAttributes: 'alt'
  189. } );
  190. buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  191. buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  192. editor.setData( '<div alt="foo"></div>' );
  193. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<div></div>' );
  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.register( 'div', { inheritAllFrom: '$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.extend( 'div', { allowIn: '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.extend( 'div', { allowIn: '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.register( 'limit', {
  294. inheritAllFrom: '$block',
  295. isLimit: true
  296. } );
  297. model.schema.extend( 'div', { allowIn: 'limit' } );
  298. buildModelConverter()
  299. .for( editor.data.modelToView, editor.editing.modelToView ).fromElement( 'limit' ).toElement( 'limit' );
  300. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'limit' ).toElement( 'limit' );
  301. editor.setData( '<limit><div>foo<img src="foo.jpg" alt="foo" />bar</div></limit>' );
  302. // <limit> element does not have converters so it is not converted.
  303. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<limit><div>foobar</div></limit>' );
  304. } );
  305. it( 'should not convert and autohoist image element without src attribute (which is not allowed by schema)', () => {
  306. editor.setData( '<div>foo<img alt="foo" />bar</div>' );
  307. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<div>foobar</div>' );
  308. } );
  309. } );
  310. } );
  311. } );
  312. describe( 'conversion in editing pipeline', () => {
  313. describe( 'model to view', () => {
  314. it( 'should convert', () => {
  315. setModelData( model, '<image src="foo.png" alt="alt text"></image>' );
  316. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  317. '<figure class="ck-widget image" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>'
  318. );
  319. } );
  320. it( 'converted element should be widgetized', () => {
  321. setModelData( model, '<image src="foo.png" alt="alt text"></image>' );
  322. const figure = viewDocument.getRoot().getChild( 0 );
  323. expect( figure.name ).to.equal( 'figure' );
  324. expect( isImageWidget( figure ) ).to.be.true;
  325. } );
  326. it( 'should convert attribute change', () => {
  327. setModelData( model, '<image src="foo.png" alt="alt text"></image>' );
  328. const image = document.getRoot().getChild( 0 );
  329. model.change( writer => {
  330. writer.setAttribute( 'alt', 'new text', image );
  331. } );
  332. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  333. '<figure class="ck-widget image" contenteditable="false"><img alt="new text" src="foo.png"></img></figure>'
  334. );
  335. } );
  336. it( 'should convert attribute removal', () => {
  337. setModelData( model, '<image src="foo.png" alt="alt text"></image>' );
  338. const image = document.getRoot().getChild( 0 );
  339. model.change( writer => {
  340. writer.removeAttribute( 'alt', image );
  341. } );
  342. expect( getViewData( viewDocument, { withoutSelection: true } ) )
  343. .to.equal( '<figure class="ck-widget image" contenteditable="false"><img src="foo.png"></img></figure>' );
  344. } );
  345. it( 'should not convert change if is already consumed', () => {
  346. setModelData( model, '<image src="foo.png" alt="alt text"></image>' );
  347. const image = document.getRoot().getChild( 0 );
  348. editor.editing.modelToView.on( 'attribute:alt:image', ( evt, data, consumable ) => {
  349. consumable.consume( data.item, 'attribute:alt' );
  350. }, { priority: 'high' } );
  351. model.change( writer => {
  352. writer.removeAttribute( 'alt', image );
  353. } );
  354. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  355. '<figure class="ck-widget image" contenteditable="false"><img alt="alt text" src="foo.png"></img></figure>'
  356. );
  357. } );
  358. it( 'should convert srcset attribute to srcset and sizes', () => {
  359. setModelData( model,
  360. '<image ' +
  361. 'src="foo.png" ' +
  362. 'alt="alt text" ' +
  363. 'srcset=\'{ "data":"small.png 148w, big.png 1024w" }\'>' +
  364. '</image>' );
  365. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  366. '<figure class="ck-widget image" contenteditable="false">' +
  367. '<img alt="alt text" sizes="100vw" src="foo.png" srcset="small.png 148w, big.png 1024w"></img>' +
  368. '</figure>'
  369. );
  370. } );
  371. it( 'should not convert srcset attribute if has wrong data', () => {
  372. setModelData( model,
  373. '<image ' +
  374. 'src="foo.png" ' +
  375. 'alt="alt text" ' +
  376. 'srcset=\'{ "foo":"bar" }\'>' +
  377. '</image>' );
  378. const image = document.getRoot().getChild( 0 );
  379. model.change( writer => {
  380. writer.removeAttribute( 'srcset', image );
  381. } );
  382. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  383. '<figure class="ck-widget image" contenteditable="false">' +
  384. '<img alt="alt text" src="foo.png"></img>' +
  385. '</figure>'
  386. );
  387. } );
  388. it( 'should convert srcset attribute to srcset, width and sizes', () => {
  389. setModelData( model,
  390. '<image ' +
  391. 'src="foo.png" ' +
  392. 'alt="alt text" ' +
  393. 'srcset=\'{ "data":"small.png 148w, big.png 1024w", "width":"1024" }\'>' +
  394. '</image>' );
  395. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  396. '<figure class="ck-widget image" contenteditable="false">' +
  397. '<img alt="alt text" sizes="100vw" src="foo.png" srcset="small.png 148w, big.png 1024w" width="1024"></img>' +
  398. '</figure>'
  399. );
  400. } );
  401. it( 'should remove sizes and srcsset attribute when srcset attribute is removed from model', () => {
  402. setModelData( model, '<image src="foo.png" srcset=\'{ "data": "small.png 148w, big.png 1024w" }\'></image>' );
  403. const image = document.getRoot().getChild( 0 );
  404. model.change( writer => {
  405. writer.removeAttribute( 'srcset', image );
  406. } );
  407. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  408. '<figure class="ck-widget image" contenteditable="false">' +
  409. '<img src="foo.png"></img>' +
  410. '</figure>'
  411. );
  412. } );
  413. it( 'should remove width, sizes and srcsset attribute when srcset attribute is removed from model', () => {
  414. setModelData( model,
  415. '<image ' +
  416. 'src="foo.png" ' +
  417. 'srcset=\'{ "data": "small.png 148w, big.png 1024w", "width": "1024" }\'>' +
  418. '</image>'
  419. );
  420. const image = document.getRoot().getChild( 0 );
  421. model.change( writer => {
  422. writer.removeAttribute( 'srcset', image );
  423. } );
  424. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  425. '<figure class="ck-widget image" contenteditable="false">' +
  426. '<img src="foo.png"></img>' +
  427. '</figure>'
  428. );
  429. } );
  430. it( 'should not convert srcset attribute if is already consumed', () => {
  431. editor.editing.modelToView.on( 'attribute:srcset:image', ( evt, data, consumable ) => {
  432. const modelImage = data.item;
  433. consumable.consume( modelImage, evt.name );
  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. } );