Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the incorrect usage of image.decode #401

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/clone-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ function cloneCSSStyle<T extends HTMLElement>(nativeNode: T, clonedNode: T) {
) {
value = 'block'
}

if (name === 'd' && clonedNode.getAttribute('d')) {
value = `path(${clonedNode.getAttribute('d')})`
}

targetStyle.setProperty(
name,
value,
Expand Down
22 changes: 12 additions & 10 deletions src/embed-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,24 @@ async function embedImageNode<T extends HTMLElement | SVGImageElement>(

const dataURL = await resourceToDataURL(url, getMimeType(url), options)
await new Promise((resolve, reject) => {
clonedNode.onload = resolve
clonedNode.onerror = reject

const image = clonedNode as HTMLImageElement
if (image.decode) {
image.decode = resolve as any
}

if (image.loading === 'lazy') {
image.loading = 'eager'
}

if (isImageElement) {
if (clonedNode.loading === 'lazy') {
clonedNode.loading = 'eager'
}

clonedNode.srcset = ''
clonedNode.src = dataURL

// `src` should be set before calling `decode`
if (clonedNode.decode) {
clonedNode.decode().then(resolve).catch(reject)
} else {
clonedNode.onload = resolve
}
} else {
clonedNode.onload = resolve
clonedNode.href.baseVal = dataURL
}
})
Expand Down
14 changes: 11 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,20 @@ export function canvasToBlob(
export function createImage(url: string): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
const img = new Image()
img.decode = () => resolve(img) as any
img.onload = () => resolve(img)
img.onerror = reject
img.crossOrigin = 'anonymous'
img.decoding = 'async'
img.src = url
img.onerror = reject

// `src` should be set before calling `decode`
if (img.decode) {
img
.decode()
.then(() => resolve(img))
.catch(reject)
} else {
img.onload = () => resolve(img)
}
})
}

Expand Down