html-to-image
html-to-image copied to clipboard
Fixes issue fetching font css when cssRules are inaccessilbe
Fixes the handling of font css when cssRules are inaccessible.
Description
When cssRules are inaccessible in a style sheet, getCSSRules will throw an exception trying to read them, and then the catch handler will fetch the css. But, when it goes to insert the rule, it has a bug where it uses sheet.cssRules.length, which causes an exception again and the rules are lost.
This results in the error:
Error inlining remote css file SecurityError: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules
This is referenced in a few issues such as issue #49.
Motivation and Context
This fixes issues when fonts are referenced via external style sheets and the css cannot be read. The resulting image will be missing the fonts that failed. This error is mentioned in a few issues such as https://github.com/bubkoo/html-to-image/issues/49.
Types of changes
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Enhancement (changes that improvement of current feature or performance)
- [ ] Refactoring (changes that neither fixes a bug nor adds a feature)
- [ ] Test Case (changes that add missing tests or correct existing tests)
- [ ] Code style optimization (changes that do not affect the meaning of the code)
- [ ] Docs (changes that only update documentation)
- [ ] Chore (changes that don't modify src or test files)
Self Check before Merge
- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have read the CONTRIBUTING document.
- [ ] I have added tests to cover my changes.
- [x] All new and existing tests passed.
👋 @dancrevier
💖 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:
fix: don't overwrite prevent_default if default wasn't preventedfeat: add graph.scale() methoddocs: graph.getShortestPath is now available
Things that will help get your PR across the finish line:
- Follow the TypeScript coding style.
- Run
npm run lintlocally to catch formatting errors earlier. - Document any user-facing changes you've made.
- Include tests when adding/changing behavior.
- Include screenshots and animated GIFs whenever possible.
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
Attention: Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
Project coverage is 62.93%. Comparing base (
b751cbf) to head (38abea8). Report is 13 commits behind head on master.
| Files | Patch % | Lines |
|---|---|---|
| src/embed-webfonts.ts | 0.00% | 1 Missing :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## master #384 +/- ##
=======================================
Coverage 62.93% 62.93%
=======================================
Files 10 10
Lines 580 580
Branches 143 143
=======================================
Hits 365 365
Misses 151 151
Partials 64 64
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
I ran this branch locally, and it fixed my issue reported here for the most part. Google fonts are loading now.
The part that's not fixed is the icons, but that appears to be a separate issue.
When can we get this PR reviewed and merged in? This bug is blocking us from adopting this package.
+1
+1
+1
+1! May we have this merged? Thank you so much!
+1! May we have this merged? Thank you so much!
pls merge
+1
+1 needs this issue fixed
We need this to be merged in.
+1 pls
Come on, we need this.
Why is this not merged yet?
there is a default fix for this first get the fonts fontEmbedCss and then skipFonts
async function useHtml2Img(el: HTMLCanvasElement) {
try {
const fontEmbedCss = await getFontEmbedCSS(el);
const blob = <Blob>(
await toBlob(el, { skipFonts: true, fontEmbedCSS: fontEmbedCss })
);
if (window.saveAs) {
window.saveAs(blob, 'new.png');
} else {
saveAs(blob, 'new.png');
}
} catch (error) {
console.log('error: ', error);
}
}
there is a default fix for this first get the fonts
fontEmbedCssand thenskipFontsasync function useHtml2Img(el: HTMLCanvasElement) { try { const fontEmbedCss = await getFontEmbedCSS(el); const blob = <Blob>( await toBlob(el, { skipFonts: true, fontEmbedCSS: fontEmbedCss }) ); if (window.saveAs) { window.saveAs(blob, 'new.png'); } else { saveAs(blob, 'new.png'); } } catch (error) { console.log('error: ', error); } }
This doesn't work. getFontEmbedCSS throws the same exception.
@diadal Please can you share with us your reluctance to merge? There seems some hesitation on your part but without knowing why, we're just left not knowing what is happening and whether to persevere with this library or not.