gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitaa056c03aa056c03ident: a startup error prints its message before exit 1 (hybriel#247)mreaa056c03/plugins/web/view.hl

31.4 KB

  1. // The View walker — realm-neutral: the same code renders HTML on the server and
  2. // (compiled to JS) creates or claims DOM in the browser. It walks a TREE, the
  3. // syntax of a View member reflected into plain hybrids by the server (see
  4. // server.hl `tree()`), against an INSTANCE whose members supply the values.
  5. //
  6. // Tree node shapes:
  7. // { k = 'el' tag key path = [ keys… ] site attrs = [ { name text } | { name member } ] children = [ … ] }
  8. // `site` is `file:line:col` of the literal — the id its class is minted under
  9. // `tag` is what the DOM gets (a custom key kebab-cased), `key` the source key
  10. // { k = 'text' text }
  11. // { k = 'member' name } an ordered entry naming a member of the class — a SITE
  12. // { k = 'field' name path } `p.title`: a path off a member or a `for` row variable
  13. // { k = 'for' row list body } `for (row of list) { … }`, body rendered per entry
  14. // { k = 'if' cond then other } `if (cond) { … } else { … }`
  15. // { k = 'on' event } a DOM event the owning literal handles (its inline `on`)
  16. // { k = 'component' cls key path bindings ons } `Child { count = count on done(v) { … } }`:
  17. // a composed component — its own mount renders here
  18. // { k = 'other' tag } a construct this round does not render yet (if / for / on)
  19. //
  20. // `ctx` rides through every call: `ctx.slot` is the HTML a shell's `slot` member
  21. // stands for (null when rendering a page alone), `ctx.kids` the child mounts of
  22. // the instance being rendered, keyed by the component node's path joined with
  23. // '/'. A component node renders ITS mount: its own instance, its own kids.
  24. // THE HTML ELEMENTS. An entry whose key is one of these is that element; any other
  25. // key is a CUSTOM element and is written kebab-case (`postsList` → `<posts-list>`),
  26. // which is also what a Style rule of that name selects. This list is what §4 calls
  27. // "the compiler's own View element list": one list, so a tag rule in a styles file
  28. // and a tag in a View can never disagree.
  29. static tags = [
  30. 'a' 'abbr' 'address' 'article' 'aside' 'audio' 'b' 'bdi' 'bdo' 'blockquote' 'body' 'br' 'button'
  31. 'canvas' 'caption' 'cite' 'code' 'col' 'colgroup' 'data' 'datalist' 'dd' 'del' 'details' 'dfn'
  32. 'dialog' 'div' 'dl' 'dt' 'em' 'embed' 'fieldset' 'figcaption' 'figure' 'footer' 'form'
  33. 'h1' 'h2' 'h3' 'h4' 'h5' 'h6' 'head' 'header' 'hgroup' 'hr' 'html' 'i' 'iframe' 'img' 'input'
  34. 'ins' 'kbd' 'label' 'legend' 'li' 'link' 'main' 'map' 'mark' 'menu' 'meta' 'meter' 'nav'
  35. 'noscript' 'object' 'ol' 'optgroup' 'option' 'output' 'p' 'picture' 'pre' 'progress' 'q' 'rp'
  36. 'rt' 'ruby' 's' 'samp' 'script' 'search' 'section' 'select' 'slot' 'small' 'source' 'span'
  37. 'strong' 'style' 'sub' 'summary' 'sup' 'table' 'tbody' 'td' 'template' 'textarea' 'tfoot'
  38. 'th' 'thead' 'time' 'title' 'tr' 'track' 'u' 'ul' 'var' 'video' 'wbr' 'svg' 'path' 'circle'
  39. 'rect' 'line' 'polyline' 'polygon' 'g' 'text' 'use' 'defs' 'symbol' 'ellipse' 'stop' 'tspan'
  40. 'feBlend' 'feColorMatrix' 'feComposite' 'feFlood' 'feGaussianBlur' 'feMerge' 'feMergeNode'
  41. 'feOffset' 'feTurbulence' 'feDropShadow' 'feMorphology' 'feImage' 'feTile'
  42. 'feComponentTransfer' 'feFuncR' 'feFuncG' 'feFuncB' 'feFuncA' 'feConvolveMatrix'
  43. 'feDiffuseLighting' 'feSpecularLighting' 'feDisplacementMap' 'feDistantLight' 'fePointLight'
  44. 'feSpotLight' 'linearGradient' 'radialGradient' 'clipPath' 'textPath' 'foreignObject'
  45. 'animateMotion' 'animateTransform'
  46. ]
  47. // a form control with a `value` the app binds (a member or a field path), and no
  48. // autocomplete of the author's own
  49. boundValue(&node) {
  50. if (node.tag != 'input' && node.tag != 'textarea' && node.tag != 'select') { return false }
  51. let bound = false
  52. for (a of node.attrs) {
  53. if (a.name == 'autocomplete') { return false }
  54. if (a.name == 'value' && (a.member != null || a.ref != null)) { bound = true }
  55. }
  56. return bound
  57. }
  58. // the value an option reports to the form: its `value`, or null when it has none
  59. // and its text stands for it
  60. optionValue(&node, &page, &rows) {
  61. for (a of node.attrs) {
  62. if (a.name == 'value') {
  63. if (a.member != null) { return '' + value({ k = 'member'; name = a.member; }, &page, &rows) }
  64. if (a.ref != null) { return '' + value(&a.ref, &page, &rows) }
  65. return '' + a.text
  66. }
  67. }
  68. return null
  69. }
  70. // ---- THE ELEMENTS THE BROWSER'S PARSER INSERTS ----------------------------------
  71. // HTML has elements whose START TAG IS OPTIONAL: a `tr` written straight inside a
  72. // `table` is parsed into a `tbody` the parser creates, a `td` outside a `tr` gets both,
  73. // and a `col` gets a `colgroup`. The served tree and the PARSED tree have to be the same
  74. // tree — the claim walk pairs them element for element — so the tree carries what the
  75. // parser would insert and the author goes on writing `table { tr { td { … } } }`.
  76. // (Measured 2026-09-14 on demo-nested: 38 rows, 38 "claim: no element for tr under
  77. // TABLE", one per row; the fix in the app was to write the `tbody` by hand.)
  78. //
  79. // One row per (parent, child) pair the standard's optional-tag rules name. The walk
  80. // applies them REPEATEDLY, so a `td` under a `table` gains a `tbody` and then a `tr`
  81. // inside it — what the parser does, and not a table special case: a pair added to this
  82. // list is a pair the language then renders the way the browser will read it.
  83. static impliedElements = [
  84. { under = 'table' child = 'tr' wrap = 'tbody' }
  85. { under = 'table' child = 'td' wrap = 'tbody' }
  86. { under = 'table' child = 'th' wrap = 'tbody' }
  87. { under = 'table' child = 'col' wrap = 'colgroup' }
  88. { under = 'tbody' child = 'td' wrap = 'tr' }
  89. { under = 'tbody' child = 'th' wrap = 'tr' }
  90. { under = 'thead' child = 'td' wrap = 'tr' }
  91. { under = 'thead' child = 'th' wrap = 'tr' }
  92. { under = 'tfoot' child = 'td' wrap = 'tr' }
  93. { under = 'tfoot' child = 'th' wrap = 'tr' }
  94. ]
  95. // the container the parser would open for this child under this parent, or null
  96. impliedWrap(under, child) {
  97. if (child == null) { return null }
  98. for (r of impliedElements) { if (r.under == under && r.child == child) { return r.wrap } }
  99. return null
  100. }
  101. // THE TAG A NODE PUTS IN THE DOCUMENT: an element's own, and for a `for` or an `if` the
  102. // tag of the first element it renders — a region of `tr`s under a `table` needs the
  103. // `tbody` a bare `tr` needs, and it is the same rule that says so.
  104. firstTag(&n) {
  105. if (n == null) { return null }
  106. if (n.k == 'el') { return n.tag }
  107. if (n.k == 'for') { return firstTagIn(&n.body) }
  108. if (n.k == 'if') {
  109. let t = firstTagIn(&n.then)
  110. return t == null ? firstTagIn(&n.other) : t
  111. }
  112. return null
  113. }
  114. firstTagIn(&list) {
  115. if (list == null) { return null }
  116. for (n of list) {
  117. let t = firstTag(&n)
  118. if (t != null) { return t }
  119. }
  120. return null
  121. }
  122. // an element's children with those containers written out. A RUN of children that need
  123. // the same one SHARES it, because the parser opens one `tbody` and goes on putting rows
  124. // in it until something else closes it.
  125. implied(under, children, path, site) {
  126. let out = []
  127. let run = []
  128. let runWrap = null
  129. for (c of children) {
  130. let w = impliedWrap(under, firstTag(&c))
  131. if (w != runWrap && run.length > 0) {
  132. out.push(impliedNode(runWrap, run, path, site))
  133. run = []
  134. }
  135. runWrap = w
  136. if (w == null) { out.push(c) } else { run.push(c) }
  137. }
  138. if (run.length > 0) { out.push(impliedNode(runWrap, run, path, site)) }
  139. return out
  140. }
  141. // the node for a container nobody wrote: no attributes, no handlers, and the rule
  142. // applied again inside it (`table { td }` is `table > tbody > tr > td`)
  143. impliedNode(tag, children, path, site) {
  144. let here = path.slice(0)
  145. here.push(tag)
  146. return { k = 'el'; tag = tag; key = tag; path = here; attrs = []; children = implied(tag, children, here, site + '>' + tag); site = site + '>' + tag; }
  147. }
  148. static voidTags = [ 'area' 'base' 'br' 'col' 'embed' 'hr' 'img' 'input' 'link' 'meta' 'source' 'track' 'wbr' ]
  149. // THE VERBATIM ELEMENTS: whitespace between their tags is CONTENT, not layout. A
  150. // textarea's text IS its value, and a `pre`, a `script` and a `style` show or run
  151. // exactly what stands there — so the readable form never indents inside them
  152. // (the creator, 2026-09-13: "textarea seems to show the indentation as value").
  153. static verbatimTags = [ 'textarea' 'pre' 'script' 'style' ]
  154. isTag(name) {
  155. return tags.includes(name)
  156. }
  157. // the tag the DOM gets: an element of the list keeps its spelling (SVG has camelCase
  158. // elements: `linearGradient`, `clipPath`, `foreignObject`), any other key is custom, kebab-case
  159. domTag(name) {
  160. return tags.includes(name) ? name : kebab(name)
  161. }
  162. // camelCase → kebab-case (the archived WebView.kebab, verbatim): a separator only
  163. // BETWEEN a lowercase letter or digit and an uppercase one; at position 0, after
  164. // another uppercase or after a sigil an uppercase simply lowercases, so `ChatForm`
  165. // is `chat-form` and `#Svg` is `#svg`. The tag written on a custom element and the
  166. // selector a Style rule emits both come through here, so they cannot disagree.
  167. static KEBAB_JOINERS = "-_#@"
  168. kebab(name) {
  169. let out = ''
  170. let i = 0
  171. let prev = ''
  172. while (i < name.length) {
  173. let c = name.charAt(i)
  174. let lower = c.toLowerCase()
  175. let between = prev != '' && prev == prev.toLowerCase() && KEBAB_JOINERS.indexOf(prev) < 0
  176. if (c != lower && between) { out = out + '-' + lower } else { out = out + lower }
  177. prev = c
  178. i = i + 1
  179. }
  180. return out
  181. }
  182. // THE TAG-PREFIX STRIP (archived WebView.className, verbatim): rule `headerMain`
  183. // referenced under `header` is class `main`; under any other tag it keeps its name.
  184. className(rule, tag) {
  185. if (rule.length <= tag.length) { return rule }
  186. if (!rule.startsWith(tag)) { return rule }
  187. let rest = rule.slice(tag.length)
  188. let head = rest.charAt(0)
  189. if (head == head.toLowerCase()) { return rule }
  190. return head.toLowerCase() + rest.slice(1)
  191. }
  192. // THE PER-FILE CLASS of a `#local` rule (archived WebView.localClass, verbatim):
  193. // four characters out of the file's project-relative key, so it is stable across
  194. // checkouts and a golden can pin it. Every local rule of one file shares it; the
  195. // element's tag in front of it keeps them apart in the sheet (`svg.ma31`).
  196. static LOCAL_KEY_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/._-"
  197. static LOCAL_LETTERS = "abcdefghijklmnopqrstuvwxyz"
  198. static LOCAL_ALPHA = "abcdefghijklmnopqrstuvwxyz0123456789"
  199. localClass(fileKey) {
  200. let h = 0
  201. let i = 0
  202. while (i < fileKey.length) {
  203. h = (h * 31 + LOCAL_KEY_CHARS.indexOf(fileKey.charAt(i)) + 1) % 1000003
  204. i = i + 1
  205. }
  206. let out = LOCAL_LETTERS.charAt(h % 26)
  207. let rest = (h - (h % 26)) / 26
  208. let j = 0
  209. while (j < 3) {
  210. out = out + LOCAL_ALPHA.charAt(rest % 36)
  211. rest = (rest - (rest % 36)) / 36
  212. j = j + 1
  213. }
  214. return out
  215. }
  216. // the language has no escapes: a newline and a tab are written as themselves
  217. static newline = "
  218. "
  219. static tab = " "
  220. // a component node's key in a mount's `kids`: its path, '/'-joined (a list has no
  221. // join in the language; the walk is written out)
  222. kidKey(&path) {
  223. let out = ''
  224. for (p of path) { out = out + '/' + p }
  225. return out
  226. }
  227. // THE KEY OF ONE ROW'S CHILD: the component node's path plus the index of every
  228. // enclosing `for` iteration, which `ctx.rowKey` carries down the walk. Outside
  229. // every `for` the rowKey is empty and this is the plain path key.
  230. kidKeyIn(&path, &ctx) {
  231. return kidKey(&path) + (ctx.rowKey != null ? ctx.rowKey : '')
  232. }
  233. // THE KEY OF ONE ROW — "lists are keyed by the record's id" (CONCEPT §2), and by
  234. // the row's index where the row is not a record or carries no id (a list of strings
  235. // is one: demo-blog's paragraphs). ONE rule, called by everything that has to tell
  236. // one row from the next: the server's HTML walk below, the mount key of a row's
  237. // children (WebFramework `mountKids`), and the browser's keyed region (client.hl).
  238. // Two rows with the SAME id are two rows to the language and one key to this rule;
  239. // the browser's region gives the second one a key of its own, the server does not.
  240. rowKey(&entry, i) {
  241. if (entry != null) {
  242. let t = hlTypeName(&entry)
  243. if ((t == 'Hybrid' || t == 'Instance') && entry.id != null) { return '' + entry.id }
  244. }
  245. return '' + i
  246. }
  247. // THE KEYS OF A LIST, in order: rowKey of each row, and where one id stands twice the second and later
  248. // rows get ':' + their index (#119). The browser's region (client listKeys) keys a list the same way, so a
  249. // child of the second row of one id is its own mount on the server too, not the first row's.
  250. listKeys(&entries) {
  251. let out = []
  252. let seen = {}
  253. let ri = 0
  254. for (entry of entries) {
  255. let k = rowKey(&entry, ri)
  256. if (seen[k] != null) { k = k + ':' + ri }
  257. seen[k] = true
  258. out.push(k)
  259. ri = ri + 1
  260. }
  261. return out
  262. }
  263. // the walking context of one `for` iteration: the same slot and kids, one row deeper (`key`: listKeys' key of the row)
  264. rowCtx(&ctx, key) {
  265. let out = ctx + {}
  266. out.rowKey = (ctx.rowKey != null ? ctx.rowKey : '') + '#' + key
  267. return out
  268. }
  269. // A LITERAL'S VALUE, TYPED — COMPONENTS.md §12, "typed, not stringified". `hlSyntax`
  270. // hands a literal back as its source text and its KIND, and a reference binding carries
  271. // the value rather than the text: `Level2 { n = 1 }` gives the child a Number, so `n * 97`
  272. // is arithmetic and not "operator '*' does not apply to String and Number" (measured
  273. // 2026-09-14, demo-nested wall 3). An attribute and a text child keep the text, because
  274. // HTML is text; only what crosses into a member of another class is typed.
  275. literalValue(kind, text) {
  276. if (kind == 'number') { return toNumber(text) }
  277. if (kind == 'boolean') { return text == 'true' }
  278. if (kind == 'null_literal') { return null }
  279. return text
  280. }
  281. // THE VALUE A REFERENCE BINDING CARRIES when it is a literal or a folded static — the
  282. // one place the two are read, so the server's mount and the browser's do it alike. A
  283. // static is folded to its VALUE and carried as one ('folded'), never printed and parsed
  284. // back; a literal carries its source text and its kind, which is all the syntax has.
  285. refValue(b) {
  286. if (b.kind == 'folded') { return b.value }
  287. if (b.kind != null) { return literalValue(b.kind, b.text) }
  288. return b.text
  289. }
  290. // HTML BOOLEAN ATTRIBUTES MEAN PRESENCE, NOT VALUE (mission 084's rule, ticket #33): a
  291. // parser reads `disabled="false"` as DISABLED, so a bound one is written ` disabled=""`
  292. // when its value is on and left out when it is off, and the browser sets or removes it
  293. // and writes the property. The list is mission 084's, the one the archived SSR, graph and
  294. // client agreed on.
  295. static boolAttrs = ['checked' 'disabled' 'selected' 'readonly' 'required' 'multiple' 'open' 'hidden']
  296. isBoolAttr(name) {
  297. return boolAttrs.includes(name)
  298. }
  299. // is a boolean attribute ON for this value? false, null, '' and 0 leave it off — what the
  300. // browser's own `!!v` says, so the server's markup and the client's write agree
  301. boolOn(val) {
  302. if (val == null || val == false || val == '') { return false }
  303. if (hlTypeName(val) == 'Number' && val == 0) { return false }
  304. return true
  305. }
  306. escape(s) {
  307. if (s == null) { return '' }
  308. // EVERY occurrence, not the first: `replace` stops after one, so a value with two
  309. // quotes in it ("a" "b") left the second one raw in the attribute it was written into
  310. return ('' + s).replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;').replaceAll('"', '&quot;')
  311. }
  312. // THE VALUE a read names: a `for` row variable in scope wins over a member of the
  313. // same name (nesting is scope); a field path walks into it.
  314. value(&node, &page, &rows) {
  315. if (node.k == 'text') { return refValue(node) }
  316. if (node.k == 'expr') { return evalExpr(&node.x, &page, &rows) }
  317. // READ IN PLACE: `v = rows[node.name]` copied the whole row value (a block with all its spans) and each
  318. // `v = v[f]` step copied the rest again, per read, per mount; a path of up to three steps is one indexed read
  319. let name = node.name
  320. let inRow = rows[name] != null
  321. if (node.k == 'field') {
  322. let path = node.path
  323. let n = path.length
  324. if (n == 1) {
  325. if (inRow) { return rows[name] == null ? null : rows[name][path[0]] }
  326. return page[name] == null ? null : page[name][path[0]]
  327. }
  328. if (n == 2) {
  329. if (inRow) { return rows[name][path[0]] == null ? null : rows[name][path[0]][path[1]] }
  330. return page[name][path[0]] == null ? null : page[name][path[0]][path[1]]
  331. }
  332. }
  333. let v = null
  334. if (inRow) { v = rows[name] } else { v = page[name] }
  335. if (node.k == 'field') {
  336. for (f of node.path) {
  337. if (v == null) { return null }
  338. v = v[f]
  339. }
  340. }
  341. return v
  342. }
  343. // A VIEW EXPRESSION (`p { "x" + n }`, `a - b`, `n > 3 ? 'big' : 'small'`) is evaluated by
  344. // the LANGUAGE'S OWN OPERATORS: this walk only picks the operator the author wrote and
  345. // applies it, so what `+` gives null and a string, what `==` says of two types, what `&&`
  346. // returns — all of it is the runtime's answer, on the server (the interpreter or a
  347. // binary) and in the browser (the client target), and hl:web has no rule of its own.
  348. // The leaves are reads (`value`) and literals. `&&`, `||` and `?:` evaluate what they
  349. // pick only, as the language does.
  350. evalExpr(&x, &page, &rows) {
  351. if (x.k == 'bin') {
  352. let l = evalExpr(&x.l, &page, &rows)
  353. let op = x.op
  354. if (op == 'logical_and') { return l && evalExpr(&x.r, &page, &rows) }
  355. if (op == 'logical_or') { return l || evalExpr(&x.r, &page, &rows) }
  356. let r = evalExpr(&x.r, &page, &rows)
  357. if (op == 'add') { return l + r }
  358. if (op == 'subtract') { return l - r }
  359. if (op == 'multiply') { return l * r }
  360. if (op == 'divide') { return l / r }
  361. if (op == 'modulo') { return l % r }
  362. if (op == 'equal') { return l == r }
  363. if (op == 'not_equal') { return l != r }
  364. if (op == 'less_than') { return l < r }
  365. if (op == 'greater_than') { return l > r }
  366. if (op == 'less_equal') { return l <= r }
  367. if (op == 'greater_equal') { return l >= r }
  368. if (op == 'bit_and') { return l & r }
  369. if (op == 'bit_or') { return l | r }
  370. if (op == 'bit_xor') { return l ^ r }
  371. if (op == 'shift_left') { return l << r }
  372. return l >> r
  373. }
  374. if (x.k == 'un') {
  375. let o = evalExpr(&x.x, &page, &rows)
  376. if (x.op == 'logical_not') { return !o }
  377. if (x.op == 'negate') { return -o }
  378. return ~o
  379. }
  380. if (x.k == 'cond') { return evalExpr(&x.c, &page, &rows) ? evalExpr(&x.a, &page, &rows) : evalExpr(&x.b, &page, &rows) }
  381. return value(&x, &page, &rows)
  382. }
  383. // THE BIG VALUES OF THE WALK ARE TAKEN BY REFERENCE, and this is the whole of why the
  384. // server's render was slow (mission 312 wall 2, measured 2026-09-15). A METHOD CALL
  385. // DEEP-COPIES ITS ARGUMENTS — a lambda call does not, which is why this went unseen —
  386. // so every one of these calls copied the page INSTANCE and the `ctx` holding every
  387. // child mount of the page, once per element and once per read. The cost of rendering
  388. // one element was therefore proportional to everything the page was holding: a 43-row
  389. // table beside 120 components took 9.6 s, the same table alone 0.44 s, and a page whose
  390. // instance merely HELD 30 000 objects nobody renders took 34 s for 344 static elements.
  391. // Nothing in this walk writes through `page`, `rows` or `ctx` — `kidCtx`, `inner` and
  392. // `rowCtx`'s result are locals it copies on purpose — so a reference is the same walk,
  393. // and the `&` at a call site is optional, so every caller (client.hl included) is
  394. // unchanged. Measured after: 0.57 s, 1.27 s, 0.12 s for the three cases above.
  395. //
  396. // HTML of one node. `page` is the instance; `rows` the `for` variables in scope;
  397. // `ctx` carries the slot's HTML and the child mounts. `indent` is the current line's
  398. // indentation (a string of tabs) for the readable form, or null for the compact
  399. // one-line form (the production setting). Whitespace is only ever placed BETWEEN
  400. // elements: an element that carries text or a member value stays on one line, so
  401. // its text content is exactly what the author wrote and hydration finds it so.
  402. html(&node, &page, &rows, &ctx, indent) {
  403. if (node.k == 'text') { return escape(node.text) }
  404. if (node.k == 'on') { return '' } // the browser binds it
  405. if (node.k == 'member' || node.k == 'field' || node.k == 'expr') {
  406. if (node.k == 'member' && node.name == 'slot' && ctx.slot != null) { return ctx.slot }
  407. return escape(value(&node, &page, &rows))
  408. }
  409. if (node.k == 'for') {
  410. let out = ''
  411. let list = value(&node.list, &page, &rows)
  412. if (list == null) { return '' }
  413. let ri = 0
  414. let keys = listKeys(&list)
  415. for (entry of list) {
  416. let inner = rows + {}
  417. inner[node.row] = entry
  418. out = out + render(&node.body, &page, &inner, rowCtx(&ctx, keys[ri]), indent)
  419. ri = ri + 1
  420. }
  421. return out
  422. }
  423. if (node.k == 'if') {
  424. if (value(&node.cond, &page, &rows)) { return render(&node.then, &page, &rows, &ctx, indent) }
  425. return render(&node.other, &page, &rows, &ctx, indent)
  426. }
  427. if (node.k == 'component') {
  428. let kid = ctx.kids[kidKeyIn(&node.path, &ctx)]
  429. if (kid == null) { return '<!-- ' + node.cls + ' not mounted -->' }
  430. // THE FILL IS RENDERED HERE, IN THE HOST'S FRAME — the host's instance, the
  431. // host's `for` rows, the host's own child mounts — and handed to the child as
  432. // its `slot`, exactly as the shell is handed the page it wraps. The child
  433. // never evaluates it; it only places it.
  434. return render(withRootClasses(&kid.view, &node.classes), &kid.page, {}, { slot = fillHtml(&node, &page, &rows, &ctx); kids = kid.kids; }, indent)
  435. }
  436. if (node.k == 'el') {
  437. let attrs = ''
  438. // HTML GIVES A TEXTAREA NO `value` ATTRIBUTE: what stands between its tags is
  439. // its value, so a bound one is written there — the server-rendered control
  440. // already shows what the member holds, before any script runs.
  441. let valueText = null
  442. // AND A SELECT NO `value` ATTRIBUTE EITHER: what it shows is the option marked
  443. // `selected`, so a bound value is carried down to the options (`childCtx.sel`)
  444. // and marks the matching one. A `value` written on the select itself means
  445. // nothing to the browser, which then shows the FIRST option — and hydration's
  446. // two-way read then wrote that first option back into the member (measured
  447. // 2026-09-13 on demo-blog: a published post's editor said "Draft", and saving
  448. // unpublished it).
  449. let selValue = null
  450. for (a of node.attrs) {
  451. if (node.tag == 'textarea' && a.name == 'value') {
  452. // not an attribute: it is written below, as the element's content
  453. if (a.member != null) { valueText = escape(value({ k = 'member'; name = a.member; }, &page, &rows)) }
  454. else if (a.ref != null) { valueText = escape(value(&a.ref, &page, &rows)) }
  455. else { valueText = escape(a.text) }
  456. } else if (node.tag == 'select' && a.name == 'value') {
  457. // not an attribute: it selects an option below
  458. if (a.member != null) { selValue = '' + value({ k = 'member'; name = a.member; }, &page, &rows) }
  459. else if (a.ref != null) { selValue = '' + value(&a.ref, &page, &rows) }
  460. else { selValue = '' + a.text }
  461. } else if (isBoolAttr(a.name) && (a.member != null || a.ref != null)) {
  462. if (boolOn(value(a.member != null ? { k = 'member'; name = a.member; } : a.ref, &page, &rows))) { attrs = attrs + ' ' + a.name + '=""' }
  463. } else if (a.member != null) {
  464. // a null read is no attribute (#177), unless extra classes ride on it
  465. let av = value({ k = 'member'; name = a.member; }, &page, &rows)
  466. if (av != null || a.extra != null) { attrs = attrs + ' ' + a.name + '="' + escape(av) + extraOf(a) + '"' }
  467. } else if (a.ref != null) {
  468. let av = value(&a.ref, &page, &rows)
  469. if (av != null || a.extra != null) { attrs = attrs + ' ' + a.name + '="' + escape(av) + extraOf(a) + '"' }
  470. } else {
  471. attrs = attrs + ' ' + a.name + '="' + escape(a.text) + '"'
  472. }
  473. }
  474. // A CONTROL WHOSE VALUE THE APP BINDS IS THE APP'S: the browser must neither
  475. // autofill it nor restore a remembered value over it on reload (Chrome restores
  476. // unnamed text inputs by type and document order, so a remembered value lands
  477. // on whichever input now stands at that position — measured by the creator
  478. // 2026-09-13: "Gamma" in the add field after Ctrl+R)
  479. if (boundValue(&node)) { attrs = attrs + ' autocomplete="off"' }
  480. // THE CONTEXT IS NEVER COPIED INTO A LOCAL HERE. `let kidCtx = ctx` costs a copy
  481. // of everything the frame holds — every child mount of this page — and it stood
  482. // on the path of EVERY element: with the 43-row matrix in a page of 121
  483. // components it was 5.1 s against 0.6 s for the same two halves apart (measured
  484. // 2026-09-15, mission 312 wall 2). A select is the only node that changes the
  485. // context for its children, so it is the only branch that builds one.
  486. let children = ''
  487. if (node.tag == 'select') {
  488. // the select's value walks down to its options, however deep the `for` or the
  489. // `if` that builds them stands
  490. let kidCtx = ctx + {}
  491. kidCtx.sel = selValue
  492. children = kidsHtml(&node, &page, &rows, &kidCtx, indent)
  493. } else if (node.tag == 'script') {
  494. children = rawKids(&node, &page, &rows, &ctx)
  495. } else {
  496. children = kidsHtml(&node, &page, &rows, &ctx, indent)
  497. }
  498. // the textarea's value stands where its content stands
  499. if (valueText != null) { children = valueText }
  500. // THE OPTION THE SELECT SHOWS: the one whose value is the member's. An option
  501. // with no `value` reports its text, as the browser reads it.
  502. if (node.tag == 'option' && ctx.sel != null) {
  503. let ov = optionValue(&node, &page, &rows)
  504. if (ov == null) { ov = children }
  505. if (ov == ctx.sel) { attrs = attrs + ' selected' }
  506. }
  507. // a void element has no end tag (the HTML standard's list; the claim walks
  508. // elements, so the browser's parse of either form agrees with the tree)
  509. if (voidTags.includes(node.tag)) { return '<' + node.tag + attrs + '>' }
  510. return '<' + node.tag + attrs + '>' + children + '</' + node.tag + '>'
  511. }
  512. return '<!-- ' + node.tag + ' not rendered yet -->'
  513. }
  514. // AN ELEMENT'S CHILDREN, in the readable form or the compact one — split out of `html`
  515. // so that the one node which changes the context for its children (a select, which hands
  516. // its value down to its options) is the only one that builds a context at all.
  517. // A SCRIPT'S CONTENT IS RAW TEXT (#140): HTML decodes no entity in it, so `(() => 1)()`
  518. // served as `(() =&gt; 1)()` was a syntax error on a full page load (a client-built one
  519. // sets the text and never saw it). Text and values are written as they are, on one line;
  520. // only `</script` is broken (`<\/script`, the same string to the script), or the element
  521. // would end there.
  522. rawKids(&node, &page, &rows, &ctx) {
  523. let out = ''
  524. for (c of node.children) {
  525. if (c.k == 'text') { out = out + rawText(c.text) }
  526. else if (c.k == 'member' || c.k == 'field' || c.k == 'expr') { out = out + rawText(value(&c, &page, &rows)) }
  527. else { out = out + html(&c, &page, &rows, &ctx, null) }
  528. }
  529. return out
  530. }
  531. rawText(s) {
  532. if (s == null) { return '' }
  533. return ('' + s).replaceAll('</script', '<\/script')
  534. }
  535. kidsHtml(&node, &page, &rows, &ctx, indent) {
  536. if (indent != null && blocky(&node, &ctx)) {
  537. return block(&node.children, &page, &rows, &ctx, indent + tab) + newline + indent
  538. }
  539. let out = ''
  540. for (c of node.children) { out = out + html(&c, &page, &rows, &ctx, null) }
  541. return out
  542. }
  543. // The HTML a reference's fill stands for, or null when the reference fills nothing
  544. // (a bare `Card`, or `Card { }`) — then the child's `slot` renders as the member it
  545. // is, which is nothing.
  546. //
  547. // COMPACT, whatever form the page is rendered in: this frame is the HOST's, and the
  548. // indentation of the line the child places `slot` on is decided inside the child,
  549. // one frame further down. Whitespace between elements is not content and the claim
  550. // walk steps ELEMENTS, so a fill that is written on one line is claimed the same.
  551. fillHtml(&node, &page, &rows, &ctx) {
  552. if (node.fill == null || node.fill.length == 0) { return null }
  553. return render(&node.fill, &page, &rows, &ctx, null)
  554. }
  555. // AN ELEMENT'S CHILDREN MINUS THE HANDLERS A REFERENCE OVERWROTE. COMPONENTS.md:757
  556. // — "a same-named event binding on the reference overwrites the child root's own": the
  557. // reference's `on click` REPLACES the child root's `on click`, it does not stand beside
  558. // it, so the child's own entry for that event is never bound on that element.
  559. withoutOns(&nodes, &ons) {
  560. let out = []
  561. for (n of nodes) {
  562. let overwritten = false
  563. for (o of ons) { if (n.k == 'on' && o.event == n.event) { overwritten = true } }
  564. if (!overwritten) { out.push(n) }
  565. }
  566. return out
  567. }
  568. // THE CLASSES A REFERENCE PUTS ON THE CHILD'S ROOT, appended to a class the child
  569. // BINDS (`button { class = tone }`): the bound value is only known at render, so the
  570. // extra classes ride beside it and every writer of that attribute appends them —
  571. // here, and in the browser's create and repaint. Before this the merge handled only a
  572. // LITERAL class, and a child root that bound its own came out with TWO class
  573. // attributes, of which a browser keeps the first — so the host's `#Child` class
  574. // silently did nothing (measured 2026-09-13 on press.hl's `button { class = tone }`).
  575. extraOf(a) {
  576. if (a.extra == null) { return '' }
  577. return ' ' + a.extra
  578. }
  579. // A composed child's ROOT elements carry the classes the host's `#Child` local rule
  580. // put on the reference: the child's tree is shared, so the roots are copied with
  581. // the class merged in, and the child's own tree is untouched.
  582. withRootClasses(&nodes, &classes) {
  583. if (classes == null || classes.length == 0) { return nodes }
  584. let out = []
  585. for (n of nodes) {
  586. if (n.k == 'el') {
  587. let attrs = []
  588. let merged = false
  589. for (a of n.attrs) {
  590. if (a.name == 'class' && a.text != null) { attrs.push({ name = 'class'; text = a.text + ' ' + joinClasses(classes); }) merged = true }
  591. else if (a.name == 'class') { attrs.push({ name = 'class'; member = a.member; ref = a.ref; extra = joinClasses(classes); }) merged = true }
  592. else { attrs.push(a) }
  593. }
  594. if (!merged) { attrs.push({ name = 'class'; text = joinClasses(classes); }) }
  595. out.push({ k = 'el'; tag = n.tag; key = n.key; path = n.path; attrs = attrs; children = n.children; site = n.site; })
  596. } else { out.push(n) }
  597. }
  598. return out
  599. }
  600. joinClasses(classes) {
  601. let out = ''
  602. for (c of classes) { out = out == '' ? c : out + ' ' + c }
  603. return out
  604. }
  605. // Does this element hold only elements (and control flow around them)? Then its
  606. // children may each take a line. A text or member child makes it inline. The
  607. // shell's `slot` counts as an element: the child page stands there as a block.
  608. blocky(&node, &ctx) {
  609. if (verbatimTags.includes(node.tag)) { return false }
  610. if (node.children.length == 0) { return false }
  611. for (c of node.children) {
  612. if (c.k == 'text' || c.k == 'field' || c.k == 'expr') { return false }
  613. if (c.k == 'member' && !(c.name == 'slot' && ctx.slot != null)) { return false }
  614. }
  615. return true
  616. }
  617. // Elements one per line at `indent`; `for` and `if` unfold into lines at the same
  618. // level. Anything else that appears here renders inline at the end of the run.
  619. block(&nodes, &page, &rows, &ctx, indent) {
  620. let out = ''
  621. for (n of nodes) {
  622. if (n.k == 'el') {
  623. out = out + newline + indent + html(&n, &page, &rows, &ctx, indent)
  624. } else if (n.k == 'component') {
  625. out = out + html(&n, &page, &rows, &ctx, indent)
  626. } else if (n.k == 'for') {
  627. let list = value(&n.list, &page, &rows)
  628. if (list != null) {
  629. let ri = 0
  630. let keys = listKeys(&list)
  631. for (entry of list) {
  632. let inner = rows + {}
  633. inner[n.row] = entry
  634. out = out + block(&n.body, &page, &inner, rowCtx(&ctx, keys[ri]), indent)
  635. ri = ri + 1
  636. }
  637. }
  638. } else if (n.k == 'if') {
  639. if (value(&n.cond, &page, &rows)) { out = out + block(&n.then, &page, &rows, &ctx, indent) }
  640. else { out = out + block(&n.other, &page, &rows, &ctx, indent) }
  641. } else if (n.k == 'member' && n.name == 'slot' && ctx.slot != null) {
  642. out = out + newline + indent + ctx.slot
  643. } else if (n.k != 'on') {
  644. out = out + html(&n, &page, &rows, &ctx, indent)
  645. }
  646. }
  647. return out
  648. }
  649. // HTML of a whole View tree (a list of nodes), compact when `indent` is null
  650. render(&nodes, &page, &rows, &ctx, indent) {
  651. if (indent != null) { return block(&nodes, &page, &rows, &ctx, indent) }
  652. let out = ''
  653. for (n of nodes) { out = out + html(&n, &page, &rows, &ctx, null) }
  654. return out
  655. }

Branches

Latest commits

  • aa056c03ident: a startup error prints its message before exit 1 (hybriel#247)mre
  • ee37a861ident#G48: gitoria's registration gets the wildcard origin of its repo addresses at load (gate tests/gitoria-origin.mjs)mre
  • dc6d486fident#27: the docs gate expects the new Access entry in the main menumre
  • d277633cident#27: README: identity access and the introspect endpointmre
  • e999b0d3ident#27: the Access tab — one key per identity for all its apps: give access in three steps, the overview matrix, a cell's sheet (since, last use, level, rotate, revoke), the app's check POST /api/introspect, agent identities hidden from the login picker, an inbox icon in the header's icon style (gate: tests/access.mjs)mre
  • 436206fcident#47: deploy.sh runs the sign-in gate on the deploy's own port slot, like the sessionwrites and docs gates (the default 8700 can be taken by another process)mre
  • a4887ea3ident#47: the selector's sign-in frame no longer has a Continue button; the identity row itself is the link (gate tests/signin.mjs fails on the old code)mre
  • d8ec703dident#45: /docs shows docs/IDENT.md (hl:markdown, vendored from ac1ed9da), one Docs entry in the main menu, README for developers only, gates tests/docs.mjs and tests/sessionwrites.mjs in deploy.shmre
  • ec6aad35Merge branch 'a28g-amp-callsites' into d45-docsmre
  • 6bfbd3f2ident#45: docs/IDENT.md, what ident is and how it works; CONCEPT.md marks one id per app as droppedmre
  • c0edca26ident#28 A28G: write & at every call that hands a session to an &-parameter; gate tests/sessionwrites.mjs pins that sign-in, pending address, embed sign-in and selector land in the session filesmre
  • 80965c08ident#28: README says the deploy goes through the standbymre
  • 2f41670cident#28: ident-standby service on port 45012 (no background round in the standby) and deploy.sh hands over through standby-swap.shmre
  • a544b261ident#28: README names the new vendored Hybrielmre
  • 716860b0ident#28: gates follow Hybriel ac1ed9da (lazy sessions, meta attribute order, JSON key order, default X-Frame-Options switched off for the embed page)mre
  • 889e00fbident#28: Hybriel master ac1ed9da (sessions reload a newer file, a replayed reconnect carries the server session); a startup fault exits 1 (on Error)mre
  • f5a55f8aident#11: apps can ask for nickname, firstname and lastname (fields list on /api/exchange, new POST /api/profile); empty fields come back null; gate checks, READMEmre
  • d8be66ebident: report and log line for the widget cache and frame height changemre
  • 2068f61aident#31: browsers revalidate the widget scripts on every load; the sign-in frame is exactly as tall as the current step (the frame page reports its height, selector.js accepts it from ident only)mre
  • a22d2daaident#31: the sign-in frame is no taller than the tallest step needs (plain CSS height per width), gate check for itmre