Reapply "(breaking) nuke css dynamic variables and flatten css` and rule` into the same function"

This reverts commit b8c80905f0.
This commit is contained in:
CoolElectronics 2024-04-08 19:41:17 -04:00
parent b8c80905f0
commit a60d4fb5c4
No known key found for this signature in database
GPG key ID: F63593D168636C50

View file

@ -1,28 +1,14 @@
Object.assign(window, { css, rule, styled: { new: css, rule: rule } }) Object.assign(window, { css, rule: css, styled: { new: css, rule: css } })
const cssmap = {} const cssmap = {}
function scopify_css(uid, css) { export function css(strings, ...values) {
const virtualDoc = document.implementation.createHTMLDocument('') let str = ''
const virtualStyleElement = document.createElement('style') for (let f of strings) {
virtualDoc.body.appendChild(virtualStyleElement) str += f + (values.shift() || '')
let cssParsed = ''
virtualStyleElement.textContent = css
//@ts-ignore
for (const rule of virtualStyleElement.sheet.cssRules) {
rule.selectorText = rule.selectorText.includes('self')
? `.${uid}.self${rule.selectorText.replace('self', '')}`
: `.${uid} ${rule.selectorText}`
cssParsed += `${rule.cssText}\n`
} }
return cssParsed let cached = cssmap[str]
} if (cached) return cached
function tagcss(strings, values, isblock) {
let cached = cssmap[strings[0]]
let cachable = strings.length == 1
if (cachable && cached) return cached
const uid = `dl${Array(5) const uid = `dl${Array(5)
.fill(0) .fill(0)
.map(() => { .map(() => {
@ -31,46 +17,10 @@ function tagcss(strings, values, isblock) {
.join('')}` .join('')}`
const styleElement = document.createElement('style') const styleElement = document.createElement('style')
document.head.appendChild(styleElement) document.head.appendChild(styleElement)
const flattened_template = [] styleElement.textContent = `.${uid} { ${str}; }`
for (const i in strings) { cssmap[str] = uid
flattened_template.push(strings[i])
if (values[i]) {
const prop = values[i]
if (isDLPtr(prop)) {
const current_i = flattened_template.length
let oldparsed
handle(prop, (val) => {
flattened_template[current_i] = String(val)
let parsed = flattened_template.join('')
if (parsed != oldparsed)
if (isblock)
styleElement.textContent = scopify_css(uid, parsed)
else styleElement.textContent = `.${uid} { ${parsed}; }`
oldparsed = parsed
})
} else {
flattened_template.push(String(prop))
}
}
}
if (isblock) {
styleElement.textContent = scopify_css(uid, flattened_template.join(''))
} else {
styleElement.textContent = `.${uid} { ${flattened_template.join('')}; }`
}
if (cachable) cssmap[strings[0]] = uid
return uid return uid
} }
function rule(strings, ...values) {
return tagcss(strings, values, false)
}
function css(strings, ...values) {
return tagcss(strings, values, true)
}