gitoria

layouts

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit413b11e1413b11e1Record corrected LY7 nesting, exact CSS and Chrome proofmre413b11e1/plugins/web/css.hl

24.7 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. out.push({ key = e.key; node = e.value; value = val; isBlock = v != null && (v.tag == 'object_expression' || (v.tag == 'binary_expression' && isHybridValue(val))); })
  211. }
  212. }
  213. }
  214. return markRepeated(key, out)
  215. }
  216. // THE VALUES OF A BLOCK THAT THE INSTANCE CANNOT HOLD (hybriel#257). Two members of one
  217. // name are one member in the constructed instance, so a query written twice
  218. // (`@media { minWidth … }` and `@media { maxWidth … }`) cannot be read from it by name.
  219. // Every occurrence of such a name is marked SYNTAX: its entries, at any depth, are read
  220. // off the source in place, where each block has its own position. Only a literal can be
  221. // read that way (a string, a number, a boolean); a name or a call inside such a block
  222. // is a located error that says what to do.
  223. static SYNTAX = '[read from the syntax]'
  224. syntaxEntry(key, e, v) {
  225. if (v != null && v.tag == 'object_expression') { return { key = e.key; node = e.value; value = SYNTAX; isBlock = true; } }
  226. if (v == null || v.tag != 'literal') {
  227. 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)' = { … })")
  228. }
  229. let val = v.text
  230. if (v.kind == 'number') { val = toNumber(v.text) }
  231. if (v.kind == 'boolean') { val = v.text == 'true' }
  232. return { key = e.key; node = e.value; value = val; isBlock = false; }
  233. }
  234. // a query name that occurs more than once among the entries of one block: every
  235. // occurrence is read from the syntax (see SYNTAX)
  236. markRepeated(key, entries) {
  237. count = {}
  238. for (e of entries) {
  239. if (isMediaBlock(e.key)) { count[e.key] = count[e.key] == null ? 1 : count[e.key] + 1 }
  240. }
  241. out = []
  242. for (e of entries) {
  243. if (count[e.key] != null && count[e.key] > 1 && e.value != SYNTAX) {
  244. sn = hlSyntax(gen, key, e.node)
  245. if (sn == null || sn.tag != 'object_expression') {
  246. 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")
  247. }
  248. out.push({ key = e.key; node = e.node; value = SYNTAX; isBlock = true; })
  249. } else {
  250. out.push(e)
  251. }
  252. }
  253. return out
  254. }
  255. // a hybrid VALUE (a rule), as opposed to a scalar declaration or a var() token
  256. isHybridValue(v) { return v != null && hlTypeName(v) == 'Hybrid' }
  257. // a value as CSS text: a string as written, a number unitless, a static already
  258. // folded, a css variable (or a string composed with one) resolved to var(--name)
  259. text(v) {
  260. return varText(v)
  261. }
  262. declLine(k, v) {
  263. if (minify) { return view.kebab(k) + ':' + text(v) + ';' }
  264. return ' ' + view.kebab(k) + ': ' + text(v) + ';' + NL
  265. }
  266. // a media condition: `minWidth = 960` → `(min-width: 960px)`, a string verbatim
  267. condition(k, v) {
  268. if (hlTypeName(v) == 'Number') { return '(' + view.kebab(k) + ': ' + v + 'px)' }
  269. return '(' + view.kebab(k) + ': ' + v + ')'
  270. }
  271. // the whole prelude, `@media (a) and (b)`: an enclosing query of the SAME kind is
  272. // joined with `and`; one of another kind cannot be, in a sheet written flat
  273. queryText(kind, outer, literal, conds, name) {
  274. let parts = []
  275. if (outer != '') {
  276. 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") }
  277. parts.push(outer.slice(kind.length + 1, outer.length))
  278. }
  279. if (literal != '') { parts.push(literal) }
  280. for (c of conds) { parts.push(c) }
  281. let out = ''
  282. for (p of parts) { out = out == '' ? p : out + ' and ' + p }
  283. return kind + ' ' + out
  284. }
  285. // ---- where a rule lands (the archive's placesFor) -------------------------------
  286. // `f` is the file block: { key cls refs tags hasView isStyles }; refs are what the
  287. // View wrote ([{ tag rule }]), tags the element names the file's own View renders.
  288. selectorsFor(f, name) {
  289. if (f.isStyles) {
  290. if (selectorShaped(name)) { return [name] }
  291. return [view.domTag(name)]
  292. }
  293. let local = localName(name)
  294. // the View writes `id = 'x'`: `#x` is that id, nothing else (ticket #51, R3)
  295. if (local != '' && f.ids != null && f.ids.includes(local)) { return [name] }
  296. if (local != '' && !selectorShaped(local)) {
  297. let out = []
  298. for (r of f.refs) {
  299. if (r.rule == name) {
  300. let sel = view.domTag(r.tag) + '.' + view.localClass(f.key)
  301. if (!out.includes(sel)) { out.push(sel) }
  302. }
  303. }
  304. return out
  305. }
  306. if (selectorShaped(name)) { return [name] }
  307. let classes = []
  308. for (r of f.refs) {
  309. if (r.rule == name) {
  310. let cls = view.className(name, r.tag)
  311. if (!classes.includes(cls)) { classes.push(cls) }
  312. }
  313. }
  314. if (classes.length == 0) {
  315. if (f.tags.includes(name) || name == 'body' || name == 'html') { return [view.domTag(name)] }
  316. return []
  317. }
  318. let out = []
  319. for (cls of classes) {
  320. if (cls == name) { out.push('.' + view.kebab(cls)) }
  321. else { out.push(view.domTag(name.slice(0, name.length - cls.length)) + '.' + view.kebab(cls)) }
  322. }
  323. return out
  324. }
  325. // ---- emission -------------------------------------------------------------------
  326. putRule(f, name, selector, body, query) {
  327. let first = seen[query + '|' + selector]
  328. if (first != null) {
  329. if (first.body == body) { return null }
  330. 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"
  331. // 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)
  332. console.log('error: ' + msg)
  333. hlError(msg)
  334. }
  335. seen[query + '|' + selector] = { body = body; rule = name; file = f.key; }
  336. if (query == '') {
  337. parts.push(minify ? selector + '{' + body + '}' : NL + selector + ' {' + NL + body + '}' + NL)
  338. } else {
  339. mediaPut(query, selector, body)
  340. }
  341. return null
  342. }
  343. mediaPut(query, selector, body) {
  344. if (!mediaOrder.includes(query)) { mediaOrder.push(query) }
  345. // inside a media block every line is indented two spaces (the archive's goldens)
  346. let inner = minify ? selector + '{' + body + '}' : ' ' + selector + ' {' + NL + ' ' + body.replace(NL + ' ', NL + ' ') + ' }' + NL
  347. mediaParts.push({ query = query; text = inner; })
  348. return null
  349. }
  350. // ONE RULE: its own declarations under `sels`, its nested rules composed below it
  351. // (descendant), its media blocks under the query; a container emits nothing itself
  352. emitRule(f, name, sels, entries, query) {
  353. let body = ''
  354. let kids = []
  355. let medias = []
  356. for (e of entries) {
  357. if (e.isBlock) {
  358. if (isMediaBlock(e.key)) { medias.push(e) } else { kids.push(e) }
  359. } else if (e.value != null) {
  360. body = body + declLine(e.key, e.value)
  361. }
  362. }
  363. if (body != '') {
  364. for (sel of sels) { putRule(f, name, sel, body, query) }
  365. }
  366. for (k of kids) {
  367. 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") }
  368. let composed = nestedSels(f, sels, k.key)
  369. if (composed.length == 0) {
  370. dead.push(f.key + ' ' + f.cls + '.' + name + ' ' + k.key)
  371. } else {
  372. emitRule(f, name + ' ' + k.key, composed, entriesOf(f.key, k.node, k.value), query)
  373. }
  374. }
  375. for (m of medias) {
  376. // a query inside a query of the SAME kind joins with `and` (queryText refuses another kind)
  377. mediaBlock(f, m.key, m.node, m.value, sels, name, query)
  378. }
  379. return null
  380. }
  381. // ONE `@media` BLOCK, at the top of a Style (prefixes empty) or inside a rule
  382. // (prefixes = the enclosing rule's selectors): scalar entries are CONDITIONS
  383. // (only for the bare `@media` spelling), block entries are RULES under the query;
  384. // inside a rule, scalar entries are the ENCLOSING RULE's declarations under it.
  385. mediaBlock(f, name, node, hybrid, prefixes, outer, outerQuery) {
  386. let literal = mediaLiteral(name)
  387. let conds = []
  388. let own = ''
  389. let rules = []
  390. let overrides = ''
  391. for (e of entriesOf(f.key, node, hybrid)) {
  392. if (e.isBlock) { rules.push(e) }
  393. // a css VARIABLE re-assigned under the query: `bg = '#000'` inside
  394. // `@media { prefersColorScheme = 'dark' … }` is `:root { --bg: #000 }` there
  395. else if (prefixes.length == 0 && varNames[e.key] != null) { overrides = overrides + declLine('--' + view.kebab(e.key), e.value) }
  396. else if (literal == '' && prefixes.length == 0) { conds.push(condition(e.key, e.value)) }
  397. // INSIDE A RULE a bare query's scalars are its conditions when they name a feature
  398. // (`pointer = 'coarse'`, `minWidth = 960`) and the rule's declarations otherwise
  399. else if (literal == '' && isFeature(queryKind(name), e.key)) { conds.push(condition(e.key, e.value)) }
  400. else if (prefixes.length > 0) { own = own + declLine(e.key, e.value) }
  401. 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") }
  402. }
  403. if (prefixes.length > 0 && literal == '' && conds.length == 0) {
  404. 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)' = { … }")
  405. }
  406. let q = queryText(queryKind(name), outerQuery, literal, conds, name)
  407. if (overrides != '') { putRule(f, name, ':root', overrides, q) }
  408. if (own != '') { for (sel of prefixes) { putRule(f, outer, sel, own, q) } }
  409. for (r of rules) {
  410. if (isMediaBlock(r.key)) { mediaBlock(f, r.key, r.node, r.value, prefixes, outer, q) }
  411. 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") }
  412. else {
  413. let composed = prefixes.length > 0 ? nestedSels(f, prefixes, r.key) : selectorsFor(f, r.key)
  414. if (composed.length == 0) { dead.push(f.key + ' ' + f.cls + '.' + r.key + ' (in ' + name + ')') }
  415. else { emitRule(f, r.key, composed, entriesOf(f.key, r.node, r.value), q) }
  416. }
  417. }
  418. return null
  419. }
  420. // a file's rules in authored order: [{ key node value isBlock }] — a component's
  421. // Style entries, or a styles file's root members (its inherits' first)
  422. fileRules(f) {
  423. return f.rules
  424. }
  425. // ONE `'@keyframes name' = { 'from' = { … } to { … } '50%' = { … } }`: every entry is
  426. // a stop, its key the stop's selector verbatim, its entries declarations
  427. keyframes(f, r) {
  428. let name = r.key.slice(10, r.key.length).trim()
  429. if (name == '') { hlError("'@keyframes' in " + f.key + " names no animation — write the name into the key: '@keyframes fade' = { 'from' = { opacity = 0 } to { opacity = 1 } }") }
  430. let body = ''
  431. for (st of entriesOf(f.key, r.node, r.value)) {
  432. 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") }
  433. let decls = ''
  434. for (d of entriesOf(f.key, st.node, st.value)) {
  435. if (d.isBlock) { hlError("'" + d.key + "' in the stop '" + st.key + "' of '" + r.key + "' (" + f.key + ") is a block — a stop holds declarations only") }
  436. if (d.value != null) { decls = decls + (minify ? declLine(d.key, d.value) : ' ' + declLine(d.key, d.value)) }
  437. }
  438. body = body + (minify ? st.key + '{' + decls + '}' : ' ' + st.key + ' {' + NL + decls + ' }' + NL)
  439. }
  440. putRule(f, r.key, '@keyframes ' + name, body, '')
  441. return null
  442. }
  443. // ---- the sheet ------------------------------------------------------------------
  444. // `files` are the blocks in cascade order: the styles file (with its inherits
  445. // walked first by the caller), then every served component.
  446. sheet(files) {
  447. parts = []
  448. seen = {}
  449. dead = []
  450. mediaOrder = []
  451. mediaParts = []
  452. rootDecls = []
  453. vars = {}
  454. varNames = {}
  455. for (f of files) { collectVars(f) }
  456. // the declared variables come first in :root, in declaration order; their dark
  457. // values open the dark-scheme query with a :root of their own (an explicit
  458. // `@media { prefersColorScheme = 'dark' … }` re-assignment lands after it and wins)
  459. let darkBody = ''
  460. for (f of files) {
  461. for (r of fileRules(f)) {
  462. if (!r.isBlock && isVar(r.value)) {
  463. rootDecls.push(declLine('--' + view.kebab(r.key), r.value.value))
  464. if (r.value.dark != null) { darkBody = darkBody + declLine('--' + view.kebab(r.key), r.value.dark) }
  465. }
  466. }
  467. }
  468. if (darkBody != '') { mediaPut(DARK, ':root', darkBody) }
  469. for (f of files) {
  470. for (r of fileRules(f)) {
  471. if (!r.isBlock) {
  472. // a scalar root member of a styles file is not a rule (a variable was taken above)
  473. } else if (isMediaBlock(r.key)) {
  474. mediaBlock(f, r.key, r.node, r.value, [], '', '')
  475. } else if (isKeyframes(r.key)) {
  476. keyframes(f, r)
  477. } else if (isRootBlock(r.key)) {
  478. for (d of entriesOf(f.key, r.node, r.value)) {
  479. if (d.isBlock) { hlError("'" + r.key + "' holds custom properties only — '" + d.key + "' is a rule; put it beside the block, not inside it") }
  480. rootDecls.push(declLine(d.key, d.value))
  481. }
  482. } else {
  483. let sels = selectorsFor(f, r.key)
  484. if (sels.length == 0) { dead.push(f.key + ' ' + f.cls + '.' + r.key + ' bound to nothing (no reference, no matching element)') }
  485. else { emitRule(f, r.key, sels, entriesOf(f.key, r.node, r.value), '') }
  486. }
  487. }
  488. }
  489. let out = minify ? '' : HEADER
  490. if (rootDecls.length > 0) {
  491. let body = ''
  492. for (d of rootDecls) { body = body + d }
  493. out = out + (minify ? ':root{' + body + '}' : NL + ':root {' + NL + body + '}' + NL)
  494. }
  495. for (p of parts) { out = out + p }
  496. for (q of mediaOrder) {
  497. let inner = ''
  498. for (p of mediaParts) { if (p.query == q) { inner = inner + p.text } }
  499. out = out + (minify ? q + '{' + inner + '}' : NL + q + ' {' + NL + inner + '}' + NL)
  500. }
  501. if (dead.length > 0) {
  502. // A RULE THAT MATCHES NOTHING IS A BUILD ERROR (hybriel#243), not a log line: the
  503. // page would ship without it and nothing on the page says so
  504. let all = ''
  505. for (d of dead) { all = all + NL + ' ' + d }
  506. 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')
  507. }
  508. return out
  509. }

