Skip to content

Add inline styles for custom fonts#3345

Merged
swissspidy merged 4 commits intodevelopfrom
fix/3344-font-styles
Sep 26, 2019
Merged

Add inline styles for custom fonts#3345
swissspidy merged 4 commits intodevelopfrom
fix/3344-font-styles

Conversation

@swissspidy
Copy link
Copy Markdown
Collaborator

Fixes issue for fonts not originating from Google Fonts.

Fixes #3344.

Fixes issue for fonts not originating from Google Fonts
@swissspidy swissspidy added the Needs Testing Issues that need to be confirmed. label Sep 25, 2019
@googlebot googlebot added the cla: yes Signed the Google CLA label Sep 25, 2019
@swissspidy swissspidy changed the title Add inline styles for custom fonts [WIP] Add inline styles for custom fonts Sep 25, 2019
@miina
Copy link
Copy Markdown
Contributor

miina commented Sep 25, 2019

Seems to work as expected. It's marked WIP right now -- what's still todo -- was it this?
// @todo Prevent adding duplicates!

@swissspidy
Copy link
Copy Markdown
Collaborator Author

According to Weston's comment above the // @todo Prevent adding duplicates! is inaccurate and this should happen already. I haven't tested that yet though.

Also, this method doesn't seem to have any tests at all, which is a bit worrisome.

@westonruter westonruter added this to the v1.3 milestone Sep 25, 2019
@westonruter
Copy link
Copy Markdown
Member

This is working for me.

Given this story page, with three system fonts being used (including 3 Text blocks with the same font):

image

Before

image

<!--
The style[amp-custom] element is populated with:
     0 B: link#wp-block-library-css[rel=stylesheet][href=https://wordpressdev.lndo.site/content/plugins/gutenberg/build/block-library/style.css?ver=1569279408][media=all]
   160 B (19%): link#amp-stories-css[rel=stylesheet][href=https://wordpressdev.lndo.site/content/plugins/amp/assets/css/amp-stories.css?ver=1.3.1-alpha][media=all]
   289 B (22%): link#amp-stories-frontend-css[rel=stylesheet][href=https://wordpressdev.lndo.site/content/plugins/amp/assets/css/amp-stories-frontend.css?ver=1.3.1-alpha][media=]
    84 B: amp-story-page#11fc4530-aa8d-4ad2-a037-65e0269c4bdf.wp-block-amp-amp-story-page amp-wp-74885c3
   123 B: div.amp-story-block-wrapper amp-wp-7b64e67
   123 B: div.amp-story-block-wrapper amp-wp-5a4df0a
   128 B: div.amp-story-block-wrapper amp-wp-cd08bbf
   128 B: div.amp-story-block-wrapper amp-wp-5d86f2f
   123 B: div.amp-story-block-wrapper amp-wp-97fdaf8
    75 B: h1.wp-block-amp-amp-story-text amp-wp-f7dbba7[data-font-family=Arial Narrow]
Total included size: 1,233 bytes (8% of 15,402 total after tree shaking)
-->

After

image

<!--
The style[amp-custom] element is populated with:
     0 B: link#wp-block-library-css[rel=stylesheet][href=https://wordpressdev.lndo.site/content/plugins/gutenberg/build/block-library/style.css?ver=1569279408][media=all]
   160 B (19%): link#amp-stories-css[rel=stylesheet][href=https://wordpressdev.lndo.site/content/plugins/amp/assets/css/amp-stories.css?ver=1.3.1-alpha][media=all]
   289 B (22%): link#amp-stories-frontend-css[rel=stylesheet][href=https://wordpressdev.lndo.site/content/plugins/amp/assets/css/amp-stories-frontend.css?ver=1.3.1-alpha][media=]
    89 B: style[data-font-family=Arial]
   108 B: style[data-font-family=Arial Black]
    82 B: style[data-font-family=Arial Narrow]
    84 B: amp-story-page#11fc4530-aa8d-4ad2-a037-65e0269c4bdf.wp-block-amp-amp-story-page amp-wp-74885c3
   123 B: div.amp-story-block-wrapper amp-wp-7b64e67
   123 B: div.amp-story-block-wrapper amp-wp-5a4df0a
   128 B: div.amp-story-block-wrapper amp-wp-cd08bbf
   128 B: div.amp-story-block-wrapper amp-wp-5d86f2f
   123 B: div.amp-story-block-wrapper amp-wp-97fdaf8
    75 B: h1.wp-block-amp-amp-story-text amp-wp-f7dbba7[data-font-family=Arial Narrow]
Total included size: 1,512 bytes (9% of 15,681 total after tree shaking)
-->

Note that even though there are 5 Text blocks being used, with 3 of them using the same font (Arial Black), the manifest shows that only one copy is actually included after the style sanitizer processes the stylesheets. This is confirmed by looking at the CSS, where there are only 3 rules with [data-font-family] selectors:

/* ... */
[data-font-family="Arial"] {
	font-family: "Arial", "Helvetica Neue", "Helvetica", "sans-serif"
}

[data-font-family="Arial Black"] {
	font-family: "Arial Black", "Arial Black", "Arial Bold", "Gadget", "sans-serif"
}

