Skip to content

Commit

Permalink
fix: move custom onerror handler into options
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Biroš committed Feb 13, 2024
1 parent 8c8e816 commit 3b6fd62
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/embed-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,15 @@ 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 = async (...params) => {
if (clonedNode.onerror) {
try {
const result = await clonedNode.onerror(...params)
resolve(result)
} catch (error) {
reject(error)
clonedNode.onerror = options.onImageErrorHandler
? (...attributes) => {
try {
resolve(options.onImageErrorHandler!(...attributes))
} catch (error) {
reject(error)
}
}
} else {
reject(new Error('Image failed to load'))
}
}
: reject

Check warning on line 66 in src/embed-images.ts

View check run for this annotation

Codecov / codecov/patch

src/embed-images.ts#L66

Added line #L66 was not covered by tests

const image = clonedNode as HTMLImageElement
if (image.decode) {
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,8 @@ export interface Options {
*
*/
fetchRequestInit?: RequestInit
/**
* An event handler for the error event when any image in html has problem with loading.
*/
onImageErrorHandler?: OnErrorEventHandler
}

0 comments on commit 3b6fd62

Please sign in to comment.