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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dancrevier
Copy link

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

vivcat 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

Patch and project coverage have no change.

Comparison is base (b751cbf) 62.93% compared to head (38abea8) 62.93%.

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           
Impacted Files Coverage Δ
src/embed-webfonts.ts 30.63% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

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

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.

None yet