Skip to content

Commit fbb8326

Browse files
authored
fix: correctly handle CSS layers (#123)
1 parent 032f2ba commit fbb8326

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

packages/beasties/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ export default class Beasties {
549549
}
550550

551551
// keep font rules, they're handled in the second pass:
552-
if (rule.type === 'atrule' && rule.name === 'font-face')
552+
if (rule.type === 'atrule' && (rule.name === 'font-face' || rule.name === 'layer'))
553553
return
554554

555555
// If there are no remaining rules, remove the whole rule:

packages/beasties/test/__snapshots__/beasties.test.ts.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ exports[`beasties > skip invalid path 1`] = `
124124
"
125125
`;
126126
127+
exports[`beasties > works with at-rules (@layer) 1`] = `
128+
"<html data-beasties-container>
129+
<head>
130+
<style>@layer foo, bar;@layer foo {h1{color:red}}@layer bar {}</style><link rel="preload" href="/style.css" as="style">
131+
</head>
132+
<body>
133+
<h1>Hello World!</h1>
134+
<link rel="stylesheet" href="/style.css"></body>
135+
</html>"
136+
`;
137+
127138
exports[`beasties > works with pseudo classes and elements 1`] = `
128139
"<html data-beasties-container>
129140
<head>

packages/beasties/test/beasties.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,51 @@ describe('beasties', () => {
298298
expect(result).toContain('<link rel="stylesheet" href="/style.css">')
299299
expect(result).toMatchSnapshot()
300300
})
301+
302+
it('works with at-rules (@layer)', async () => {
303+
const logger: Logger = {
304+
warn: () => {},
305+
info: () => {},
306+
error: () => {},
307+
debug: () => {},
308+
}
309+
310+
const beasties = new Beasties({
311+
reduceInlineStyles: false,
312+
path: '/',
313+
logLevel: 'warn',
314+
logger,
315+
})
316+
const assets: Record<string, string> = {
317+
'/style.css': trim`
318+
@layer foo, bar;
319+
320+
@layer foo {
321+
h1 { color: red }
322+
h4 { background: blue; }
323+
}
324+
325+
@layer bar {
326+
h4 { background: lime; }
327+
}
328+
`,
329+
}
330+
331+
const loggerWarnSpy = vi.spyOn(logger, 'warn')
332+
beasties.readFile = filename => assets[filename.replace(/^\w:/, '').replace(/\\/g, '/')]!
333+
const result = await beasties.process(trim`
334+
<html>
335+
<head>
336+
<link rel="stylesheet" href="/style.css">
337+
</head>
338+
<body>
339+
<h1>Hello World!</h1>
340+
</body>
341+
</html>
342+
`)
343+
expect(loggerWarnSpy).not.toHaveBeenCalled()
344+
expect(result).toContain('<style>@layer foo, bar;@layer foo {h1{color:red}}@layer bar {}</style>')
345+
expect(result).toContain('<link rel="stylesheet" href="/style.css">')
346+
expect(result).toMatchSnapshot()
347+
})
301348
})

0 commit comments

Comments
 (0)