'use strict';
const fs = require('fs');
const path = require('path');
const root = path.join(__dirname, '..');
const css = JSON.parse(fs.readFileSync(path.join(root, 'data/webref/css.json'), 'utf8'));
const commit = fs.readFileSync(path.join(root, 'data/webref/COMMIT'), 'utf8').trim();
const str = (s) => JSON.stringify(s);
const opt = (s) => (s === undefined ? 'None' : `Some(${str(s)})`);
const PROPERTY_PATCHES = {
'line-clamp': { syntax: "none | [ <integer [1,∞]> || auto || <'block-ellipsis'> ] -webkit-legacy?" },
'column-width': {
syntax: 'auto | <length [0,∞]>',
initial: 'auto',
inherited: 'no',
href: 'https://drafts.csswg.org/css-multicol-2/#cw',
},
};
const TYPE_PATCHES = {
'timeline-range-name': 'cover | contain | entry | exit | entry-crossing | exit-crossing | scroll',
'url-set': '<image-set()>',
'image': '<url> | <image()> | <image-set()> | <cross-fade()> | <element()> | <gradient> | <light-dark-image>',
'single-transition': '[ none | <single-transition-property> ] || <time [0s,∞]> || <easing-function> || <time> || <transition-behavior-value>',
};
const props = [...css.properties].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
const lines = [];
lines.push('// SPDX-License-Identifier: Apache-2.0');
lines.push('// Copyright 2026 James Conway');
lines.push('');
lines.push(`// Generated by scripts/properties.js from data/webref/css.json (${commit}).`);
lines.push('// Don\'t edit by hand.');
lines.push('');
lines.push('use super::properties::PropertyData;');
lines.push('');
lines.push('pub(crate) static PROPERTIES: &[PropertyData] = &[');
for (const raw of props) {
const p = { ...raw, ...(PROPERTY_PATCHES[raw.name] || {}) };
const inherited = p.inherited === 'yes' ? 'Some(true)' : p.inherited === 'no' ? 'Some(false)' : 'None';
lines.push(' PropertyData {');
lines.push(` name: ${str(p.name)},`);
lines.push(` syntax: ${opt(p.syntax)},`);
lines.push(` initial: ${opt(p.initial)},`);
lines.push(` inherited: ${inherited},`);
lines.push(` longhands: &[${(p.longhands || []).map(str).join(', ')}],`);
lines.push(` alias_of: ${opt(p.legacyAliasOf)},`);
lines.push(` href: ${str(p.href)},`);
lines.push(' },');
}
lines.push('];');
lines.push('');
const PATCHES = {
'blur': 'blur( <length [0,∞]>? )',
'brightness': 'brightness( [ <number [0,∞]> | <percentage [0,∞]> ]? )',
'contrast': 'contrast( [ <number [0,∞]> | <percentage [0,∞]> ]? )',
'grayscale': 'grayscale( [ <number [0,∞]> | <percentage [0,∞]> ]? )',
'invert': 'invert( [ <number [0,∞]> | <percentage [0,∞]> ]? )',
'opacity': 'opacity( [ <number [0,∞]> | <percentage [0,∞]> ]? )',
'saturate': 'saturate( [ <number [0,∞]> | <percentage [0,∞]> ]? )',
'sepia': 'sepia( [ <number [0,∞]> | <percentage [0,∞]> ]? )',
'drop-shadow': 'drop-shadow( [ <color>? && [ <length>{2} <length [0,∞]>? ] ] )',
'circle': 'circle( [ <radial-extent> | <length-percentage [0,∞]> ]? [ at <position> ]? )',
'ellipse': 'ellipse( [ <radial-extent> | <length-percentage [0,∞]> ]{0,2} [ at <position> ]? )',
'polygon': "polygon( <'fill-rule'>? [ round <length [0,∞]> ]? , [<length-percentage> <length-percentage>]# )",
};
function table(name, items, key) {
const seen = new Map();
for (const t of items) {
if (!t.syntax && !(name === 'TYPES' && TYPE_PATCHES[key(t)])) continue;
const k = key(t);
if (!seen.has(k) || (seen.get(k).for && !t.for)) seen.set(k, t);
}
const keys = [...seen.keys()].sort();
lines.push(`pub(crate) static ${name}: &[(&str, &str)] = &[`);
for (const k of keys) {
const syntax = (name === 'FUNCTIONS' && PATCHES[k]) || (name === 'TYPES' && TYPE_PATCHES[k]) || seen.get(k).syntax;
lines.push(` (${str(k)}, ${str(syntax)}),`);
}
lines.push('];');
lines.push('');
}
table('TYPES', css.types, (t) => t.name);
table('FUNCTIONS', css.functions, (t) => t.name.replace(/\(\)$/, ''));
fs.writeFileSync(path.join(root, 'src/values/generated.rs'), lines.join('\n'));
console.error(`${props.length} properties`);