Skip to content

Commit

Permalink
fix(VImg): SVG size wrong after uncached load (#12825)
Browse files Browse the repository at this point in the history
Co-authored-by: Kael <kaelwd@gmail.com>
  • Loading branch information
YipingRuan and KaelWD committed Dec 23, 2020
1 parent 1289033 commit a6e2e44
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions packages/vuetify/src/components/VImg/VImg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ export default mixins(
this.getSrc()
this.isLoading = false
this.$emit('load', this.src)

if (
this.image &&
(this.normalisedSrc.src.endsWith('.svg') || this.normalisedSrc.src.startsWith('data:image/svg+xml'))
) {
if (this.image.naturalHeight && this.image.naturalWidth) {
this.naturalWidth = this.image.naturalWidth
this.calculatedAspectRatio = this.image.naturalWidth / this.image.naturalHeight
} else {
this.calculatedAspectRatio = 1
}
}
},
onError () {
this.hasError = true
Expand Down Expand Up @@ -214,23 +226,13 @@ export default mixins(
},
pollForSize (img: HTMLImageElement, timeout: number | null = 100) {
const poll = () => {
let { naturalHeight, naturalWidth } = img

if (
img.src &&
(img.src.endsWith('.svg') || img.src.startsWith('data:image/svg+xml')) &&
naturalHeight === 0 &&
naturalWidth === 0
) {
naturalHeight = 1
naturalWidth = 1
}
const { naturalHeight, naturalWidth } = img

if (naturalHeight || naturalWidth) {
this.naturalWidth = naturalWidth
this.calculatedAspectRatio = naturalWidth / naturalHeight
} else {
timeout != null && !this.hasError && setTimeout(poll, timeout)
} else if (!img.complete && this.isLoading && !this.hasError && timeout != null) {
setTimeout(poll, timeout)
}
}

Expand Down

0 comments on commit a6e2e44

Please sign in to comment.