Skip to content

Commit b6166ee

Browse files
authored
blog: Can we have better CSS Coverage ranges, please? (#38)
1 parent 5efa288 commit b6166ee

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
title: Can we have better CSS Coverage ranges, please?
3+
excerpt: Browers often don't report atrule ranges correctly in the CSS Coverage ranges.
4+
---
5+
6+
The last couple of months I've been working hard on improving how we can [inspect](/css-coverage/) what parts of CSS are used by the browser and which parts aren't. There's some stuff I'm doing to [make](https://github.com/projectwallace/css-code-coverage) large coverage reports so you can inspect several pages in one go by combining coverage JSON files into a single one, prettifying all the CSS etc. But there's one thing that is being consistently troublesome for me:
7+
8+
**Browers often don't report atrule ranges correctly.**
9+
10+
'Often' doesn't mean 'always', but more than enough that it has bitten me more than I care to admit. It depends on where in they live and whether neighbouring rules are covered. But take this example that I've created using Playwright's [coverage API](https://playwright.dev/docs/api/class-coverage#coverage-start-css-coverage), but I've observed this in Edge/Chrome as well:
11+
12+
```html
13+
<!doctype html>
14+
<html>
15+
<head>
16+
<link rel="stylesheet" href="http://localhost/style.css" />
17+
</head>
18+
<body>
19+
<h1>Hello world</h1>
20+
<p>Text</p>
21+
</body>
22+
</html>
23+
```
24+
25+
```css
26+
/* style.css */
27+
@media all {
28+
h1 {
29+
color: green;
30+
}
31+
}
32+
@supports (display: grid) {
33+
h1 {
34+
font-size: 24px;
35+
}
36+
}
37+
```
38+
39+
Which parts do you think are marked as covered by the browser? The `@media all {}` rule? Or `@supports (display: grid) {}`. Well yes. No. Both. Neither. Let's look at what the browser/Playwright generates for this:
40+
41+
```js
42+
let coverage = [
43+
{ start: 7, end: 11 },
44+
{ start: 14, end: 37 },
45+
{ start: 50, end: 66 },
46+
{ start: 69, end: 95 }
47+
]
48+
```
49+
50+
It doesn't mark the whole stylesheet as covered. Instead there are 4 different ranges. Let's highlight the bits that the browser says are covered, leaving out tabs and newlines for readability:
51+
52+
1. `all ` (Missing `@media ` and opening `{`)
53+
2. `h1 { color: green; }`
54+
3. `(display: grid) ` (Missing `@supports ` and opening `{`)
55+
4. `h1 { font-size: 24px; }`
56+
57+
And both atrule ranges also miss their closing `}`.
58+
59+
## What I want
60+
61+
Can browsers please:
62+
63+
- Include the atrule name in the ranges
64+
- Include the opening _and_ closing brackets (`{}`) in the ranges
65+
- Be smart and then mark my example in a _single_ range covering the whole stylesheet
66+
67+
Thanks.

src/lib/components/Markdown.svelte

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,13 @@
181181
margin-right: 0.25ch;
182182
margin-left: 0.25ch;
183183
color: var(--highlight-name);
184+
background-color: var(--bg-200);
184185
}
185186
186187
:global(.markdown pre code) {
187188
color: var(--fg-200);
188189
}
189190
190-
:global(.markdown :is(p, li, figcaption) :not(pre) code::before),
191-
:global(.markdown :is(p, li, figcaption) :not(pre) code::after) {
192-
content: '`';
193-
}
194-
195191
:global(.markdown aside) {
196192
border: 2px solid var(--accent);
197193
padding: var(--space-6) var(--space-5);

src/lib/css/style.css

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -391,16 +391,12 @@
391391
line-height: calc(var(--leading-base) * 1rem);
392392
}
393393

394-
p code {
394+
:is(p, li) code {
395395
display: inline-block;
396396
margin-right: 1ch;
397397
margin-left: 1ch;
398398
color: light-dark(var(--accent-700), var(--accent-300));
399-
}
400-
401-
p code::before,
402-
p code::after {
403-
content: '`';
399+
padding-inline: .5ch;
404400
}
405401

406402
[aria-selected='true'] {

0 commit comments

Comments
 (0)