8
0

imageediting.js 20 KB

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