1
1
import $ from 'jquery' ;
2
+ import { GET } from '../modules/fetch.js' ;
2
3
import { hideElem } from '../utils/dom.js' ;
4
+ import { parseUrl } from '../utils.js' ;
3
5
4
- function getDefaultSvgBoundsIfUndefined ( svgXml , src ) {
6
+ function getDefaultSvgBoundsIfUndefined ( text , src ) {
5
7
const DefaultSize = 300 ;
6
8
const MaxSize = 99999 ;
7
9
8
- const svg = svgXml . documentElement ;
10
+ const parser = new DOMParser ( ) ;
11
+ const svgDoc = parser . parseFromString ( text , 'image/svg+xml' ) ;
12
+ const svg = svgDoc . documentElement ;
9
13
const width = svg ?. width ?. baseVal ;
10
14
const height = svg ?. height ?. baseVal ;
11
15
if ( width === undefined || height === undefined ) {
@@ -65,7 +69,7 @@ export function initImageDiff() {
65
69
} ;
66
70
}
67
71
68
- $ ( '.image-diff:not([data-image-diff-loaded])' ) . each ( function ( ) {
72
+ $ ( '.image-diff:not([data-image-diff-loaded])' ) . each ( async function ( ) {
69
73
const $container = $ ( this ) ;
70
74
$container . attr ( 'data-image-diff-loaded' , 'true' ) ;
71
75
@@ -88,29 +92,27 @@ export function initImageDiff() {
88
92
89
93
for ( const info of imageInfos ) {
90
94
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 ) ;
103
104
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 ) ;
112
114
}
113
- } ) ;
115
+ }
114
116
} else {
115
117
info . loaded = true ;
116
118
setReadyIfLoaded ( ) ;
0 commit comments