refactor: improve image embedding and async image loading#504
Conversation
- Refactor embedImageNode to separately handle HTML and SVG image elements by introducing `updateHTMLImageElement` and `updateSVGImageElement` helper functions with proper JSDoc comments. - Enhance error handling by invoking `options.onImageErrorHandler` and ensuring lazy-loaded images are set to eager. - Update createImage to set crossOrigin, use async decoding, and improve promise handling for better reliability. - Rename variables in embedChildren for clarity. - Add tests for `svgToDataURL` and `nodeToDataURL` to verify correct data URL conversion. These changes improve code clarity, reliability, and performance in the image embedding process.
|
💖 Thanks for opening this pull request! 💖 Please follow the contributing guidelines. And we use semantic commit messages to streamline the release process. Examples of commit messages with semantic prefixes:
Things that will help get your PR across the finish line:
We get a lot of pull requests on this repo, so please be patient and we will get back to you as soon as we can. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #504 +/- ##
=======================================
Coverage 66.50% 66.50%
=======================================
Files 10 10
Lines 612 612
Branches 150 150
=======================================
Hits 407 407
Misses 144 144
Partials 61 61 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
I gave this a try, and in FireFox it sometimes results in this error: "DOMException: invalid image request." |
Bug fix and Refactor
updateHTMLImageElementandupdateSVGImageElementhelper functions with proper JSDoc comments.options.onImageErrorHandlerand ensuring lazy-loaded images are set to eager.svgToDataURLandnodeToDataURLto verify correct data URL conversion.These changes improve code clarity, reliability, and performance in the image embedding process.
Description
Motivation and Context
This change leverages the native
decode()method on image elements to ensure that images are fully processed before rendering. Previously, we encountered issues where the final rendered output was incomplete or inconsistent because the image data hadn't been fully decoded when the source was assigned. By invokingdecode(), we:decode()follows modern web standards for asynchronous image processing, as detailed in the MDN documentation.This change started because I was reading these issues, and decided to find the root cause of an issue on my project:
Types of changes
Self Check before Merge