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

Fixes issue fetching font css when cssRules are inaccessilbe #384

Merged
merged 1 commit into from
Feb 13, 2025

Conversation

dancrevier
Copy link
Contributor

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 #49.

Types of changes

  • 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

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

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 bubkoo#49.
@biiibooo biiibooo bot added the fix label Mar 13, 2023
@biiibooo
Copy link
Contributor

biiibooo bot commented Mar 13, 2023

👋 @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 prevented
  • feat: add graph.scale() method
  • docs: graph.getShortestPath is now available

Things that will help get your PR across the finish line:

  • Follow the TypeScript coding style.
  • Run npm run lint locally 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
Copy link

codecov bot commented Mar 13, 2023

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 51 commits behind head on master.

Files with missing lines Patch % Lines
src/embed-webfonts.ts 0.00% 1 Missing ⚠️
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           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@petrgazarov
Copy link

petrgazarov commented Mar 28, 2023

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.

@stumpykilo
Copy link

stumpykilo commented Mar 29, 2023

When can we get this PR reviewed and merged in? This bug is blocking us from adopting this package.

@homanp
Copy link

homanp commented Apr 5, 2023

+1

2 similar comments
@robertjmschmidt
Copy link

+1

@morjanmihail
Copy link

+1

@sherryysj
Copy link

+1! May we have this merged? Thank you so much!

1 similar comment
@panditlakshya
Copy link

+1! May we have this merged? Thank you so much!

@plarner30
Copy link

pls merge

@RolandoAndrade
Copy link

+1

PJM178 added a commit to PJM178/html-to-image that referenced this pull request Oct 18, 2023
@Kitenite
Copy link

+1 needs this issue fixed

@incuedAA
Copy link

We need this to be merged in.

@petrmiko
Copy link

+1 pls

@cliveportman
Copy link

Come on, we need this.

@arielzao150
Copy link

Why is this not merged yet?

@diadal
Copy link

diadal commented Feb 9, 2024

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);
  }
}

@wendellavila
Copy link

wendellavila commented Jul 8, 2024

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);
  }
}

This doesn't work. getFontEmbedCSS throws the same exception.

@cliveportman
Copy link

@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.

@bubkoo bubkoo merged commit cb65be0 into bubkoo:master Feb 13, 2025
1 of 2 checks passed
@biiibooo
Copy link
Contributor

biiibooo bot commented Feb 13, 2025

👋 @dancrevier Congrats on merging your first pull request! 🎉🎉🎉

@biiibooo
Copy link
Contributor

biiibooo bot commented Feb 14, 2025

🎉 This PR is included in version 1.11.13 🎉

The release is available on:

Your semantic-release bot 📦🚀

@biiibooo biiibooo bot added the released label Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.