niklasvh / html2canvas

Screenshots with JavaScript
https://html2canvas.hertzen.com/
MIT License
30.47k stars 4.8k forks source link

Font families being incorrectly identified. #3160

Open tascord opened 6 months ago

tascord commented 6 months ago

Please make sure you are testing with the latest release of html2canvas. Old versions are not supported and issues reported for them will be closed.

Please follow the general troubleshooting steps first:

Bug reports:

The !important CSS selector seems to cause unexpected issues when h2c decides which font should be used. Fiddle.

When using the following selector to apply a font to all but font-awesome's icons, h2c fails to correctly identify which font should be used to render the icon's <i/> tags. This is in contrast to how the DOM correctly applies fonts.

*:not(i) {
    font-family: "Montserrat", sans-serif !important;
}

Html2Canvas Generated Image without !important

Html2Canvas Generated Image with !important

Specifications:

tascord commented 6 months ago

https://github.com/niklasvh/html2canvas/blob/6020386bbeed60ad68e675fdcaa6220e292fd35a/src/dom/node-parser.ts#L22

The issue seems to be related to the fact that parent element, a <HTML2CANVASPSEUDOELEMENT/> fails to inherit correct styles that are then parsed to the TextContainer when the !important keyword is present.