11import $ from 'jquery' ;
2+ import { GET } from '../modules/fetch.js' ;
23import { hideElem } from '../utils/dom.js' ;
4+ import { parseUrl } from '../utils.js' ;
35
4- function getDefaultSvgBoundsIfUndefined ( svgXml , src ) {
6+ function getDefaultSvgBoundsIfUndefined ( text , src ) {
57 const DefaultSize = 300 ;
68 const MaxSize = 99999 ;
79
8- const svg = svgXml . documentElement ;
10+ const parser = new DOMParser ( ) ;
11+ const svgDoc = parser . parseFromString ( text , 'image/svg+xml' ) ;
12+ const svg = svgDoc . documentElement ;
913 const width = svg ?. width ?. baseVal ;
1014 const height = svg ?. height ?. baseVal ;
1115 if ( width === undefined || height === undefined ) {
@@ -65,7 +69,7 @@ export function initImageDiff() {
6569 } ;
6670 }
6771
68- $ ( '.image-diff:not([data-image-diff-loaded])' ) . each ( function ( ) {
72+ $ ( '.image-diff:not([data-image-diff-loaded])' ) . each ( async function ( ) {
6973 const $container = $ ( this ) ;
7074 $container . attr ( 'data-image-diff-loaded' , 'true' ) ;
7175
@@ -88,29 +92,27 @@ export function initImageDiff() {
8892
8993 for ( const info of imageInfos ) {
9094 if ( info . $image . length > 0 ) {
91- $ . ajax ( {
92- url : info . path ,
93- success : ( data , _ , jqXHR ) => {
94- info . $image . on ( 'load' , ( ) => {
95- info . loaded = true ;
96- setReadyIfLoaded ( ) ;
97- } ) . on ( 'error' , ( ) => {
98- info . loaded = true ;
99- setReadyIfLoaded ( ) ;
100- info . $boundsInfo . text ( '(image error)' ) ;
101- } ) ;
102- info . $image . attr ( 'src' , info . path ) ;
95+ info . $image . on ( 'load' , ( ) => {
96+ info . loaded = true ;
97+ setReadyIfLoaded ( ) ;
98+ } ) . on ( 'error' , ( ) => {
99+ info . loaded = true ;
100+ setReadyIfLoaded ( ) ;
101+ info . $boundsInfo . text ( '(image error)' ) ;
102+ } ) ;
103+ info . $image . attr ( 'src' , info . path ) ;
103104
104- if ( jqXHR . getResponseHeader ( 'Content-Type' ) === 'image/svg+xml' ) {
105- const bounds = getDefaultSvgBoundsIfUndefined ( data , info . path ) ;
106- if ( bounds ) {
107- info . $image . attr ( 'width' , bounds . width ) ;
108- info . $image . attr ( 'height' , bounds . height ) ;
109- hideElem ( info . $boundsInfo ) ;
110- }
111- }
105+ // this may be dead code as we currently do not render SVGs images in image diffs
106+ if ( parseUrl ( info . path ) . pathname . toLowerCase ( ) . endsWith ( '.svg' ) ) {
107+ const resp = await GET ( info . path ) ;
108+ const text = await resp . text ( ) ;
109+ const bounds = getDefaultSvgBoundsIfUndefined ( text , info . path ) ;
110+ if ( bounds ) {
111+ info . $image . attr ( 'width' , bounds . width ) ;
112+ info . $image . attr ( 'height' , bounds . height ) ;
113+ hideElem ( info . $boundsInfo ) ;
112114 }
113- } ) ;
115+ }
114116 } else {
115117 info . loaded = true ;
116118 setReadyIfLoaded ( ) ;
0 commit comments