-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathct.css
282 lines (195 loc) · 5.66 KB
/
ct.css
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*!==========================================================================
#CT.CSS
========================================================================== */
/*!
* ct.css – Let’s take a look inside your <head>…
*
* © Harry Roberts 2021 – twitter.com/csswizardry
*/
/**
* It’s slightly easier to remember topics than it is colours. Set up some
* custom properties for use later on.
*/
head {
--ct-is-problematic: solid;
--ct-is-affected: dashed;
--ct-notify: #0bce6b;
--ct-warn: #ffa400;
--ct-error: #ff4e42;
}
/**
* Show the <head> and set up the items we might be interested in.
*/
head,
head script,
head script:not([src])[async],
head script:not([src])[defer],
head style, head [rel="stylesheet"],
head script ~ meta[http-equiv="content-security-policy"],
head > meta[charset]:not(:nth-child(-n+5)) {
display: block;
}
head script,
head style, head [rel="stylesheet"],
head title,
head script ~ meta[http-equiv="content-security-policy"],
head > meta[charset]:not(:nth-child(-n+5)) {
margin: 5px;
padding: 5px;
border-width: 5px;
background-color: white;
color: #333;
}
head ::before,
head script, head style {
font: 16px/1.5 monospace, monospace;
display: block;
}
head ::before {
font-weight: bold;
}
/**
* External Script and Style
*/
head script[src],
head link[rel="stylesheet"] {
border-style: var(--ct-is-problematic);
border-color: var(--ct-warn);
}
head script[src]::before {
content: "[Blocking Script – " attr(src) "]"
}
head link[rel="stylesheet"]::before {
content: "[Blocking Stylesheet – " attr(href) "]"
}
/**
* Inline Script and Style.
*/
head style:not(:empty),
head script:not(:empty) {
max-height: 5em;
overflow: auto;
background-color: #ffd;
white-space: pre;
border-color: var(--ct-notify);
border-style: var(--ct-is-problematic);
}
head script:not(:empty)::before {
content: "[Inline Script] ";
}
head style:not(:empty)::before {
content: "[Inline Style] ";
}
/**
* Blocked Title.
*
* These selectors are generally more complex because the Key Selector (`title`)
* depends on the specific conditions of preceding JS--we can’t cast a wide net
* and narrow it down later as we can when targeting elements directly.
*/
head script[src]:not([async]):not([defer]):not([type=module]) ~ title,
head script:not(:empty) ~ title {
display: block;
border-style: var(--ct-is-affected);
border-color: var(--ct-error);
}
head script[src]:not([async]):not([defer]):not([type=module]) ~ title::before,
head script:not(:empty) ~ title::before {
content: "[<title> blocked by JS] ";
}
/**
* Blocked Scripts.
*
* These selectors are generally more complex because the Key Selector
* (`script`) depends on the specific conditions of preceding CSS--we can’t cast
* a wide net and narrow it down later as we can when targeting elements
* directly.
*/
head [rel="stylesheet"]:not([media="print"]):not(.ct) ~ script,
head style:not(:empty) ~ script {
border-style: var(--ct-is-affected);
border-color: var(--ct-warn);
}
head [rel="stylesheet"]:not([media="print"]):not(.ct) ~ script::before,
head style:not(:empty) ~ script::before {
content: "[JS blocked by CSS – " attr(src) "]";
}
/**
* Using both `async` and `defer` is redundant (an anti-pattern, even). Let’s
* flag that.
*/
head script[src][src][async][defer] {
display: block;
border-style: var(--ct-is-problematic);
border-color: var(--ct-warn);
}
head script[src][src][async][defer]::before {
content: "[async and defer is redundant: prefer defer – " attr(src) "]";
}
/**
* Async and defer simply do not work on inline scripts. It won’t do any harm,
* but it’s useful to know about.
*/
head script:not([src])[async],
head script:not([src])[defer] {
border-style: var(--ct-is-problematic);
border-color: var(--ct-warn);
}
head script:not([src])[async]::before {
content: "The async attribute is redundant on inline scripts"
}
head script:not([src])[defer]::before {
content: "The defer attribute is redundant on inline scripts"
}
/**
* Third Party blocking resources.
*
* Expect false-positives here… it’s a crude proxy at best.
*
* Selector-chaining (e.g. `[src][src]`) is used to bump up specificity.
*/
head script[src][src][src^="//"],
head script[src][src][src^="http"],
head [rel="stylesheet"][href^="//"],
head [rel="stylesheet"][href^="http"] {
border-style: var(--ct-is-problematic);
border-color: var(--ct-error);
}
head script[src][src][src^="//"]::before,
head script[src][src][src^="http"]::before {
content: "[Third Party Blocking Script – " attr(src) "]";
}
head [rel="stylesheet"][href^="//"]::before,
head [rel="stylesheet"][href^="http"]::before {
content: "[Third Party Blocking Stylesheet – " attr(href) "]";
}
/**
* Mid-HEAD CSP disables the Preload Scanner
*/
head script ~ meta[http-equiv="content-security-policy"] {
border-style: var(--ct-is-problematic);
border-color: var(--ct-error);
}
head script ~ meta[http-equiv="content-security-policy"]::before {
content: "[Meta CSP defined after JS]"
}
/**
* Charset should appear as early as possible
*/
head > meta[charset]:not(:nth-child(-n+5)) {
border-style: var(--ct-is-problematic);
border-color: var(--ct-warn);
}
head > meta[charset]:not(:nth-child(-n+5))::before {
content: "[Charset should appear as early as possible]";
}
/**
* Hide all irrelevant or non-matching scripts and styles (including ct.css).
*
* We’re done!
*/
link[rel="stylesheet"][media="print"],
link[rel="stylesheet"].ct, style.ct,
script[async], script[defer], script[type=module] {
display: none;
}