Skip to content

Commit 29afbf8

Browse files
authored
Add custom property functions SQL script
1 parent 897d645 commit 29afbf8

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#standardSQL
2+
CREATE TEMPORARY FUNCTION getCustomPropertyFunctions(css JSON)
3+
RETURNS ARRAY<STRING>
4+
LANGUAGE js
5+
OPTIONS (library = "gs://httparchive/lib/css-utils.js")
6+
AS '''
7+
try {
8+
function compute(ast) {
9+
let ret = {
10+
properties: {},
11+
functions: {},
12+
supports: {},
13+
"pseudo-classes": {},
14+
fallback: {
15+
none: 0,
16+
literal: 0,
17+
var: 0
18+
},
19+
initial: 0
20+
};
21+
22+
walkRules(ast, rule => {
23+
for (let match of rule.supports.matchAll(/\\(--(?<name>[\\w-]+)\\s*:/g)) {
24+
incrementByKey(ret.supports, match.groups.name);
25+
}
26+
}, {type: "supports"});
27+
28+
let parsedSelectors = {};
29+
30+
walkDeclarations(ast, ({property, value}, rule) => {
31+
if (matches(value, /\\bvar\\(\\s*--/)) {
32+
if (!property.startsWith("--")) {
33+
incrementByKey(ret.properties, property);
34+
}
35+
36+
for (let call of extractFunctionCalls(value)) {
37+
if (call.name === "var") {
38+
let fallback = call.args.split(",").slice(1).join(",");
39+
40+
if (matches(fallback, /\\bvar\\(\\s*--/)) {
41+
ret.fallback.var++;
42+
}
43+
else if (fallback) {
44+
ret.fallback.literal++;
45+
}
46+
else {
47+
ret.fallback.none++;
48+
}
49+
}
50+
else if (call.args.includes("var(--")) {
51+
incrementByKey(ret.functions, call.name);
52+
}
53+
}
54+
}
55+
56+
if (property.startsWith("--")) {
57+
if (value === "initial") {
58+
ret.initial++;
59+
}
60+
61+
if (rule.selectors) {
62+
for (let selector of rule.selectors) {
63+
let sast = parsedSelectors[selector] = parsedSelectors[selector] || parsel.parse(selector);
64+
parsel.walk(sast, node => {
65+
if (node.type === "pseudo-class") {
66+
incrementByKey(ret["pseudo-classes"], node.name);
67+
}
68+
})
69+
}
70+
}
71+
72+
}
73+
});
74+
75+
for (let type in ret) {
76+
ret[type] = sortObject(ret[type]);
77+
}
78+
79+
return ret;
80+
}
81+
82+
let custom_property = compute(css);
83+
return Object.keys(custom_property.functions);
84+
} catch (e) {
85+
return [];
86+
}
87+
''';
88+
89+
SELECT
90+
client,
91+
function,
92+
COUNT(DISTINCT page) AS pages,
93+
total,
94+
COUNT(DISTINCT page) / total AS pct
95+
FROM (
96+
SELECT DISTINCT
97+
client,
98+
page,
99+
LOWER(function) AS function
100+
FROM
101+
`httparchive.crawl.parsed_css`
102+
LEFT JOIN
103+
UNNEST(getCustomPropertyFunctions(css)) AS function
104+
WHERE
105+
function IS NOT NULL AND
106+
date = '2025-07-01' AND
107+
rank <= 1000000 AND
108+
is_root_page -- remove if wanna look at home pages AND inner pages. Old tables only had home pages.
109+
)
110+
JOIN (
111+
SELECT
112+
client,
113+
COUNT(0) AS total
114+
FROM
115+
`httparchive.crawl.pages`
116+
WHERE
117+
date = '2025-07-01' AND
118+
rank <= 1000000 AND
119+
is_root_page -- remove if wanna look at home pages AND inner pages. Old tables only had home pages.
120+
GROUP BY
121+
client
122+
)
123+
USING (client)
124+
GROUP BY
125+
client,
126+
total,
127+
function
128+
HAVING
129+
pages >= 100
130+
ORDER BY
131+
pct DESC

0 commit comments

Comments
 (0)