[data-font-family="Arial Narrow"] {
	font-family: "Arial Narrow", "Arial", "sans-serif"
}
/* ... */

@westonruter
Copy link
Copy Markdown
Member

I realized this could be simplified a bit more and harmonize style rule creation for system vs Google Fonts, and as a bonus it ensures that if by chance someone enqueues a Google Font early, it won't fail to get the inline style.

In d29bfe0, a page with 4 fonts being used (3 system and 1 Google Font):

image

<!--
The style[amp-custom] element is populated with:
     0 B: link#wp-block-library-css[rel=stylesheet][href=https://wordpressdev.lndo.site/content/plugins/gutenberg/build/block-library/style.css?ver=1569279408][media=all]
   160 B (19%): link#amp-stories-css[rel=stylesheet][href=https://wordpressdev.lndo.site/content/plugins/amp/assets/css/amp-stories.css?ver=1.3.1-alpha][media=all]
   289 B (22%): link#amp-stories-frontend-css[rel=stylesheet][href=https://wordpressdev.lndo.site/content/plugins/amp/assets/css/amp-stories-frontend.css?ver=1.3.1-alpha][media=]
    89 B: style[data-font-family=Arial]
   108 B: style[data-font-family=Arial Black]
    82 B: style[data-font-family=Arial Narrow]
    65 B: style[data-font-family=Aref Ruqaa]
    84 B: amp-story-page#11fc4530-aa8d-4ad2-a037-65e0269c4bdf.wp-block-amp-amp-story-page amp-wp-74885c3
   123 B: div.amp-story-block-wrapper amp-wp-7b64e67
   123 B: div.amp-story-block-wrapper amp-wp-5a4df0a
   128 B: div.amp-story-block-wrapper amp-wp-cd08bbf
   128 B: div.amp-story-block-wrapper amp-wp-5d86f2f
   123 B: div.amp-story-block-wrapper amp-wp-97fdaf8
   126 B: div.amp-story-block-wrapper amp-wp-1a8febe
    75 B: h1.wp-block-amp-amp-story-text amp-wp-f7dbba7[data-font-family=Aref Ruqaa]
Total included size: 1,703 bytes (10% of 15,872 total after tree shaking)
--><style amp-custom="">
/*...*/
[data-font-family="Arial"]{font-family:"Arial","Helvetica Neue","Helvetica","sans-serif"}
[data-font-family="Arial Black"]{font-family:"Arial Black","Arial Black","Arial Bold","Gadget","sans-serif"}
[data-font-family="Arial Narrow"]{font-family:"Arial Narrow","Arial","sans-serif"}
[data-font-family="Aref Ruqaa"]{font-family:"Aref Ruqaa","serif"}
/*...*/
</style>
<!-- ... -->
<link crossorigin="anonymous" rel="stylesheet" id="aref-ruqaa-font-css" href="https://fonts.googleapis.com/css?family=Aref%20Ruqaa%3A400%2C700&amp;subset=latin%2Clatin-ext" media="all">

@westonruter
Copy link
Copy Markdown
Member

Also, this method doesn't seem to have any tests at all, which is a bit worrisome.

Added some tests in 2d089c4.

@westonruter westonruter changed the title [WIP] Add inline styles for custom fonts Add inline styles for custom fonts Sep 25, 2019
@swissspidy
Copy link
Copy Markdown
Collaborator Author

Thanks a ton @westonruter! Works like a charm now 🎉

@swissspidy swissspidy merged commit b0bcd20 into develop Sep 26, 2019
@swissspidy swissspidy deleted the fix/3344-font-styles branch September 26, 2019 08:35
swissspidy added a commit that referenced this pull request Sep 26, 2019
* Fixes issue for fonts not originating from Google Fonts
* Simplifies font style rule generation
* Adds test for `AMP_Story_Post_Type::render_block_with_google_fonts`
westonruter added a commit that referenced this pull request Oct 1, 2019
* tag '1.3.0': (318 commits)
  Bump 1.3.0
  Add inline styles for custom fonts (#3345)
  Limit deeply-nesting test to 200 to fix Xdebug error (#3341)
  Bump 1.3-RC2 (#3335)
  Sanitize invalid children of amp-story and amp-story-page elements to prevent white story of death (#3336)
  Remove unused Travis deploy stage (#3340)
  Implement automated accessibility testing using Axe (#3294)
  Only add all Google Font style rules in editor context
  Prevent adding AMP query var to Story URLs in Compatibility Tool
  Prevent attempting to redirect Stories with rejected validation errors
  Ensure all AMP scripts (including v0.js) get moved to the head
  Make sure that media picker is background types are filter correctly.
  Normalize style[type] attribute quote style after r46164 in WP core
  Fix phpunit covers tags
  Bump version to 1.3-RC1
  Strip 100% width/height from layout=fill elements
  Fix issue with cut (#3246)
  Remove unused Google Fonts SVGs (#3289)
  Fix resize for non-fit text box (#3259)
  Use template_dir consistently as signal for transitional mode
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes Signed the Google CLA Needs Testing Issues that need to be confirmed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inline styles not added for blocks with custom font

4 participants