Search
HomeGet startedDevelopStylesComponents
Develop index

Markup and sanitizing

PublishedUpdated

Icons and content options are markup: the library writes them as HTML. Text options, such as a button's text, are never HTML. Most apps have nothing to do:

Where the markup comes from What to do
Your own code: icons you import, templates you wrote Nothing
A choice a user or a content system makes Name your icons
Markup a user or a content system writes Set a policy
Any, on a page that enforces Trusted Types Set a policy

Name your icons

Store a name, not the markup, and look it up among your own icons. No one else's markup reaches the page, so there is nothing to sanitize:

import { createButton } from 'material';

const icons = new Map([
  ['save', '<svg viewBox="0 0 24 24"><path d="M5 5h14v14H5z"/></svg>'],
  ['send', '<svg viewBox="0 0 24 24"><path d="M4 4l16 8-16 8z"/></svg>'],
]);

// From a user or a content system: text only.
const choice = { icon: 'send', label: 'Send' };

const button = createButton({ text: choice.label, icon: icons.get(choice.icon) ?? '' });
document.body.append(button.element);

Set a policy

When the markup itself comes from someone else, clean it before it is written. configureHTML takes a function from markup to markup. material does not include a sanitizer: use one that is kept up to date, such as DOMPurify.

import DOMPurify from 'dompurify';
import { configureHTML } from 'material';

configureHTML({ sanitize: (html) => DOMPurify.sanitize(html) });

The browser's own setHTML does not fit here: it is safe where it writes, but a policy returns a string that is parsed again, and it is not in every browser.

Under require-trusted-types-for 'script' a plain string throws, so return a TrustedHTML from a policy your content security policy allows:

const policy = window.trustedTypes.createPolicy('app', {
  createHTML: (html) => DOMPurify.sanitize(html),
});
configureHTML({ sanitize: (html) => policy.createHTML(html) });

Set it before the first component. It applies to factories, web components and server rendering alike. Without one, markup is written as it is.

What reaches it

  • Every string that is not empty, the library's own icons included.
  • Any other value given where markup is expected is turned into text once, then passed through it.
  • null, undefined and '' clear the element and do not reach it.
  • A TrustedHTML that the page's trustedTypes.isHTML recognises, and whatever the policy returns, is written as it is. An object that only looks like one is not trusted.

The policy covers markup that arrives as data, not scripts already running in the page or in its frames.

Attributes that are markup

Never put a user's text in one of these without a policy. An avatar is markup too: not a person's name, nor an image's address.

Element Attributes that are markup
<m-button> icon
<m-extended-fab> icon
<m-fab> icon
<m-icon-button> icon, selected-icon (while toggle and selected)
<m-navigation-rail> expand-icon (while the rail is collapsed), collapse-icon (while the rail is expanded)
<m-search> leading-icon, trailing-icon, avatar (not a person's name or an image URL)
<m-slider> icon, inset-icon (size M, L or XL, not a range or centred slider, when the track can hold it), inset-icon-at-min (the same, while the value is at the minimum)
<m-split-button> icon
<m-switch> icon
<m-text-field> leading-icon, trailing-icon

On the items inside a component:

Element Attributes that are markup
<m-button-group-item> icon, selected-icon (an icon-only item, while selected)
<m-chip> icon, trailing-icon, avatar (input chips only, and it takes precedence over icon; not a person's name or an image URL)
<m-navigation-drawer-item> icon, selected-icon (while that destination is active)
<m-list-item> leading-icon, leading-avatar (not a person's name or an image URL), trailing-icon
<m-menu-item> icon
<m-navigation-bar-item> icon, selected-icon (while that destination is active)
<m-navigation-rail-item> icon, selected-icon (while that destination is active)
<m-search-suggestion> icon
<m-select-option> icon (written into the menu)
<m-tab> icon

The FAB menu's icon, and its items', is markup as well. That menu is not rendered on the server, where the attribute stays escaped; the policy applies when it upgrades.

The scaffold on the server

renderScaffold's children go through the policy. Its trustedChildren option does not: it is written as given, so it is only for markup your app wrote. See Scaffold.