Skip to content

Commit 45222b0

Browse files
authored
Add cssmerge post-processor to consolidate CSS extracts (#1849)
This adds a cssmerge post-processor at the crawl level that consolidates definitions from CSS extracts into a single file. The CSS entries in the resulting file are de-duplicated. The module starts with a number of comments that detail the approach. The intent is to create a view similar to that offered in MDN data to address w3c/webref#1519 Comments at the top of the module also detail the main differences between the two views. Main one is structural: we tend to use arrays in Webref so this code also produces arrays, while MDN data uses indexed objects. It's easy to build an indexed view from an array though. Consolidation focuses on syntaxes and the code does not attempt to compute the `units` list that exists in MDN data. It is easy to build that list by looking at values of a handful of CSS properties. We could add it later on to the consolidated file if that proves needed. A couple of remaining open questions: 1. This creates a `css.json` file, similar to the `events.json` file that we create when we post-process events. That's nice but it would be good to ship that file in `@webref/css` in the end. That package already has a `CSS.json` file at the root. How/Where to package the `css.json` file? 2. To de-duplicate, the most recent definition is used. That's the only approach I can think of that can be automated. Consumers of MDN data may need a more nuanced approach where the syntax better matches what core browsers currently support. We can perhaps refine the process later on if needed with some sort of patching mechanism (but my hunch is that this will remain a manual process).
1 parent dd68bd1 commit 45222b0

File tree

4 files changed

+952
-0
lines changed

4 files changed

+952
-0
lines changed

schemas/postprocessing/css.json

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
{
2+
"$schema": "http://json-schema.org/schema#",
3+
"$id": "https://github.com/w3c/reffy/blob/main/schemas/postprocessing/css.json",
4+
5+
"type": "object",
6+
"additionalProperties": false,
7+
"required": ["atrules", "functions", "properties", "selectors", "types"],
8+
"properties": {
9+
"atrules": {
10+
"type": "array",
11+
"items": {
12+
"type": "object",
13+
"required": ["name", "descriptors"],
14+
"additionalProperties": false,
15+
"properties": {
16+
"name": { "type": "string", "pattern": "^@" },
17+
"href": { "$ref": "../common.json#/$defs/url" },
18+
"value": { "$ref": "../common.json#/$defs/cssValue" },
19+
"prose": { "type": "string" },
20+
"descriptors": {
21+
"type": "array",
22+
"items": {
23+
"type": "object",
24+
"required": ["name", "for"],
25+
"additionalProperties": true,
26+
"properties": {
27+
"name": { "type": "string" },
28+
"for": { "type": "string" },
29+
"href": { "$ref": "../common.json#/$defs/url" },
30+
"value": { "$ref": "../common.json#/$defs/cssValue" }
31+
}
32+
}
33+
}
34+
}
35+
}
36+
},
37+
"functions": {
38+
"type": "array",
39+
"items": {
40+
"type": "object",
41+
"required": ["name", "type"],
42+
"additionalProperties": false,
43+
"properties": {
44+
"name": { "type": "string", "pattern": "^<[^>]+>$|^.*()$" },
45+
"for": { "type": "string" },
46+
"href": { "$ref": "../common.json#/$defs/url" },
47+
"type": { "type": "string", "enum": ["function"] },
48+
"prose": { "type": "string" },
49+
"value": { "$ref": "../common.json#/$defs/cssValue" }
50+
}
51+
}
52+
},
53+
"properties": {
54+
"type": "array",
55+
"items": {
56+
"type": "object",
57+
"additionalProperties": true,
58+
"required": ["name"],
59+
"properties": {
60+
"name": { "$ref": "../common.json#/$defs/cssPropertyName" },
61+
"href": { "$ref": "../common.json#/$defs/url" },
62+
"value": { "$ref": "../common.json#/$defs/cssValue" },
63+
"legacyAliasOf": { "$ref": "../common.json#/$defs/cssPropertyName" },
64+
"styleDeclaration": {
65+
"type": "array",
66+
"items": { "type": "string" },
67+
"minItems": 1
68+
}
69+
}
70+
}
71+
},
72+
"selectors": {
73+
"type": "array",
74+
"items": {
75+
"type": "object",
76+
"required": ["name"],
77+
"additionalProperties": false,
78+
"properties": {
79+
"name": { "$ref": "../common.json#/$defs/cssPropertyName" },
80+
"href": { "$ref": "../common.json#/$defs/url" },
81+
"prose": { "type": "string" },
82+
"value": { "$ref": "../common.json#/$defs/cssValue" }
83+
}
84+
}
85+
},
86+
"types": {
87+
"type": "array",
88+
"items": {
89+
"type": "object",
90+
"required": ["name", "type"],
91+
"additionalProperties": false,
92+
"properties": {
93+
"name": { "type": "string", "pattern": "^<[^>]+>$|^.*()$" },
94+
"for": { "type": "string" },
95+
"href": { "$ref": "../common.json#/$defs/url" },
96+
"type": { "type": "string", "enum": ["type"] },
97+
"prose": { "type": "string" },
98+
"value": { "$ref": "../common.json#/$defs/cssValue" }
99+
}
100+
}
101+
}
102+
}
103+
}

src/lib/post-processor.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import path from 'node:path';
5252
import { pathToFileURL } from 'node:url';
5353
import { createFolderIfNeeded, shouldSaveToFile } from './util.js';
5454
import csscomplete from '../postprocessing/csscomplete.js';
55+
import cssmerge from '../postprocessing/cssmerge.js';
5556
import events from '../postprocessing/events.js';
5657
import idlnames from '../postprocessing/idlnames.js';
5758
import idlparsed from '../postprocessing/idlparsed.js';
@@ -64,6 +65,7 @@ import patchdfns from '../postprocessing/patch-dfns.js';
6465
*/
6566
const modules = {
6667
csscomplete,
68+
cssmerge,
6769
events,
6870
idlnames,
6971
idlparsed,

0 commit comments

Comments
 (0)