8
0

imageediting.js 19 KB

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