8
0

imageediting.js 19 KB

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