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

Extract inline SVG styles and include them in page head #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

salim-b
Copy link
Contributor

@salim-b salim-b commented Oct 31, 2023

Summary

This PR builds upon (i.e. includes) #2. The changes specific to this PR are found in 89c8d13.

Extract styles (<style> elements) from inline SVGs, write them to page store and finally include them in the page's <head>.

The new head/inline-svg-styles.html] partial must be sourced by the consumer of this Hyas integration, of course:

<head>
  ···
  {{ if templates.Exists "partials/head/inline-svg-styles.html" -}}
    {{ partial "head/inline-svg-styles.html" . }}
  {{- end }}
  ···
</head>

Motivation

Avoid having to set style-src 'unsafe-inline' CSP when an inlined SVG contains a <style> tag.

Caveats

  • There are two types of "inline styles": External ones defined via <style> tag (full CSS with selectors etc.) and ones set directly on the HTML elements to be styled (<svg>, <path>, etc.) via the style attribute. The latter ones can't be easily extracted (we would need to derive/generate proper selectors for them) and thus are left untouched by this PR.
  • Due to the way Hugo's page store works, the extraction-and-inclusion mechanism only works for inline-svg usage in .Content (shortcodes, code blocks (we could add a render hook for svg code blocks)), not when the partial is used elsewhere. As long as theme authors are aware of this (i.e. that they can't use partial "inline-svg" "some.svg" if some-svg contains inline styles), everything should be fine, though. ☺️

It should probably be mentioned in the documentation of this Hyas integration that for the attribute-based type of inline styles, style-src 'unsafe-inline' has to be set if CSP is used. However, this holds true regardless of this PR.

Checks

  • Read Create a Pull Request
  • Supports all screen sizes (if relevant)
  • Supports both light and dark mode (if relevant)
  • Passes npm run test

- Add missing closing outer `</svg>` tag.
- Set filename (prefixed with `svg-`) as `id` attribute instead of
  adding it as `class`. The `id` argument to the shortcode has
  precedence over the prefixed filename, so users can supply a custom
  `id`.
- Add support for `role` attribute via the `role` argument to the
  shortcode. Defaults to
  [`role="img"`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/img_role#svg_and_roleimg)
  if none is provided.
- Set `aria-labelledby` to`<title>` elements `id` (or the
  `ariaLabelledby` shortcode argument, if supplied).
- Add support for the
  [`<desc>` element](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/desc)
  via the `desc` arg to the shortcode and sets the the corresponding
  `aria-describedby ` to its `id` (or the `ariaDescribedby` shortcode
  argument, if supplied).
- Add `<title>` and `<desc>` elements as first/second (instead of last)
  child to the outer `<svg>` element for [backwards compatbility with
  SVG 1.1](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title).
- Fix removal of existing attributes (ensure to only remove them from
  the outer `svg` element and not its children).
- Improve other regular expressions to be more precise and avoid
  unnecessary minification.
@salim-b salim-b changed the title Styles Extract inline SVG styles and include them in page head Nov 1, 2023
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

Successfully merging this pull request may close these issues.

1 participant