Skip to content

Commit

Permalink
feat: try fix svg+xml decode (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed Jul 31, 2023
1 parent 8d1860b commit cada074
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/embed-css-style-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function embedCssStyleImage(
if (!value) {
return null
}
if ((IN_SAFARI || IN_FIREFOX) && value.includes('data:image/svg+xml')) {
if (IN_SAFARI || IN_FIREFOX) {
context.drawImageCount++
}
return replaceCssUrlToDataUrl(value, null, context, true).then(newValue => {
Expand Down
2 changes: 1 addition & 1 deletion src/embed-image-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function embedImageElement<T extends HTMLImageElement | SVGImageElement>(
]
}

if ((IN_SAFARI || IN_FIREFOX) && originalSrc.includes('data:image/svg+xml')) {
if (IN_SAFARI || IN_FIREFOX) {
context.drawImageCount++
}
} else if (isSVGElementNode(clone) && !isDataUrl(clone.href.baseVal)) {
Expand Down
21 changes: 9 additions & 12 deletions src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IN_SAFARI, blobToDataUrl, consoleWarn } from './utils'
import type { Context, Request } from './context'
import { IN_FIREFOX, IN_SAFARI, blobToDataUrl, consoleWarn } from './utils'
import type { Context } from './context'

export type BaseFetchOptions = RequestInit & {
url: string
Expand Down Expand Up @@ -53,19 +53,18 @@ export function contextFetch(context: Context, options: ContextFetchOptions) {
workers,
} = context

let request: Request
if (requestType === 'image' && (IN_SAFARI || IN_FIREFOX)) {
context.drawImageCount++
}

if (!requests.has(rawUrl)) {
let request = requests.get(rawUrl)
if (!request) {
// cache bypass so we dont have CORS issues with cached images
// ref: https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Bypassing_the_cache
if (bypassingCache) {
url += (/\?/.test(url) ? '&' : '?') + new Date().getTime()
}

if (requestType === 'image' && IN_SAFARI) {
context.drawImageCount++
}

const baseFetchOptions: BaseFetchOptions = {
url,
timeout,
Expand All @@ -86,8 +85,8 @@ export function contextFetch(context: Context, options: ContextFetchOptions) {
? new Promise((resolve, reject) => {
const worker = workers[requests.size & (workers.length - 1)]
worker.postMessage({ rawUrl, ...baseFetchOptions })
request.resovle = resolve
request.reject = reject
request!.resovle = resolve

This comment has been minimized.

Copy link
@jportela

jportela Jul 31, 2023

request.resovle? Don't you mean request.resolve? Not sure if this makes a difference, but I noticed the typo and wanted to point it out in case it helps preventing a bug

request!.reject = reject
})
: baseFetch(baseFetchOptions)
).catch(error => {
Expand All @@ -104,8 +103,6 @@ export function contextFetch(context: Context, options: ContextFetchOptions) {
}) as any

requests.set(rawUrl, request)
} else {
request = requests.get(rawUrl)!
}

return request.response
Expand Down
16 changes: 9 additions & 7 deletions src/image-to-canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ export async function imageToCanvas<T extends HTMLImageElement>(

drawImage()

for (let i = 0; i < drawImageCount; i++) {
await new Promise<void>(resolve => {
setTimeout(() => {
drawImage()
resolve()
}, i + drawImageInterval)
})
if (context.isEnable('fixSvgXmlDecode')) {
for (let i = 0; i < drawImageCount; i++) {
await new Promise<void>(resolve => {
setTimeout(() => {
drawImage()
resolve()
}, i + drawImageInterval)
})
}
}

context.drawImageCount = 0
Expand Down
9 changes: 8 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,18 @@ export interface Options {
* default: true
*/
removeControlCharacter?: boolean

/**
* Fix svg+xml image decode (for Safari、Firefox)
*
* default: true
*/
fixSvgXmlDecode?: boolean
}

/**
* Canvas `drawImage` interval
* is used to fix errors in decoding images in safari/webkit
* is used to fix errors in decoding images in Safari、Firefox
*
* default: 100
*/
Expand Down

1 comment on commit cada074

@vercel
Copy link

@vercel vercel bot commented on cada074 Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

modern-screenshot – ./

modern-screenshot-qq15725.vercel.app
modern-screenshot-git-main-qq15725.vercel.app
modern-screenshot.vercel.app

Please sign in to comment.