gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/plugins/web/css.hl

27.3 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. // Split only selector-list commas; functional pseudos, attributes, strings and
  118. // escaped commas belong to their selector (hybriel#408).
  119. selectorParts(String source) {
  120. let out = []
  121. let part = ''
  122. let depth = 0
  123. let brackets = 0
  124. let quote = ''
  125. let escaped = false
  126. for (c of source) {
  127. if (escaped) { part = part + c escaped = false }
  128. else if (c.charCodeAt(0) == 92) { part = part + c escaped = true }
  129. else if (quote != '') {
  130. part = part + c
  131. if (c == quote) { quote = '' }
  132. } else if (c == ',' && depth == 0 && brackets == 0) {
  133. out.push(part.trim())
  134. part = ''
  135. } else {
  136. part = part + c
  137. if (c == "'" || c == '"') { quote = c }
  138. else if (c == '[') { brackets = brackets + 1 }
  139. else if (c == ']') { brackets = brackets - 1 }
  140. else if (brackets == 0 && c == '(') { depth = depth + 1 }
  141. else if (brackets == 0 && c == ')') { depth = depth - 1 }
  142. }
  143. }
  144. out.push(part.trim())
  145. return out
  146. }
  147. // the selectors a nested entry composes under its enclosing rule's `sels`: a
  148. // pseudo-class sticks to each (`a:hover`), anything else is a descendant (`a span`)
  149. nestedSels(Hybrid f, Hybrid sels, String key) {
  150. let out = []
  151. if (isPseudo(key)) {
  152. for (p of sels) {
  153. let composed = []
  154. for (s of selectorParts(p)) { composed.push(s + ':' + view.kebab(key)) }
  155. out.push(composed.join(', '))
  156. }
  157. return out
  158. }
  159. let kidSels = selectorsFor(f, key)
  160. // a tag that only a COMPOSED component's View renders (hybriel#243): `'.bar' { 'img' { … } }`
  161. // is `.bar img` when Child, composed inside `.bar`, has the img
  162. if (kidSels.length == 0 && f.deepTags != null && f.deepTags.includes(key)) { kidSels = [view.domTag(key)] }
  163. // a tag a CALLER writes into this component's slot (hybriel#270): it lands inside the element
  164. if (kidSels.length == 0 && f.fillTags != null && f.fillTags.includes(view.domTag(key))) { kidSels = [view.domTag(key)] }
  165. for (p of sels) {
  166. for (s of kidSels) {
  167. let composed = []
  168. for (outer of selectorParts(p)) {
  169. for (child of selectorParts(s)) { composed.push(outer + ' ' + child) }
  170. }
  171. out.push(composed.join(', '))
  172. }
  173. }
  174. return out
  175. }
  176. isKeyframes(name) { return name == '@keyframes' || name.startsWith('@keyframes ') }
  177. isRootBlock(name) { return name == '@root' || name == ':root' }
  178. // THE THREE QUERIES share one shape: `@media`, `@supports`, `@container` — bare with
  179. // conditions as scalars, or spelled out in the key
  180. static QUERIES = ['@media', '@supports', '@container']
  181. queryKind(name) {
  182. for (k of QUERIES) {
  183. if (name == k || name.startsWith(k + ' ') || name.startsWith(k + '(')) { return k }
  184. }
  185. return ''
  186. }
  187. isMediaBlock(name) { return queryKind(name) != '' }
  188. // THE FEATURES a bare query inside a rule reads as conditions, camelCase as written.
  189. // `width`, `height`, `color` and `grid` are also declarations and stay declarations
  190. // there: write `minWidth` / `maxWidth` (and `minHeight`, `maxHeight`) for a size.
  191. static MEDIA_FEATURES = ['aspectRatio', 'orientation', 'resolution', 'pointer', 'hover', 'anyPointer', 'anyHover',
  192. 'prefersColorScheme', 'prefersReducedMotion', 'prefersContrast', 'prefersReducedData', 'prefersReducedTransparency',
  193. 'displayMode', 'overflowBlock', 'overflowInline', 'update', 'scan', 'colorGamut', 'colorIndex', 'monochrome',
  194. 'forcedColors', 'invertedColors', 'scripting', 'videoDynamicRange', 'dynamicRange']
  195. static RANGE_FEATURES = ['width', 'height', 'aspectRatio', 'resolution', 'color', 'colorIndex', 'monochrome']
  196. static CONTAINER_FEATURES = ['inlineSize', 'blockSize', 'aspectRatio', 'orientation']
  197. isFeature(kind, name) {
  198. if (kind == '@media') {
  199. if (MEDIA_FEATURES.includes(name)) { return true }
  200. if (name.length > 3 && (name.startsWith('min') || name.startsWith('max'))) {
  201. rest = name.slice(3, name.length)
  202. low = rest.charAt(0).toLowerCase() + rest.slice(1, rest.length)
  203. return RANGE_FEATURES.includes(low)
  204. }
  205. return false
  206. }
  207. if (kind == '@container') {
  208. if (CONTAINER_FEATURES.includes(name)) { return true }
  209. if (name.length > 3 && (name.startsWith('min') || name.startsWith('max'))) {
  210. rest = name.slice(3, name.length)
  211. low = rest.charAt(0).toLowerCase() + rest.slice(1, rest.length)
  212. return ['width', 'height', 'inlineSize', 'blockSize', 'aspectRatio'].includes(low)
  213. }
  214. }
  215. return false
  216. }
  217. mediaLiteral(name) { return name.slice(queryKind(name).length, name.length).trim() }
  218. localName(name) { return name.startsWith('#') ? name.slice(1) : '' }
  219. selectorShaped(name) {
  220. if (name.length == 0) { return false }
  221. if ('0123456789'.indexOf(name.charAt(0)) >= 0) { return true }
  222. let i = 0
  223. while (i < name.length) {
  224. if (SELECTOR_CHARS.indexOf(name.charAt(i)) >= 0) { return true }
  225. i = i + 1
  226. }
  227. return false
  228. }
  229. // ---- one block's syntax, with its values --------------------------------------
  230. // the entries of a hybrid node: [{ key node value isBlock }] in authored order,
  231. // values read off `hybrid` (the instance's evaluated member at the same path)
  232. entriesOf(key, node, hybrid) {
  233. let out = []
  234. let n = node == 0 ? null : hlSyntax(gen, key, node)
  235. if (n == null || n.tag != 'object_expression') {
  236. // A MERGED RULE (`extR = baseR + { top = 'auto' }`, COMPONENTS §4: "Style gets it
  237. // for free") has no literal to read: its entries are the merged VALUE's, and a
  238. // nested block is read the same way. It used to read as no entries at all, so the
  239. // View wrote the class and the sheet carried no rule for it (mission 322 row 9).
  240. if (isHybridValue(hybrid)) {
  241. for (k of hybrid.keys()) { out.push({ key = k; node = 0; value = hybrid[k]; isBlock = isHybridValue(hybrid[k]); }) }
  242. }
  243. return out
  244. }
  245. own = hybrid == SYNTAX
  246. for (h of n.entries) {
  247. let e = hlSyntax(gen, key, h)
  248. if (e != null && e.tag == 'object_property') {
  249. if (e.key.indexOf('&') >= 0) {
  250. hlError("hl:web: " + key + ':' + e.line + ':' + e.col + " — '&' in Style key '" + e.key + "' is refused. Apply a class with Style.name on the element, or write the whole selector quoted without '&'")
  251. }
  252. let v = hlSyntax(gen, key, e.value)
  253. if (own) {
  254. out.push(syntaxEntry(key, e, v))
  255. } else {
  256. val = hybrid != null ? hybrid[e.key] : null
  257. // A static hybrid has no syntax block here; its evaluated value is the rule (W219, #253).
  258. out.push({ key = e.key; node = e.value; value = val; isBlock = (v != null && v.tag == 'object_expression') || isHybridValue(val); })
  259. }
  260. }
  261. }
  262. return markRepeated(key, out)
  263. }
  264. // THE VALUES OF A BLOCK THAT THE INSTANCE CANNOT HOLD (hybriel#257). Two members of one
  265. // name are one member in the constructed instance, so a query written twice
  266. // (`@media { minWidth … }` and `@media { maxWidth … }`) cannot be read from it by name.
  267. // Every occurrence of such a name is marked SYNTAX: its entries, at any depth, are read
  268. // off the source in place, where each block has its own position. Only a literal can be
  269. // read that way (a string, a number, a boolean); a name or a call inside such a block
  270. // is a located error that says what to do.
  271. static SYNTAX = '[read from the syntax]'
  272. syntaxEntry(key, e, v) {
  273. if (v != null && v.tag == 'object_expression') { return { key = e.key; node = e.value; value = SYNTAX; isBlock = true; } }
  274. if (v == null || v.tag != 'literal') {
  275. 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)' = { … })")
  276. }
  277. let val = v.text
  278. if (v.kind == 'number') { val = toNumber(v.text) }
  279. if (v.kind == 'boolean') { val = v.text == 'true' }
  280. return { key = e.key; node = e.value; value = val; isBlock = false; }
  281. }
  282. // a query name that occurs more than once among the entries of one block: every
  283. // occurrence is read from the syntax (see SYNTAX)
  284. markRepeated(key, entries) {
  285. count = {}
  286. for (e of entries) {
  287. if (isMediaBlock(e.key)) { count[e.key] = count[e.key] == null ? 1 : count[e.key] + 1 }
  288. }
  289. out = []
  290. for (e of entries) {
  291. if (count[e.key] != null && count[e.key] > 1 && e.value != SYNTAX) {
  292. sn = hlSyntax(gen, key, e.node)
  293. if (sn == null || sn.tag != 'object_expression') {
  294. 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")
  295. }
  296. out.push({ key = e.key; node = e.node; value = SYNTAX; isBlock = true; })
  297. } else {
  298. out.push(e)
  299. }
  300. }
  301. return out
  302. }
  303. // a hybrid VALUE (a rule), as opposed to a scalar declaration or a var() token
  304. isHybridValue(v) { return v != null && hlTypeName(v) == 'Hybrid' }
  305. // a value as CSS text: a string as written, a number unitless, a static already
  306. // folded, a css variable (or a string composed with one) resolved to var(--name)
  307. text(v) {
  308. return varText(v)
  309. }
  310. declLine(k, v) {
  311. if (minify) { return view.kebab(k) + ':' + text(v) + ';' }
  312. return ' ' + view.kebab(k) + ': ' + text(v) + ';' + NL
  313. }
  314. // a media condition: `minWidth = 960` → `(min-width: 960px)`, a string verbatim
  315. condition(k, v) {
  316. if (hlTypeName(v) == 'Number') { return '(' + view.kebab(k) + ': ' + v + 'px)' }
  317. return '(' + view.kebab(k) + ': ' + v + ')'
  318. }
  319. // the whole prelude, `@media (a) and (b)`: an enclosing query of the SAME kind is
  320. // joined with `and`; one of another kind cannot be, in a sheet written flat
  321. queryText(kind, outer, literal, conds, name) {
  322. let parts = []
  323. if (outer != '') {
  324. 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") }
  325. parts.push(outer.slice(kind.length + 1, outer.length))
  326. }
  327. if (literal != '') { parts.push(literal) }
  328. for (c of conds) { parts.push(c) }
  329. let out = ''
  330. for (p of parts) { out = out == '' ? p : out + ' and ' + p }
  331. return kind + ' ' + out
  332. }
  333. // ---- where a rule lands (the archive's placesFor) -------------------------------
  334. // `f` is the file block: { key cls refs tags hasView isStyles }; refs are what the
  335. // View wrote ([{ tag rule }]), tags the element names the file's own View renders.
  336. selectorsFor(f, name) {
  337. if (f.isStyles) {
  338. if (selectorShaped(name)) { return [name] }
  339. return [view.domTag(name)]
  340. }
  341. let local = localName(name)
  342. // the View writes `id = 'x'`: `#x` is that id, nothing else (ticket #51, R3)
  343. if (local != '' && f.ids != null && f.ids.includes(view.kebab(local))) { return [view.kebab(name)] }
  344. if (local != '' && !selectorShaped(local)) {
  345. let out = []
  346. for (r of f.refs) {
  347. if (r.rule == name) {
  348. let sel = view.domTag(r.tag) + '.' + view.localClass(f.key)
  349. if (!out.includes(sel)) { out.push(sel) }
  350. }
  351. }
  352. return out
  353. }
  354. if (selectorShaped(name)) { return [name] }
  355. let classes = []
  356. for (r of f.refs) {
  357. if (r.rule == name) {
  358. let cls = view.className(name, r.tag)
  359. if (!classes.includes(cls)) { classes.push(cls) }
  360. }
  361. }
  362. if (classes.length == 0) {
  363. if (f.tags.includes(name) || name == 'body' || name == 'html') { return [view.domTag(name)] }
  364. return []
  365. }
  366. let out = []
  367. for (cls of classes) {
  368. if (cls == name) { out.push('.' + view.kebab(cls)) }
  369. else { out.push(view.domTag(name.slice(0, name.length - cls.length)) + '.' + view.kebab(cls)) }
  370. }
  371. return out
  372. }
  373. // ---- emission -------------------------------------------------------------------
  374. // Normalize CSS whitespace only outside quoted strings and attribute brackets.
  375. cssWhitespace(source) {
  376. let out = ''
  377. let quote = ''
  378. let brackets = 0
  379. let escaped = false
  380. let pending = false
  381. for (c of source) {
  382. if (escaped) { out = out + c escaped = false }
  383. else if (c.charCodeAt(0) == 92) {
  384. if (pending && out != '') { out = out + ' ' }
  385. pending = false
  386. out = out + c
  387. escaped = true
  388. } else if (quote != '') {
  389. out = out + c
  390. if (c == quote) { quote = '' }
  391. } else if (brackets == 0 && c.trim() == '') { pending = true }
  392. else {
  393. if (pending && out != '') { out = out + ' ' }
  394. pending = false
  395. out = out + c
  396. if (c == "'" || c == '"') { quote = c }
  397. else if (c == '[') { brackets = brackets + 1 }
  398. else if (c == ']') { brackets = brackets - 1 }
  399. }
  400. }
  401. return out
  402. }
  403. putRule(f, name, selector, body, query) {
  404. selector = cssWhitespace(selector)
  405. query = cssWhitespace(query)
  406. let first = seen[query + '|' + selector]
  407. if (first != null) {
  408. if (first.body == body) { return null }
  409. 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"
  410. // 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)
  411. console.log('error: ' + msg)
  412. hlError(msg)
  413. }
  414. seen[query + '|' + selector] = { body = body; rule = name; file = f.key; }
  415. if (query == '') {
  416. parts.push(minify ? selector + '{' + body + '}' : NL + selector + ' {' + NL + body + '}' + NL)
  417. } else {
  418. mediaPut(query, selector, body)
  419. }
  420. return null
  421. }
  422. mediaPut(query, selector, body) {
  423. if (!mediaOrder.includes(query)) { mediaOrder.push(query) }
  424. // inside a media block every line is indented two spaces (the archive's goldens)
  425. let inner = minify ? selector + '{' + body + '}' : ' ' + selector + ' {' + NL + ' ' + body.replace(NL + ' ', NL + ' ') + ' }' + NL
  426. mediaParts.push({ query = query; text = inner; })
  427. return null
  428. }
  429. // ONE RULE: its own declarations under `sels`, its nested rules composed below it
  430. // (descendant), its media blocks under the query; a container emits nothing itself
  431. emitRule(f, name, sels, entries, query) {
  432. let body = ''
  433. let kids = []
  434. let medias = []
  435. for (e of entries) {
  436. if (e.isBlock) {
  437. if (isMediaBlock(e.key)) { medias.push(e) } else { kids.push(e) }
  438. } else if (e.value != null) {
  439. body = body + declLine(e.key, e.value)
  440. }
  441. }
  442. if (body != '') {
  443. for (sel of sels) { putRule(f, name, sel, body, query) }
  444. }
  445. for (k of kids) {
  446. 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") }
  447. let composed = nestedSels(f, sels, k.key)
  448. if (composed.length == 0) {
  449. dead.push(f.key + ' ' + f.cls + '.' + name + ' ' + k.key)
  450. } else {
  451. emitRule(f, name + ' ' + k.key, composed, entriesOf(f.key, k.node, k.value), query)
  452. }
  453. }
  454. for (m of medias) {
  455. // a query inside a query of the SAME kind joins with `and` (queryText refuses another kind)
  456. mediaBlock(f, m.key, m.node, m.value, sels, name, query)
  457. }
  458. return null
  459. }
  460. // ONE `@media` BLOCK, at the top of a Style (prefixes empty) or inside a rule
  461. // (prefixes = the enclosing rule's selectors): scalar entries are CONDITIONS
  462. // (only for the bare `@media` spelling), block entries are RULES under the query;
  463. // inside a rule, scalar entries are the ENCLOSING RULE's declarations under it.
  464. mediaBlock(f, name, node, hybrid, prefixes, outer, outerQuery) {
  465. let literal = mediaLiteral(name)
  466. let conds = []
  467. let own = ''
  468. let rules = []
  469. let overrides = ''
  470. for (e of entriesOf(f.key, node, hybrid)) {
  471. if (e.isBlock) { rules.push(e) }
  472. // a css VARIABLE re-assigned under the query: `bg = '#000'` inside
  473. // `@media { prefersColorScheme = 'dark' … }` is `:root { --bg: #000 }` there
  474. else if (prefixes.length == 0 && varNames[e.key] != null) { overrides = overrides + declLine('--' + view.kebab(e.key), e.value) }
  475. else if (literal == '' && prefixes.length == 0) { conds.push(condition(e.key, e.value)) }
  476. // INSIDE A RULE a bare query's scalars are its conditions when they name a feature
  477. // (`pointer = 'coarse'`, `minWidth = 960`) and the rule's declarations otherwise
  478. else if (literal == '' && isFeature(queryKind(name), e.key)) { conds.push(condition(e.key, e.value)) }
  479. else if (prefixes.length > 0) { own = own + declLine(e.key, e.value) }
  480. 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") }
  481. }
  482. if (prefixes.length > 0 && literal == '' && conds.length == 0) {
  483. 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)' = { … }")
  484. }
  485. let q = queryText(queryKind(name), outerQuery, literal, conds, name)
  486. if (overrides != '') { putRule(f, name, ':root', overrides, q) }
  487. if (own != '') { for (sel of prefixes) { putRule(f, outer, sel, own, q) } }
  488. for (r of rules) {
  489. if (isMediaBlock(r.key)) { mediaBlock(f, r.key, r.node, r.value, prefixes, outer, q) }
  490. 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") }
  491. else {
  492. let composed = prefixes.length > 0 ? nestedSels(f, prefixes, r.key) : selectorsFor(f, r.key)
  493. if (composed.length == 0) { dead.push(f.key + ' ' + f.cls + '.' + r.key + ' (in ' + name + ')') }
  494. else { emitRule(f, r.key, composed, entriesOf(f.key, r.node, r.value), q) }
  495. }
  496. }
  497. return null
  498. }
  499. // a file's rules in authored order: [{ key node value isBlock }] — a component's
  500. // Style entries, or a styles file's root members (its inherits' first)
  501. fileRules(f) {
  502. return f.rules
  503. }
  504. // ONE `'@keyframes name' = { 'from' = { … } to { … } '50%' = { … } }`: every entry is
  505. // a stop, its key the stop's selector verbatim, its entries declarations
  506. keyframes(f, r) {
  507. let name = r.key.slice(10, r.key.length).trim()
  508. if (name == '') { hlError("'@keyframes' in " + f.key + " names no animation — write the name into the key: '@keyframes fade' = { 'from' = { opacity = 0 } to { opacity = 1 } }") }
  509. let body = ''
  510. for (st of entriesOf(f.key, r.node, r.value)) {
  511. 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") }
  512. let decls = ''
  513. for (d of entriesOf(f.key, st.node, st.value)) {
  514. if (d.isBlock) { hlError("'" + d.key + "' in the stop '" + st.key + "' of '" + r.key + "' (" + f.key + ") is a block — a stop holds declarations only") }
  515. if (d.value != null) { decls = decls + (minify ? declLine(d.key, d.value) : ' ' + declLine(d.key, d.value)) }
  516. }
  517. body = body + (minify ? st.key + '{' + decls + '}' : ' ' + st.key + ' {' + NL + decls + ' }' + NL)
  518. }
  519. putRule(f, r.key, '@keyframes ' + name, body, '')
  520. return null
  521. }
  522. // ---- the sheet ------------------------------------------------------------------
  523. // `files` are the blocks in cascade order: the styles file (with its inherits
  524. // walked first by the caller), then every served component.
  525. sheet(files) {
  526. parts = []
  527. seen = {}
  528. dead = []
  529. mediaOrder = []
  530. mediaParts = []
  531. rootDecls = []
  532. vars = {}
  533. varNames = {}
  534. for (f of files) { collectVars(f) }
  535. // the declared variables come first in :root, in declaration order; their dark
  536. // values open the dark-scheme query with a :root of their own (an explicit
  537. // `@media { prefersColorScheme = 'dark' … }` re-assignment lands after it and wins)
  538. let darkBody = ''
  539. for (f of files) {
  540. for (r of fileRules(f)) {
  541. if (!r.isBlock && isVar(r.value)) {
  542. rootDecls.push(declLine('--' + view.kebab(r.key), r.value.value))
  543. if (r.value.dark != null) { darkBody = darkBody + declLine('--' + view.kebab(r.key), r.value.dark) }
  544. }
  545. }
  546. }
  547. if (darkBody != '') { mediaPut(DARK, ':root', darkBody) }
  548. for (f of files) {
  549. for (r of fileRules(f)) {
  550. if (!r.isBlock) {
  551. // a scalar root member of a styles file is not a rule (a variable was taken above)
  552. } else if (isMediaBlock(r.key)) {
  553. mediaBlock(f, r.key, r.node, r.value, [], '', '')
  554. } else if (isKeyframes(r.key)) {
  555. keyframes(f, r)
  556. } else if (isRootBlock(r.key)) {
  557. for (d of entriesOf(f.key, r.node, r.value)) {
  558. if (d.isBlock) { hlError("'" + r.key + "' holds custom properties only — '" + d.key + "' is a rule; put it beside the block, not inside it") }
  559. rootDecls.push(declLine(d.key, d.value))
  560. }
  561. } else {
  562. let sels = selectorsFor(f, r.key)
  563. if (sels.length == 0) { dead.push(f.key + ' ' + f.cls + '.' + r.key + ' bound to nothing (no reference, no matching element)') }
  564. else { emitRule(f, r.key, sels, entriesOf(f.key, r.node, r.value), '') }
  565. }
  566. }
  567. }
  568. let out = minify ? '' : HEADER
  569. if (rootDecls.length > 0) {
  570. let body = ''
  571. for (d of rootDecls) { body = body + d }
  572. out = out + (minify ? ':root{' + body + '}' : NL + ':root {' + NL + body + '}' + NL)
  573. }
  574. for (p of parts) { out = out + p }
  575. for (q of mediaOrder) {
  576. let inner = ''
  577. for (p of mediaParts) { if (p.query == q) { inner = inner + p.text } }
  578. out = out + (minify ? q + '{' + inner + '}' : NL + q + ' {' + NL + inner + '}' + NL)
  579. }
  580. if (dead.length > 0) {
  581. // A RULE THAT MATCHES NOTHING IS A BUILD ERROR (hybriel#243), not a log line: the
  582. // page would ship without it and nothing on the page says so
  583. let all = ''
  584. for (d of dead) { all = all + NL + ' ' + d }
  585. 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')
  586. }
  587. return out
  588. }

Branches

Latest commits

  • 10f8a8c8Merge branch 'code-order' into mainJukebox
  • 204fc828Merge branch 'day72' into mainJukebox
  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre