gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit0d253b450d253b45mission: T36Amre0d253b45/plugins/web/css.hl

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

Branches

Latest commits

  • 0d253b45mission: T36Amre
  • 2aa22d23log: #36 GOmre
  • 833aee2flog: #50 closed, HM56 startedmre
  • ac58c1e5mission: HM56mre
  • 58979c0clog: HM54 held for decision, DP65 startedmre
  • bb2ebc67log: #64 live, #53 mergedmre
  • 27cde42fMerge hm53-dom: tracker#53 inputs bound to membersmre
  • 2156cc7flog: HM54 reviewmre
  • 9fb82efclog: HM54 gap, fill continuesmre
  • b632eb8dlog: HM53 done, HM54 startedmre
  • ccb1a263tracker#53: bind editor drafts and use key event focus targetmre
  • 3356fcfclog: #52 blocked, HM53 startedmre
  • 833dcfc9Merge hm52-search: report only, tracker#52 blocked by framework gapmre
  • 73010cc0HM52: report replace-navigation search draft regressionmre
  • 1742e92bstatus: DP64 and HM52 runningmre
  • 29ad5b58log: #64 merged, DP64 and HM52 startedmre
  • fafddbf9Merge hm64-catalog: tracker#64 catalog from packed entriesmre
  • b2e2206dlog: #63 livemre
  • 5831e0e0log: HM64 donemre
  • 4042b923Flatten catalog membership rows into paired slotsmre