Converting CSS to Javascript

xinjs does a really nice job of generating CSS from Typescript / Javascript code, but
I've written a lot of CSS both before I started working on xinjs and while I was working
on it but before the css module was mature. Notably, xinjs-ui's styles were in css.
Converting CSS into xinjs's XinStyleSheet form is a bit of a pain, and it should
be easy to automate, so I knocked up this conversion tool as a "live example" in the
xinjs-ui documentation.
I thought it would be a good test of live examples in the xinie context, so here it is.
const tabs = preview.querySelector('xin-tabs')
const [css, js] = preview.querySelectorAll('xin-code')
const convertButton = preview.querySelector('button')
function quoteTrim(s, symbol = false) {
s = s.trim()
if (s.match(/[^\w_]/) || !symbol) {
s = s.replace(/'/g, "\\'")
return `'${s}'`
} else {
return s
}
}
function kebabToCamel(s) {
s = s.replace(/--/, '_')
return s.replace(/\-(\w)/g, (_, c) => c.toLocaleUpperCase())
}
function css2js () {
const source = css.value
const lines = source.split('\n')
const output = ['{']
let rule = ''
for(const line of lines) {
if (!line.trim()) {
continue
}
try {
rule = rule ? rule + ' ' + line.trim() : line
if (rule.match(/@import .*;/)) {
const [,url] = rule.match(/@import url\(['"](.*)['"]\);/)
output.push(`'@import': ${quoteTrim(url)},`)
rule = ''
} else if (rule.match(/\{\s*$/)) {
const [,whitespace, selector] = rule.match(/(\s*)([^\s].*)\{/)
output.push(`${whitespace}${quoteTrim(selector, true)}: {`)
rule = ''
} else if (line.match(/[^\s]*\}\s*$/)) {
output.push(line + ',')
rule = ''
} else if (rule.match(/.*:.*;/)) {
let [,whitespace, prop, value] = rule.match(/(\s*)(.*):(.*);/)
prop = kebabToCamel(prop)
output.push(`${whitespace}${quoteTrim(prop, true)}: ${quoteTrim(value)},`)
rule = ''
}
} catch(e) {
console.error(e, line)
}
}
output.push('}')
let code = output.join('\n')
code = code.replace(/'var\(--([^)]*)\)'/g, (_,v) => `vars.${kebabToCamel(v)}`)
js.value = `import { vars } from 'xinjs'\n\nexport const styleSpec = ${code}`
}
convertButton.addEventListener('click', () => {
css2js()
tabs.value = 1
})
<xin-tabs>
<button slot="after-tabs">Convert</button>
<xin-code mode="css" name="css">
@import url('https://fonts.googleapis.com/css2?family=Aleo:ital,wght@0,100..900;1,100..900&famiSpline+Sans+Mono:ital,wght@0,300..700;1,300..700&display=swap');
tr:nth-child(2n) {
background: var(--background-shaded);
}
th,
td {
padding: calc(var(--spacing) * 0.5) var(--spacing);
}
@keyframes fade-in {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
header xin-locale-picker xin-select button {
--brand-color: var(--brand-text-color);
background: transparent;
gap: 2px;
}
header xin-locale-picker xin-select button svg {
fill: var(--brand-text-color) !important;
}
</xin-code>
<xin-code mode="javascript" name="js"></xin-code>
</xin-tabs>
.preview xin-tabs {
background: var(--panel-bg);
}
.preview button {
margin: 4px;
}
.preview xin-tabs, .preview textarea, .preview xin-code {
width: 100%;
height: 100%;
resize: none;
}