Skip to content

Commit

Permalink
Merge a37babf into 5c70226
Browse files Browse the repository at this point in the history
  • Loading branch information
waterplea committed Mar 30, 2020
2 parents 5c70226 + a37babf commit 19a31ef
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import {SanitizeStyle} from '../types/sanitize-style';
import {addCSSRules} from './addCSSRules';

/**
* uponSanitizeElementHook factory to sanitize CSS rules from HTMLStyleElement through custom function
* uponSanitizeElementHook factory to sanitize CSS rules from
* HTMLStyleElement/SVGStyleElement through custom function
*/
export function createUponSanitizeElementHook(
sanitizeStyle: SanitizeStyle,
): DompurifyHook {
return node => {
if (node instanceof HTMLStyleElement || node instanceof SVGStyleElement) {
// https://github.com/microsoft/TypeScript/issues/36896
const {sheet} = node as HTMLStyleElement;
if (isStyleTag(node)) {
const {sheet} = node;

if (sheet instanceof CSSStyleSheet) {
node.textContent = addCSSRules(sheet.cssRules, sanitizeStyle).join('\n');
Expand All @@ -21,3 +21,8 @@ export function createUponSanitizeElementHook(
}
};
}

// Technically it can also be SVGStyleElement but it just complicates types and fails in SSR
function isStyleTag(node: Node): node is HTMLStyleElement {
return 'sheet' in node;
}

0 comments on commit 19a31ef

Please sign in to comment.