gitoria

kunibert

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit7480a98d7480a98dkunibert.service: SIGTERM stop is a clean exitKunibert7480a98d/plugins/web/css.hl

24.8 KB

  1. // The framework's STYLESHEET — the Style convention of COMPONENTS §4, as the
  2. // archived hl:web implemented it (archive/hlweb-v1/plugins-web/web_css.hl), re-read
  3. // 2026-09-12 after a version built from the doc alone got most of it wrong.
  4. //
  5. // STRUCTURE FROM THE SYNTAX, VALUES FROM THE INSTANCE. A hybrid's keys come back
  6. // alphabetical, so the ORDER of rules, declarations and media conditions is read
  7. // off the syntax (hlSyntax entries are authored order) while every VALUE is read
  8. // off the constructed instance at the same path — the language has already folded
  9. // the statics (`background = dark`) and merged `base + { … }` by then.
  10. //
  11. // WHAT A RULE NAME MEANS (the archive's `placesFor`, per file):
  12. // `@root` / `':root'` a CONTRIBUTION: collected from every file into ONE
  13. // `:root { }` at the top of the sheet, in order
  14. // `'@media …'` / `@media` a BLOCK of rules: collected at the END of the sheet,
  15. // (`@supports`, the same shape and place for the other two queries
  16. // `@container`) (ticket #66, proposal 5)
  17. // one `@media` per distinct query, first-seen order;
  18. // `@media { minWidth = 960 … }` composes the query from
  19. // conditions (number → px, string verbatim); any number of
  20. // different bare ones may stand in one block (read off the
  21. // syntax, see SYNTAX), inside a rule the feature-named scalars
  22. // are the conditions (hybriel#257)
  23. // `'@keyframes name'` an ANIMATION: its stops (`'from'`, `to`, `'50%'`) with
  24. // their declarations, emitted where it stands (ticket #66, 4)
  25. // selector-shaped verbatim (`'.icon'`, `'#page-anchor'`, `'a:hover'`)
  26. // referenced `Style.x` the class the View wrote (tag-prefix strip): `.x`, or
  27. // `tag.stripped`
  28. // `#x` id the View writes `id = 'x'` on an element: `#x` (R3)
  29. // `#x` local otherwise matched by NAME to this file's elements `x` (and
  30. // a composed child's root for `#Child`): `tag.<file class>`
  31. // own-name a rule named like an element of this file's View (or
  32. // body/html): the TAG selector, no class
  33. // anything else DEAD: a build error naming the file and the rule
  34. // In a STYLES FILE (root members, no View) a bare name is a tag, kebab-cased.
  35. // NESTED under a rule, a name of PSEUDOS is that rule's pseudo-class, not a
  36. // descendant: `a { hover { … } }` → `a:hover` (ticket #66, proposal 1).
  37. // A rule with no declarations and nested rules is a CONTAINER: not emitted.
  38. // Two rules emitting one selector with different bodies: refused.
  39. import HlwCssVar from './cssvar.hl'
  40. static NL = "
  41. "
  42. static SELECTOR_CHARS = ":-.#>[]()*+~,/ %@=^|!"
  43. static HEADER = "/* GENERATED by hl:web — ONE global stylesheet.
  44. Components author their Style rules per file; only the rules a View actually
  45. REFERENCES are emitted (dead rules are dropped and listed at boot). Class names
  46. follow the tag-prefix strip rule: rule `headerMain` referenced inside `header`
  47. emits class=\"main\" under the TAG-QUALIFIED selector `header.main`; under any
  48. other tag it keeps `.headerMain`. */
  49. "
  50. Boolean minify = false
  51. view = null // the View walk: kebab, className, localClass live there
  52. gen = 0 // the analysis generation the syntax handles belong to
  53. // per sheet
  54. parts = []
  55. seen = {}
  56. dead = []
  57. mediaOrder = []
  58. mediaParts = []
  59. rootDecls = []
  60. // ---- names ----------------------------------------------------------------------
  61. // ---- css variables (creator's ruling 2026-09-13) --------------------------------
  62. // `bg = var('#FFF')` declares a custom property as a VALUE; the walker names it after
  63. // its member, writes it into the sheet's one `:root`, and rewrites every use.
  64. static counter = { n = 0 }
  65. // `var('#FFF', dark = '#000')` carries its dark-scheme value too (ticket #66,
  66. // proposal 2): the sheet writes it into `:root` under `(prefers-color-scheme: dark)`
  67. static var = (value, dark = null) => {
  68. counter.n = counter.n + 1
  69. return new HlwCssVar(value = value, id = counter.n, dark = dark)
  70. }
  71. static DARK = '@media (prefers-color-scheme: dark)'
  72. // ---- units as functions (ticket #66, proposal 3) ---------------------------------
  73. // a number is emitted unitless as written; `rem(1.5)`, `px(12)`, `pct(50)` answer the
  74. // TEXT with its unit — '1.5rem', '12px', '50%' — so a value says what it measures
  75. static rem = (n) => { return '' + n + 'rem' }
  76. static px = (n) => { return '' + n + 'px' }
  77. static pct = (n) => { return '' + n + '%' }
  78. vars = {} // id → { name value } for the sheet being built
  79. varNames = {} // name → true
  80. isVar(v) { return v != null && hlTypeName(v) == 'Instance' }
  81. // the variables a file's root members declare, named after the member
  82. collectVars(f) {
  83. for (r of fileRules(f)) {
  84. if (!r.isBlock && isVar(r.value)) {
  85. r.value.name = r.key
  86. vars['' + r.value.id] = { name = r.key; value = r.value.value; }
  87. varNames[r.key] = true
  88. }
  89. }
  90. // a token the file INHERITS: its own instance's copy, under the parent's name (ticket #39)
  91. if (f.aliases != null) {
  92. for (a of f.aliases) {
  93. a.value.name = a.key
  94. vars['' + a.value.id] = { name = a.key; value = a.value.value; }
  95. }
  96. }
  97. return null
  98. }
  99. // a value's text with every variable reference resolved: a variable itself →
  100. // var(--name); a composed string carries placeholders var(--hlv<id>) → var(--name)
  101. varText(v) {
  102. if (isVar(v)) {
  103. let d = vars['' + v.id]
  104. if (d == null) { hlError("a css variable is used before any styles file declared it as a root member — `name = var(…)` at the root of the styles file") }
  105. return 'var(--' + view.kebab(d.name) + ')'
  106. }
  107. let s = '' + v
  108. if (s.indexOf('var(--hlv') < 0) { return s }
  109. for (id of vars.keys()) { s = s.replaceAll('var(--hlv' + id + ')', 'var(--' + view.kebab(vars[id].name) + ')') }
  110. return s
  111. }
  112. // the pseudo-classes a nested entry may name, camelCase as written (kebab-cased on emit)
  113. static PSEUDOS = ['hover', 'focus', 'focusVisible', 'focusWithin', 'active', 'visited', 'target',
  114. 'disabled', 'enabled', 'checked', 'required', 'optional', 'valid', 'invalid', 'readOnly',
  115. 'placeholderShown', 'firstChild', 'lastChild', 'onlyChild', 'firstOfType', 'lastOfType', 'empty']
  116. isPseudo(name) { return PSEUDOS.includes(name) }
  117. // the selectors a nested entry composes under its enclosing rule's `sels`: a
  118. // pseudo-class sticks to each (`a:hover`), anything else is a descendant (`a span`)
  119. nestedSels(f, sels, key) {
  120. let out = []
  121. if (isPseudo(key)) {
  122. for (p of sels) { out.push(p + ':' + view.kebab(key)) }
  123. return out
  124. }
  125. let kidSels = selectorsFor(f, key)
  126. // a tag that only a COMPOSED component's View renders (hybriel#243): `'.bar' { 'img' { … } }`
  127. // is `.bar img` when Child, composed inside `.bar`, has the img
  128. if (kidSels.length == 0 && f.deepTags != null && f.deepTags.includes(key)) { kidSels = [view.domTag(key)] }
  129. for (p of sels) { for (s of kidSels) { out.push(p + ' ' + s) } }
  130. return out
  131. }
  132. isKeyframes(name) { return name == '@keyframes' || name.startsWith('@keyframes ') }
  133. isRootBlock(name) { return name == '@root' || name == ':root' }
  134. // THE THREE QUERIES share one shape: `@media`, `@supports`, `@container` — bare with
  135. // conditions as scalars, or spelled out in the key
  136. static QUERIES = ['@media', '@supports', '@container']
  137. queryKind(name) {
  138. for (k of QUERIES) {
  139. if (name == k || name.startsWith(k + ' ') || name.startsWith(k + '(')) { return k }
  140. }
  141. return ''
  142. }
  143. isMediaBlock(name) { return queryKind(name) != '' }
  144. // THE FEATURES a bare query inside a rule reads as conditions, camelCase as written.
  145. // `width`, `height`, `color` and `grid` are also declarations and stay declarations
  146. // there: write `minWidth` / `maxWidth` (and `minHeight`, `maxHeight`) for a size.
  147. static MEDIA_FEATURES = ['aspectRatio', 'orientation', 'resolution', 'pointer', 'hover', 'anyPointer', 'anyHover',
  148. 'prefersColorScheme', 'prefersReducedMotion', 'prefersContrast', 'prefersReducedData', 'prefersReducedTransparency',
  149. 'displayMode', 'overflowBlock', 'overflowInline', 'update', 'scan', 'colorGamut', 'colorIndex', 'monochrome',
  150. 'forcedColors', 'invertedColors', 'scripting', 'videoDynamicRange', 'dynamicRange']
  151. static RANGE_FEATURES = ['width', 'height', 'aspectRatio', 'resolution', 'color', 'colorIndex', 'monochrome']
  152. static CONTAINER_FEATURES = ['inlineSize', 'blockSize', 'aspectRatio', 'orientation']
  153. isFeature(kind, name) {
  154. if (kind == '@media') {
  155. if (MEDIA_FEATURES.includes(name)) { return true }
  156. if (name.length > 3 && (name.startsWith('min') || name.startsWith('max'))) {
  157. rest = name.slice(3, name.length)
  158. low = rest.charAt(0).toLowerCase() + rest.slice(1, rest.length)
  159. return RANGE_FEATURES.includes(low)
  160. }
  161. return false
  162. }
  163. if (kind == '@container') {
  164. if (CONTAINER_FEATURES.includes(name)) { return true }
  165. if (name.length > 3 && (name.startsWith('min') || name.startsWith('max'))) {
  166. rest = name.slice(3, name.length)
  167. low = rest.charAt(0).toLowerCase() + rest.slice(1, rest.length)
  168. return ['width', 'height', 'inlineSize', 'blockSize', 'aspectRatio'].includes(low)
  169. }
  170. }
  171. return false
  172. }
  173. mediaLiteral(name) { return name.slice(queryKind(name).length, name.length).trim() }
  174. localName(name) { return name.startsWith('#') ? name.slice(1) : '' }
  175. selectorShaped(name) {
  176. if (name.length == 0) { return false }
  177. if ('0123456789'.indexOf(name.charAt(0)) >= 0) { return true }
  178. let i = 0
  179. while (i < name.length) {
  180. if (SELECTOR_CHARS.indexOf(name.charAt(i)) >= 0) { return true }
  181. i = i + 1
  182. }
  183. return false
  184. }
  185. // ---- one block's syntax, with its values --------------------------------------
  186. // the entries of a hybrid node: [{ key node value isBlock }] in authored order,
  187. // values read off `hybrid` (the instance's evaluated member at the same path)
  188. entriesOf(key, node, hybrid) {
  189. let out = []
  190. let n = node == 0 ? null : hlSyntax(gen, key, node)
  191. if (n == null || n.tag != 'object_expression') {
  192. // A MERGED RULE (`extR = baseR + { top = 'auto' }`, COMPONENTS §4: "Style gets it
  193. // for free") has no literal to read: its entries are the merged VALUE's, and a
  194. // nested block is read the same way. It used to read as no entries at all, so the
  195. // View wrote the class and the sheet carried no rule for it (mission 322 row 9).
  196. if (isHybridValue(hybrid)) {
  197. for (k of hybrid.keys()) { out.push({ key = k; node = 0; value = hybrid[k]; isBlock = isHybridValue(hybrid[k]); }) }
  198. }
  199. return out
  200. }
  201. own = hybrid == SYNTAX
  202. for (h of n.entries) {
  203. let e = hlSyntax(gen, key, h)
  204. if (e != null && e.tag == 'object_property') {
  205. let v = hlSyntax(gen, key, e.value)
  206. if (own) {
  207. out.push(syntaxEntry(key, e, v))
  208. } else {
  209. val = hybrid != null ? hybrid[e.key] : null
  210. // A static hybrid has no syntax block here; its evaluated value is the rule (W219, #253).
  211. out.push({ key = e.key; node = e.value; value = val; isBlock = (v != null && v.tag == 'object_expression') || isHybridValue(val); })
  212. }
  213. }
  214. }
  215. return markRepeated(key, out)
  216. }
  217. // THE VALUES OF A BLOCK THAT THE INSTANCE CANNOT HOLD (hybriel#257). Two members of one
  218. // name are one member in the constructed instance, so a query written twice
  219. // (`@media { minWidth … }` and `@media { maxWidth … }`) cannot be read from it by name.
  220. // Every occurrence of such a name is marked SYNTAX: its entries, at any depth, are read
  221. // off the source in place, where each block has its own position. Only a literal can be
  222. // read that way (a string, a number, a boolean); a name or a call inside such a block
  223. // is a located error that says what to do.
  224. static SYNTAX = '[read from the syntax]'
  225. syntaxEntry(key, e, v) {
  226. if (v != null && v.tag == 'object_expression') { return { key = e.key; node = e.value; value = SYNTAX; isBlock = true; } }
  227. if (v == null || v.tag != 'literal') {
  228. hlError("'" + e.key + "' in " + key + " sits in a query that is written more than once in one block, and such a block's values are read off the source, where only a literal (a string or a number) can be read. Write '" + e.key + "' as a literal, or give the queries distinct keys ('@media (min-width: 60rem)' = { … })")
  229. }
  230. let val = v.text
  231. if (v.kind == 'number') { val = toNumber(v.text) }
  232. if (v.kind == 'boolean') { val = v.text == 'true' }
  233. return { key = e.key; node = e.value; value = val; isBlock = false; }
  234. }
  235. // a query name that occurs more than once among the entries of one block: every
  236. // occurrence is read from the syntax (see SYNTAX)
  237. markRepeated(key, entries) {
  238. count = {}
  239. for (e of entries) {
  240. if (isMediaBlock(e.key)) { count[e.key] = count[e.key] == null ? 1 : count[e.key] + 1 }
  241. }
  242. out = []
  243. for (e of entries) {
  244. if (count[e.key] != null && count[e.key] > 1 && e.value != SYNTAX) {
  245. sn = hlSyntax(gen, key, e.node)
  246. if (sn == null || sn.tag != 'object_expression') {
  247. hlError("'" + e.key + "' in " + key + " is written more than once and one of them is not a block literal — write each as { … } or give the queries distinct keys")
  248. }
  249. out.push({ key = e.key; node = e.node; value = SYNTAX; isBlock = true; })
  250. } else {
  251. out.push(e)
  252. }
  253. }
  254. return out
  255. }
  256. // a hybrid VALUE (a rule), as opposed to a scalar declaration or a var() token
  257. isHybridValue(v) { return v != null && hlTypeName(v) == 'Hybrid' }
  258. // a value as CSS text: a string as written, a number unitless, a static already
  259. // folded, a css variable (or a string composed with one) resolved to var(--name)
  260. text(v) {
  261. return varText(v)
  262. }
  263. declLine(k, v) {
  264. if (minify) { return view.kebab(k) + ':' + text(v) + ';' }
  265. return ' ' + view.kebab(k) + ': ' + text(v) + ';' + NL
  266. }
  267. // a media condition: `minWidth = 960` → `(min-width: 960px)`, a string verbatim
  268. condition(k, v) {
  269. if (hlTypeName(v) == 'Number') { return '(' + view.kebab(k) + ': ' + v + 'px)' }
  270. return '(' + view.kebab(k) + ': ' + v + ')'
  271. }
  272. // the whole prelude, `@media (a) and (b)`: an enclosing query of the SAME kind is
  273. // joined with `and`; one of another kind cannot be, in a sheet written flat
  274. queryText(kind, outer, literal, conds, name) {
  275. let parts = []
  276. if (outer != '') {
  277. if (!outer.startsWith(kind + ' ')) { hlError("'" + name + "' stands inside '" + outer + "' — two kinds of query cannot be joined into one; write the block beside it, not inside") }
  278. parts.push(outer.slice(kind.length + 1, outer.length))
  279. }
  280. if (literal != '') { parts.push(literal) }
  281. for (c of conds) { parts.push(c) }
  282. let out = ''
  283. for (p of parts) { out = out == '' ? p : out + ' and ' + p }
  284. return kind + ' ' + out
  285. }
  286. // ---- where a rule lands (the archive's placesFor) -------------------------------
  287. // `f` is the file block: { key cls refs tags hasView isStyles }; refs are what the
  288. // View wrote ([{ tag rule }]), tags the element names the file's own View renders.
  289. selectorsFor(f, name) {
  290. if (f.isStyles) {
  291. if (selectorShaped(name)) { return [name] }
  292. return [view.domTag(name)]
  293. }
  294. let local = localName(name)
  295. // the View writes `id = 'x'`: `#x` is that id, nothing else (ticket #51, R3)
  296. if (local != '' && f.ids != null && f.ids.includes(view.kebab(local))) { return [view.kebab(name)] }
  297. if (local != '' && !selectorShaped(local)) {
  298. let out = []
  299. for (r of f.refs) {
  300. if (r.rule == name) {
  301. let sel = view.domTag(r.tag) + '.' + view.localClass(f.key)
  302. if (!out.includes(sel)) { out.push(sel) }
  303. }
  304. }
  305. return out
  306. }
  307. if (selectorShaped(name)) { return [name] }
  308. let classes = []
  309. for (r of f.refs) {
  310. if (r.rule == name) {
  311. let cls = view.className(name, r.tag)
  312. if (!classes.includes(cls)) { classes.push(cls) }
  313. }
  314. }
  315. if (classes.length == 0) {
  316. if (f.tags.includes(name) || name == 'body' || name == 'html') { return [view.domTag(name)] }
  317. return []
  318. }
  319. let out = []
  320. for (cls of classes) {
  321. if (cls == name) { out.push('.' + view.kebab(cls)) }
  322. else { out.push(view.domTag(name.slice(0, name.length - cls.length)) + '.' + view.kebab(cls)) }
  323. }
  324. return out
  325. }
  326. // ---- emission -------------------------------------------------------------------
  327. putRule(f, name, selector, body, query) {
  328. let first = seen[query + '|' + selector]
  329. if (first != null) {
  330. if (first.body == body) { return null }
  331. let msg = "style rule '" + selector + "' is emitted twice with different bodies — '" + first.rule + "' in " + first.file + " and '" + name + "' in " + f.key + " both emit that selector; put the declarations in ONE rule, or rename one of them"
  332. // printed first: an app's own `on Error` that ends the start with exit(1) takes the error and its text with it (hybriel#247)
  333. console.log('error: ' + msg)
  334. hlError(msg)
  335. }
  336. seen[query + '|' + selector] = { body = body; rule = name; file = f.key; }
  337. if (query == '') {
  338. parts.push(minify ? selector + '{' + body + '}' : NL + selector + ' {' + NL + body + '}' + NL)
  339. } else {
  340. mediaPut(query, selector, body)
  341. }
  342. return null
  343. }
  344. mediaPut(query, selector, body) {
  345. if (!mediaOrder.includes(query)) { mediaOrder.push(query) }
  346. // inside a media block every line is indented two spaces (the archive's goldens)
  347. let inner = minify ? selector + '{' + body + '}' : ' ' + selector + ' {' + NL + ' ' + body.replace(NL + ' ', NL + ' ') + ' }' + NL
  348. mediaParts.push({ query = query; text = inner; })
  349. return null
  350. }
  351. // ONE RULE: its own declarations under `sels`, its nested rules composed below it
  352. // (descendant), its media blocks under the query; a container emits nothing itself
  353. emitRule(f, name, sels, entries, query) {
  354. let body = ''
  355. let kids = []
  356. let medias = []
  357. for (e of entries) {
  358. if (e.isBlock) {
  359. if (isMediaBlock(e.key)) { medias.push(e) } else { kids.push(e) }
  360. } else if (e.value != null) {
  361. body = body + declLine(e.key, e.value)
  362. }
  363. }
  364. if (body != '') {
  365. for (sel of sels) { putRule(f, name, sel, body, query) }
  366. }
  367. for (k of kids) {
  368. if (isKeyframes(k.key)) { hlError("'" + k.key + "' inside '" + name + "' in " + f.key + " — an animation is not a rule's child: write it at the top of the Style or of the styles file") }
  369. let composed = nestedSels(f, sels, k.key)
  370. if (composed.length == 0) {
  371. dead.push(f.key + ' ' + f.cls + '.' + name + ' ' + k.key)
  372. } else {
  373. emitRule(f, name + ' ' + k.key, composed, entriesOf(f.key, k.node, k.value), query)
  374. }
  375. }
  376. for (m of medias) {
  377. // a query inside a query of the SAME kind joins with `and` (queryText refuses another kind)
  378. mediaBlock(f, m.key, m.node, m.value, sels, name, query)
  379. }
  380. return null
  381. }
  382. // ONE `@media` BLOCK, at the top of a Style (prefixes empty) or inside a rule
  383. // (prefixes = the enclosing rule's selectors): scalar entries are CONDITIONS
  384. // (only for the bare `@media` spelling), block entries are RULES under the query;
  385. // inside a rule, scalar entries are the ENCLOSING RULE's declarations under it.
  386. mediaBlock(f, name, node, hybrid, prefixes, outer, outerQuery) {
  387. let literal = mediaLiteral(name)
  388. let conds = []
  389. let own = ''
  390. let rules = []
  391. let overrides = ''
  392. for (e of entriesOf(f.key, node, hybrid)) {
  393. if (e.isBlock) { rules.push(e) }
  394. // a css VARIABLE re-assigned under the query: `bg = '#000'` inside
  395. // `@media { prefersColorScheme = 'dark' … }` is `:root { --bg: #000 }` there
  396. else if (prefixes.length == 0 && varNames[e.key] != null) { overrides = overrides + declLine('--' + view.kebab(e.key), e.value) }
  397. else if (literal == '' && prefixes.length == 0) { conds.push(condition(e.key, e.value)) }
  398. // INSIDE A RULE a bare query's scalars are its conditions when they name a feature
  399. // (`pointer = 'coarse'`, `minWidth = 960`) and the rule's declarations otherwise
  400. else if (literal == '' && isFeature(queryKind(name), e.key)) { conds.push(condition(e.key, e.value)) }
  401. else if (prefixes.length > 0) { own = own + declLine(e.key, e.value) }
  402. else { hlError("'" + name + "' already spells its query out, so '" + e.key + "' is a declaration directly inside an at-rule — put it in a rule inside the block") }
  403. }
  404. if (prefixes.length > 0 && literal == '' && conds.length == 0) {
  405. hlError("'" + name + "' inside '" + outer + "' in " + f.key + " names no condition. Inside a rule a bare query takes its conditions as scalars named after a feature (`pointer = 'coarse'`, `minWidth = 960`; `width` and `height` alone are declarations, write `minWidth`), or spell it out: '" + queryKind(name) + " (min-width: 60rem)' = { … }")
  406. }
  407. let q = queryText(queryKind(name), outerQuery, literal, conds, name)
  408. if (overrides != '') { putRule(f, name, ':root', overrides, q) }
  409. if (own != '') { for (sel of prefixes) { putRule(f, outer, sel, own, q) } }
  410. for (r of rules) {
  411. if (isMediaBlock(r.key)) { mediaBlock(f, r.key, r.node, r.value, prefixes, outer, q) }
  412. else if (isKeyframes(r.key)) { hlError("'" + r.key + "' inside '" + name + "' in " + f.key + " — an animation is not scoped by a query: write it at the top of the Style or of the styles file") }
  413. else {
  414. let composed = prefixes.length > 0 ? nestedSels(f, prefixes, r.key) : selectorsFor(f, r.key)
  415. if (composed.length == 0) { dead.push(f.key + ' ' + f.cls + '.' + r.key + ' (in ' + name + ')') }
  416. else { emitRule(f, r.key, composed, entriesOf(f.key, r.node, r.value), q) }
  417. }
  418. }
  419. return null
  420. }
  421. // a file's rules in authored order: [{ key node value isBlock }] — a component's
  422. // Style entries, or a styles file's root members (its inherits' first)
  423. fileRules(f) {
  424. return f.rules
  425. }
  426. // ONE `'@keyframes name' = { 'from' = { … } to { … } '50%' = { … } }`: every entry is
  427. // a stop, its key the stop's selector verbatim, its entries declarations
  428. keyframes(f, r) {
  429. let name = r.key.slice(10, r.key.length).trim()
  430. if (name == '') { hlError("'@keyframes' in " + f.key + " names no animation — write the name into the key: '@keyframes fade' = { 'from' = { opacity = 0 } to { opacity = 1 } }") }
  431. let body = ''
  432. for (st of entriesOf(f.key, r.node, r.value)) {
  433. if (!st.isBlock) { hlError("'" + st.key + "' in '" + r.key + "' (" + f.key + ") is a declaration, and an animation holds stops — 'from', to, '50%' — each a block of declarations") }
  434. let decls = ''
  435. for (d of entriesOf(f.key, st.node, st.value)) {
  436. if (d.isBlock) { hlError("'" + d.key + "' in the stop '" + st.key + "' of '" + r.key + "' (" + f.key + ") is a block — a stop holds declarations only") }
  437. if (d.value != null) { decls = decls + (minify ? declLine(d.key, d.value) : ' ' + declLine(d.key, d.value)) }
  438. }
  439. body = body + (minify ? st.key + '{' + decls + '}' : ' ' + st.key + ' {' + NL + decls + ' }' + NL)
  440. }
  441. putRule(f, r.key, '@keyframes ' + name, body, '')
  442. return null
  443. }
  444. // ---- the sheet ------------------------------------------------------------------
  445. // `files` are the blocks in cascade order: the styles file (with its inherits
  446. // walked first by the caller), then every served component.
  447. sheet(files) {
  448. parts = []
  449. seen = {}
  450. dead = []
  451. mediaOrder = []
  452. mediaParts = []
  453. rootDecls = []
  454. vars = {}
  455. varNames = {}
  456. for (f of files) { collectVars(f) }
  457. // the declared variables come first in :root, in declaration order; their dark
  458. // values open the dark-scheme query with a :root of their own (an explicit
  459. // `@media { prefersColorScheme = 'dark' … }` re-assignment lands after it and wins)
  460. let darkBody = ''
  461. for (f of files) {
  462. for (r of fileRules(f)) {
  463. if (!r.isBlock && isVar(r.value)) {
  464. rootDecls.push(declLine('--' + view.kebab(r.key), r.value.value))
  465. if (r.value.dark != null) { darkBody = darkBody + declLine('--' + view.kebab(r.key), r.value.dark) }
  466. }
  467. }
  468. }
  469. if (darkBody != '') { mediaPut(DARK, ':root', darkBody) }
  470. for (f of files) {
  471. for (r of fileRules(f)) {
  472. if (!r.isBlock) {
  473. // a scalar root member of a styles file is not a rule (a variable was taken above)
  474. } else if (isMediaBlock(r.key)) {
  475. mediaBlock(f, r.key, r.node, r.value, [], '', '')
  476. } else if (isKeyframes(r.key)) {
  477. keyframes(f, r)
  478. } else if (isRootBlock(r.key)) {
  479. for (d of entriesOf(f.key, r.node, r.value)) {
  480. if (d.isBlock) { hlError("'" + r.key + "' holds custom properties only — '" + d.key + "' is a rule; put it beside the block, not inside it") }
  481. rootDecls.push(declLine(d.key, d.value))
  482. }
  483. } else {
  484. let sels = selectorsFor(f, r.key)
  485. if (sels.length == 0) { dead.push(f.key + ' ' + f.cls + '.' + r.key + ' bound to nothing (no reference, no matching element)') }
  486. else { emitRule(f, r.key, sels, entriesOf(f.key, r.node, r.value), '') }
  487. }
  488. }
  489. }
  490. let out = minify ? '' : HEADER
  491. if (rootDecls.length > 0) {
  492. let body = ''
  493. for (d of rootDecls) { body = body + d }
  494. out = out + (minify ? ':root{' + body + '}' : NL + ':root {' + NL + body + '}' + NL)
  495. }
  496. for (p of parts) { out = out + p }
  497. for (q of mediaOrder) {
  498. let inner = ''
  499. for (p of mediaParts) { if (p.query == q) { inner = inner + p.text } }
  500. out = out + (minify ? q + '{' + inner + '}' : NL + q + ' {' + NL + inner + '}' + NL)
  501. }
  502. if (dead.length > 0) {
  503. // A RULE THAT MATCHES NOTHING IS A BUILD ERROR (hybriel#243), not a log line: the
  504. // page would ship without it and nothing on the page says so
  505. let all = ''
  506. for (d of dead) { all = all + NL + ' ' + d }
  507. hlError('hl:web: style rules that match nothing in their file, nor in any component it composes (file.rule):' + all + NL + 'Reference the rule with Style.rule on an element, name an element of the View, write the selector whole (\'.bar section h2\'), or delete the rule')
  508. }
  509. return out
  510. }

Branches

Latest commits

  • 7480a98dkunibert.service: SIGTERM stop is a clean exitKunibert
  • 3a8d93f9gitoria client: parse only JSON answers, accept a plain-text errorKunibert
  • c9b48bfeVendor Hybriel plugins; deploy ships bin/ from zig-outKunibert
  • a41ba15fKunibert: event intake, git cache, review runner, posting, linked tickets, deploy filesKunibert