Branches

Latest commits

  • 413b11e1Record corrected LY7 nesting, exact CSS and Chrome proofmre
  • b810453etracker-demo: nest child and compound selectors in Stylemre
  • 0b5b1d61notes-demo: nest child and compound selectors in Stylemre
  • ab22bdc2mail-demo: nest child and compound selectors in Stylemre
  • fe2d8de9home: nest child and compound selectors in Stylemre
  • e631791buser-menu: nest child and compound selectors in Stylemre
  • 69604944three-pane: nest child and compound selectors in Stylemre
  • 3e389b83main-menu: nest child and compound selectors in Stylemre
  • 05413975list-first: nest child and compound selectors in Stylemre
  • c545b642content-first: nest child and compound selectors in Stylemre
  • eae2be7aapp-sidebar: nest child selectors and document caller-slot gapmre
  • 69c9f784Record LY7 slot nesting blocker and minimal repromre
  • b1d4f379app-header: nest child and compound selectors in Stylemre
  • 6385ff8eRecord LY7 style formatting proof and review statusmre
  • 0100f51etracker-demo: format Style with nested rules and bare mediamre
  • 5f82554enotes-demo: format Style with nested rules and bare mediamre
  • 3dc27d78mail-demo: format Style with nested rules and bare mediamre
  • cf9648afhome: format Style with nested rules and bare mediamre
  • 846a6242user-menu: format Style with nested rules and bare mediamre
  • 4d58708cthree-pane: format Style with nested rules and bare mediamre