|
| 1 | +# generate_definitions |
| 2 | + |
| 3 | +A Go tool that generates the CSS property/value definition files used by the |
| 4 | +`gosub_css3` crate to validate CSS declarations. |
| 5 | + |
| 6 | +The generated JSON files live in `crates/gosub_css3/resources/definitions/` and |
| 7 | +are embedded into the crate at compile time (see |
| 8 | +`src/matcher/property_definitions.rs`). They describe, for every CSS property: |
| 9 | +its value grammar (in [CSS value definition syntax](https://developer.mozilla.org/en-US/docs/Web/CSS/Value_definition_syntax)), |
| 10 | +its initial value, and whether it is inherited — plus the shared value types |
| 11 | +(`<length>`, `<color>`, …), at-rules, and selectors those grammars reference. |
| 12 | +The CSS style matcher uses this data as its validation gate when resolving |
| 13 | +declarations. |
| 14 | + |
| 15 | +## Data sources |
| 16 | + |
| 17 | +The tool merges two upstream datasets, downloaded at run time: |
| 18 | + |
| 19 | +- **[w3c/webref](https://github.com/w3c/webref)** (`ed/css/*.json` on the |
| 20 | + `curated` branch) — machine-extracted definitions from the W3C editor's |
| 21 | + draft specs: property grammars, value types, at-rules, and selectors. |
| 22 | +- **[mdn/data](https://github.com/mdn/data)** (`css/properties.json`) — used to |
| 23 | + enrich webref properties with their `computed` and `initial` metadata. |
| 24 | + |
| 25 | +Webref files are cached in a local `.css_cache/` directory (git-ignored, |
| 26 | +created next to wherever you run the tool). Cache entries are validated |
| 27 | +against the upstream git blob SHA, so a re-run only downloads files that |
| 28 | +changed upstream. The MDN dataset is currently fetched on every run. |
| 29 | + |
| 30 | +Duplicate definitions across spec files are detected and logged; when two |
| 31 | +specs disagree on a grammar the tool logs both and keeps the latest one. |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +Requires a Go toolchain (no Rust involved). From this directory: |
| 36 | + |
| 37 | +```sh |
| 38 | +go run . |
| 39 | +``` |
| 40 | + |
| 41 | +The output is written to `.output/definitions/`: |
| 42 | + |
| 43 | +- `definitions.json` — everything in a single file |
| 44 | +- `definitions_properties.json`, `definitions_values.json`, |
| 45 | + `definitions_at-rules.json`, `definitions_selectors.json` — the same data |
| 46 | + split per category (this is what the crate embeds) |
| 47 | + |
| 48 | +Which of the two forms is emitted is controlled by the `exportType` constant |
| 49 | +in `main.go` (default: both). Output is sorted by name so regeneration |
| 50 | +produces minimal diffs. |
| 51 | + |
| 52 | +To update the definitions the engine actually uses, copy the generated files |
| 53 | +over the checked-in ones and review the diff: |
| 54 | + |
| 55 | +```sh |
| 56 | +cp .output/definitions/definitions*.json ../../resources/definitions/ |
| 57 | +``` |
| 58 | + |
| 59 | +Then run the `gosub_css3` tests — the property definitions are exercised by |
| 60 | +the matcher tests: |
| 61 | + |
| 62 | +```sh |
| 63 | +cargo test -p gosub_css3 |
| 64 | +``` |
| 65 | + |
| 66 | +## Patching upstream data |
| 67 | + |
| 68 | +Sometimes the upstream data is wrong or incompatible with our matcher. The |
| 69 | +`patch` package can apply `.patch` files to the cached webref JSON before it |
| 70 | +is parsed, and keeps an index (`.css_cache/index/cache_index.json`) of which |
| 71 | +patches have been applied to which files so they are not applied twice. |
| 72 | +Custom patches go in `.css_cache/patches/`. Note that patch application is |
| 73 | +not currently invoked from `main.go` (`DownloadPatches` is commented out); |
| 74 | +the machinery is kept for when spec data needs local fixes again. |
| 75 | + |
| 76 | +## Package layout |
| 77 | + |
| 78 | +- `main.go` — orchestrates the merge and writes the output JSON |
| 79 | +- `webref/` — downloads, caches, and parses the webref CSS spec files; |
| 80 | + normalizes grammars (e.g. quoting literal parentheses) and merges |
| 81 | + duplicates across specs |
| 82 | +- `mdn/` — fetches MDN's `properties.json` |
| 83 | +- `patch/` — patch application and cache-index bookkeeping (currently unused) |
| 84 | +- `specs/` — fetches the webref spec index (currently unused; previously used |
| 85 | + to filter to W3C specs only) |
| 86 | +- `utils/` — shared types, GitHub blob SHA hashing, cache constants |
0 commit comments