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

mj-font only emits the last style sheet for the font family #2849

Open
jlarmstrongiv opened this issue Apr 3, 2024 · 1 comment
Open

mj-font only emits the last style sheet for the font family #2849

jlarmstrongiv opened this issue Apr 3, 2024 · 1 comment

Comments

@jlarmstrongiv
Copy link

Describe the bug
mj-font is a smart component. Perhaps too smart in some cases.

I am using fontsource to host fonts on my local website. Here is an example package:

Fontsource packages for all fonts on Google Fonts, plus others fonts. They are popular for self-hosting fonts.

However, the problem with fontsource, typefaces, and other npm packages that allow local installation is that they bundle their CSS files by weight, italics, etc. without having an all or complete file for all font weights, italics, etc. So, to render noto sans with italics, you need two stylesheets:

import '@fontsource-variable/noto-sans/wght-italic.css';
import '@fontsource-variable/noto-sans';

This conflicts with mj-font, which seems to expect the name attribute to be unique. If two mj-font tags have the same name, then only the last stylesheet is emitted. Plus, if a suffix is added to the name attribute to make it unique, that font tag output will be omitted because it must be used somewhere in the email as a fontFamily attribute.

To Reproduce
Steps to reproduce the behavior:
See https://mjml.io/try-it-live/xuL4GmhZOp

Code sample

MJML

<mjml>
  <mj-head>
    <!-- Google fonts used for demonstration purposed -->
    <!-- Different font names used to represent different font family groups, likea stylesheet for italics or a stylesheet condensed -->
    
    <!-- the first declaration is not emitted -->
    <mj-font name="Raleway"
      href="https://fonts.googleapis.com/css?family=Raleway" />
    <!-- this second declartion is emitted successfully -->
    <mj-font name="Raleway"
      href="https://fonts.googleapis.com/css?family=Nunito" />
    <!-- trying to include the font under a different name is not emitted, as it is not referenced in the email -->
    <mj-font name="Raleway with italics"
      href="https://fonts.googleapis.com/css?family=Lato" />

  </mj-head>
  <mj-body>
    <mj-section>
      <mj-column>

        <mj-text font-size="20px" color="#F45E43" font-family="Raleway">Hello World</mj-text>

      </mj-column>
    </mj-section>
  </mj-body>
</mjml>

Compiled HTML output

<!doctype html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"><head><title></title><!--[if !mso]><!--><meta http-equiv="X-UA-Compatible" content="IE=edge"><!--<![endif]--><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><style type="text/css">#outlook a { padding:0; }
          body { margin:0;padding:0;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%; }
          table, td { border-collapse:collapse;mso-table-lspace:0pt;mso-table-rspace:0pt; }
          img { border:0;height:auto;line-height:100%; outline:none;text-decoration:none;-ms-interpolation-mode:bicubic; }
          p { display:block;margin:13px 0; }</style><!--[if mso]>
        <noscript>
        <xml>
        <o:OfficeDocumentSettings>
          <o:AllowPNG/>
          <o:PixelsPerInch>96</o:PixelsPerInch>
        </o:OfficeDocumentSettings>
        </xml>
        </noscript>
        <![endif]--><!--[if lte mso 11]>
        <style type="text/css">
          .mj-outlook-group-fix { width:100% !important; }
        </style>
        <![endif]--><!--[if !mso]><!--><link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet" type="text/css"><style type="text/css">@import url(https://fonts.googleapis.com/css?family=Nunito);</style><!--<![endif]--><style type="text/css">@media only screen and (min-width:480px) {
        .mj-column-per-100 { width:100% !important; max-width: 100%; }
      }</style><style media="screen and (min-width:480px)">.moz-text-html .mj-column-per-100 { width:100% !important; max-width: 100%; }</style><style type="text/css"></style><!-- Google fonts used for demonstration purposed --><!-- Different font names used to represent different font family groups, likea stylesheet for italics or a stylesheet condensed --><!-- the first declaration is not emitted --><!-- this second declartion is emitted successfully --><!-- trying to include the font under a different name is not emitted, as it is not referenced in the email --></head><body style="word-spacing:normal;"><div><!--[if mso | IE]><table align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]--><div style="margin:0px auto;max-width:600px;"><table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;"><tbody><tr><td style="direction:ltr;font-size:0px;padding:20px 0;text-align:center;"><!--[if mso | IE]><table role="presentation" border="0" cellpadding="0" cellspacing="0"><tr><td class="" style="vertical-align:top;width:600px;" ><![endif]--><div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;"><table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%"><tbody><tr><td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;"><div style="font-family:Raleway;font-size:20px;line-height:1;text-align:left;color:#F45E43;">Hello World</div></td></tr></tbody></table></div><!--[if mso | IE]></td></tr></table><![endif]--></td></tr></tbody></table></div><!--[if mso | IE]></td></tr></table><![endif]--></div></body></html>

Expected behavior
Defining mj-font with two stylesheets for the same font family name should emit two stylesheet references

MJML environment (please complete the following information):

  • OS: All
  • MJML Version: 4.15.3
  • MJML tool used: Try it live, mjml-react

Email sending environment(for rendering issues):

  • N/A

Affected email clients (for rendering issues):

  • Email Client: any that support fonts
  • OS: All
  • Browser: All
@iRyusa
Copy link
Member

iRyusa commented Apr 3, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants