Skip to content
Permalink
Browse files Browse the repository at this point in the history
filter out unsupported at-rules
  • Loading branch information
mat-sz committed Jun 22, 2022
1 parent f2c9e13 commit 96d3dfe
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
55 changes: 55 additions & 0 deletions __tests__/sanitize.test.ts
Expand Up @@ -295,4 +295,59 @@ b {background: red;}</style>`,
).toBe(`<style>a {background: red;}
b {background: red;}</style>`);
});

it('removes @keyframes rules from CSS', () => {
expect(
sanitize(
`<style>@keyframes test {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}</style>`,
'',
{ noWrapper: true, preserveCssPriority: false }
)
).toBe(`<style></style>`);
});

it('removes @supports rules from CSS', () => {
expect(
sanitize(
`<style>@supports (display: grid) {
div {
display: grid;
}
}</style>`,
'',
{ noWrapper: true, preserveCssPriority: false }
)
).toBe(`<style></style>`);
});

it('removes @font-face rules from CSS', () => {
expect(
sanitize(
`<style>@font-face {
font-family: "Open Sans";
src: url("/fonts/OpenSans-Regular-webfont.woff2") format("woff2"),
url("/fonts/OpenSans-Regular-webfont.woff") format("woff");
}</style>`,
'',
{ noWrapper: true, preserveCssPriority: false }
)
).toBe(`<style></style>`);
});

it('removes @import rules from CSS', () => {
expect(
sanitize(`<style>@import 'custom.css';</style>`, '', {
noWrapper: true,
preserveCssPriority: false,
})
).toBe(`<style></style>`);
});
});
4 changes: 3 additions & 1 deletion src/index.ts
Expand Up @@ -307,7 +307,9 @@ function sanitizeHtml(
rewriteExternalResources
);
newRules.push(rule);
} else if ('cssRules' in rule) {
} else if ('cssRules' in rule && 'media' in rule) {
// According to https://www.caniemail.com/,
// out of all at-rules, Gmail only supports @media.
const mediaRule = (rule as any) as CSSMediaRule;
const newRulesMedia: CSSRule[] = [];

Expand Down

0 comments on commit 96d3dfe

Please sign in to comment.