gitoriaLog in with ident

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit6722b72d6722b72ddeploy.sh: never send .git or .gitignore to Byrodinmre6722b72d/plugins/web/compile.hl

46.0 KB

  1. // plugins/web/compile.hl — THE FRAMEWORK COMPILES EACH COMPONENT.
  2. //
  3. // Mission 313 (the creator, 15 Sep 14:30: "clientside interpreter + table against
  4. // component code per component, no tables needed"). The old hl:web ships ONE generic client
  5. // that walks a tree the seed carries: it pairs elements with nodes, asks a table which
  6. // members each element shows, asks `view.value` what each bound spot holds, and does it
  7. // again on every paint. hl:web ships the ANSWERS instead — this file writes each
  8. // component's own code, once, where the old hl:web would have walked.
  9. //
  10. // WHAT IS EMITTED, per component, beside the class `hlJs` compiled:
  11. //
  12. // v(c,m,o,host,i,rows,rk,fill,cr,opt) the component's View, unrolled: one
  13. // statement per element, per handler, per
  14. // bound spot. `cr` false CLAIMS the server's
  15. // DOM (take host.children[i], step on), `cr`
  16. // true CREATES it — the two walks the old hl:web
  17. // spells out separately, which differ only in
  18. // where an element comes from.
  19. // b["<for site>"] one `for` body — the row factory the keyed
  20. // region builds a row with.
  21. // t/e["<if site>"] an `if`'s two branches.
  22. // f["<reference site>"] the fragment this component writes into a
  23. // child's `slot` — built in THIS frame, which
  24. // is why it is this component's code.
  25. // p["<element site>"] the paint statements: `p` writes what one
  26. // member feeds (the old hl:web's paint(&m,s,name)),
  27. // `r` rewrites a row's element where the DOM
  28. // does not already hold it (rowPaint).
  29. //
  30. // The generated code calls a FIXED runtime (client.hl) for everything that is not this
  31. // component: the mount records, the keyed region diff, the binding edges, the
  32. // derivations, the socket, navigation. It never looks anything up in a table — the
  33. // names, the sites and the write sets are written into the statements.
  34. //
  35. // It is TEXT the framework produces, served from memory beside the module (WebFramework
  36. // `module`), never a JavaScript file under plugins/.
  37. import { newline, tab } from './view.hl'
  38. import View from './view.hl'
  39. // ---- the emitted half --------------------------------------------------------------
  40. module(gen, key, nodes) {
  41. fns = [] // the sub-walks: row bodies, if branches, fills
  42. done = {}
  43. rowVars = []
  44. tbl = tableOf(gen, key) // what the language says about this file, read once
  45. regions = regionIndex(tbl) // a `for`'s list reads, an `if`'s condition reads
  46. // A SHELL'S VIEW IS A `body` ELEMENT and the document already has one: the walk runs
  47. // against document.body itself, so the root list is that element's children and the
  48. // element only takes the marks a region rebuild on it would need.
  49. let roots = nodes
  50. let bodySite = null
  51. if (nodes.length == 1 && nodes[0].k == 'el' && nodes[0].tag == 'body') {
  52. roots = nodes[0].children
  53. bodySite = nodes[0].site
  54. }
  55. let root = walkTop(key, roots)
  56. if (bodySite != null) {
  57. root = root.replace('let r=rows;', 'let r=rows;await c.markHost(m,host,' + str(bodySite) + ');')
  58. }
  59. let src = ';(function(){var g=(typeof globalThis!=="undefined"?globalThis:window);'
  60. src = src + 'var C=(g.__hlC=g.__hlC||{});'
  61. // the element a claim takes or a create makes — the one shape both walks share
  62. // THE PRELUDE, one per module: the element a claim takes or a create makes, and the
  63. // two reads. A read written out in full — `(r["title"]!=null?r["title"]:inst["title"])`
  64. // — is 45 bytes and a component's View holds dozens of them; named, it is 16. The
  65. // rule is the same rule (a `for` row variable in scope wins over a member of the
  66. // same name, and a field path walks into it), spelled once.
  67. // A claim also moves the host's CURSOR — the last node the walk took there — which is
  68. // where a text leaf and a region's mark are looked for next (client.hl `leafNode`,
  69. // `markAt`).
  70. src = src + 'function A(cr,host,i,tag){if(cr){return document.createElement(tag);}var e=host.children[i];if(e!=null){host.__hlCur=e;}return e;}'
  71. src = src + 'function S(v){return v==null?"":""+v;}'
  72. src = src + 'function RV(r,q,n){return r[n]!=null?r[n]:q[n];}'
  73. src = src + 'function RF(r,q,n,p){var v=RV(r,q,n);for(var k=0;k<p.length;k++){if(v==null)return null;v=v[p[k]];}return v;}'
  74. src = src + 'var B={},T={},E={},F={},P={};'
  75. // ONE FUNCTION PER LIST, however many sites reach it: a nested walk is written once
  76. // and named — emitting it per site made the reference app's home page 215 KB of the
  77. // same code over and over.
  78. let wi = 0
  79. for (f of fns) {
  80. wi = wi + 1
  81. let name = 'w' + wi
  82. src = src + 'var ' + name + '=' + f.src + ';'
  83. for (site of f.sites) { src = src + f.slot + '[' + str(site) + ']=' + name + ';' }
  84. }
  85. let paints = []
  86. collectPaints(&paints, roots)
  87. for (e of paints) { src = src + 'P[' + str(e.site) + ']={p:' + e.paint + ',r:' + e.row + '};' }
  88. src = src + 'C[' + str(key) + ']={v:' + root + ',b:B,t:T,e:E,f:F,p:P'
  89. // THE ANSWERS A LOOKUP STILL NEEDS, and only those. A local event's write set stands
  90. // at its own listener and a region's names at its own region — written into the
  91. // call. These three are asked for by NAME at run time and cannot be: an inbound
  92. // frame names its event, a routine handed down names the member that holds it, and
  93. // a write set names the derivations that follow it.
  94. src = src + ',ev:' + eventMap() + ',mw:' + memberMap() + ',dv:' + derivList() + '};'
  95. return src + '})();' + newline
  96. }
  97. tbl = null
  98. regions = {}
  99. // what a DECLARED handler writes, by event: one entry per event name, unioned
  100. eventMap() {
  101. let byEvent = {}
  102. for (h of tbl.handlers) {
  103. if (!h.nested) {
  104. let have = byEvent[h.event] == null ? [] : byEvent[h.event]
  105. for (w of closeWrites(tbl, h.writes, h.reads)) { if (!have.includes(w)) { have.push(w) } }
  106. byEvent[h.event] = have
  107. }
  108. }
  109. return mapOf(byEvent)
  110. }
  111. // A MEMBER HOLDING A ROUTINE: what calling it writes is what its initializer writes
  112. memberMap() {
  113. let byName = {}
  114. for (m of tbl.members) {
  115. if (m.nodes != null) { byName[m.name] = memberNames(m.nodes.writes) }
  116. }
  117. return mapOf(byName)
  118. }
  119. // EVERY INITIALIZER, IN DECLARATION ORDER, with the members it reads — the whole of
  120. // what a derivation needs; the class carries the computation itself as `__derive__`
  121. derivList() {
  122. let src = '['
  123. let first = true
  124. for (d of tbl.derivations) {
  125. if (!first) { src = src + ',' }
  126. first = false
  127. src = src + '{name:' + str(d.name) + ',site:' + str(d.site) + ',reads:' + list(memberNames(d.reads)) + '}'
  128. }
  129. return src + ']'
  130. }
  131. mapOf(byName) {
  132. let src = '{'
  133. let first = true
  134. for (k of byName.keys()) {
  135. if (!first) { src = src + ',' }
  136. first = false
  137. src = src + str(k) + ':' + list(byName[k])
  138. }
  139. return src + '}'
  140. }
  141. // the write set and the read set of the handler at this site, closed over the methods
  142. // it calls and the handlers it emits to — computed here, written into the listener
  143. handlerSets(site) {
  144. for (h of tbl.handlers) {
  145. if (h.nested && h.site == site) {
  146. return list(closeWrites(tbl, h.writes, h.reads)) + ',' + list(closeReads(tbl, h.reads))
  147. }
  148. }
  149. return '[],[]'
  150. }
  151. regionNamesAt(site) {
  152. let names = regions[site]
  153. return list(names == null ? [] : names)
  154. }
  155. fns = []
  156. // ---- the walk, unrolled ------------------------------------------------------------
  157. // `top` marks the View's own root list: an element there is a COMPOSITION ROOT — a
  158. // reference above may have overwritten some of its events and put classes on it, which
  159. // is what `opt` carries (the old hl:web's rootOverwrites and withRootClasses).
  160. walk(key, nodes) {
  161. return plain(key, nodes)
  162. }
  163. // a list's walk
  164. plain(key, nodes) {
  165. let body = ''
  166. for (n of nodes) { body = body + node(key, n, false, false) }
  167. return 'async function(c,m,o,host,i,rows,rk,fill,cr,opt){let inst=m.instance;let r=rows;' + body + 'return i;}'
  168. }
  169. // the View's own root list, whose elements are composition roots
  170. walkTop(key, nodes) {
  171. let body = ''
  172. for (n of nodes) { body = body + node(key, n, true, false) }
  173. return 'async function(c,m,o,host,i,rows,rk,fill,cr,opt){let inst=m.instance;let r=rows;' + body + 'return i;}'
  174. }
  175. // a sub-walk, registered under its own site so the runtime can reach it by name
  176. // A SUB-WALK IS WRITTEN ONCE, however often the compile reaches its list.
  177. sub(key, nodes, slot, site) {
  178. if (done[slot + ' ' + site] != null) { return null }
  179. done[slot + ' ' + site] = true
  180. fns.push({ slot = slot; sites = [site]; src = walk(key, nodes); })
  181. return null
  182. }
  183. done = {}
  184. node(key, n, top, filterOns) {
  185. if (n.k == 'el') { return elNode(key, n, top) }
  186. if (n.k == 'text') { return 'if(cr){host.appendChild(document.createTextNode(' + str(n.text) + '));}else{await c.leafAt(host,false,i,null,' + str(n.text) + ');}' }
  187. if (n.k == 'on') {
  188. // A REFERENCE ABOVE MAY HAVE OVERWRITTEN THIS EVENT on the composition root:
  189. // the root's own handler for it is dropped and the reference's stands instead
  190. // (the old hl:web's withoutOns, which applies to the root element's direct children).
  191. let guard = filterOns ? 'if(!(opt&&opt.over&&opt.over.indexOf(' + str(n.event) + ')>=0)){' : '{'
  192. return guard + 'await c.onAt(m,host,' + str(n.event) + ',' + str(n.site) + ',rows,' + handlerSets(n.site) + ');}'
  193. }
  194. if (n.k == 'member' && n.name == 'slot') {
  195. // the SHELL's slot is where the route component hangs; a child's slot is the
  196. // fragment its host wrote, and the host's frame builds it
  197. return 'if(m.isShell){i=await c.slotShell(m,host,i,cr);}else if(fill!=null){i=await c.slotFill(m,o,host,i,fill,cr);}'
  198. }
  199. // A TEXT LEAF STANDING IN A LIST — an `if` branch, a `for` body — beside elements or
  200. // alone: its own text node, and a site of the member it reads (or of the row it
  201. // stands in), painted by rewriting that node.
  202. if (n.k == 'member' || n.k == 'field') {
  203. let names = (n.name == 'Style' || rowVars.includes(n.name)) ? [] : [n.name]
  204. return 'await c.textAt(m,host,cr,function(inst,r){return ' + textRead(n) + ';},' + list(names) + ',rows,rk);'
  205. }
  206. if (n.k == 'for') {
  207. // THE ROW VARIABLE IS IN SCOPE INSIDE THE BODY, and a read of it is the ROW's,
  208. // not a member of the instance — the language resolves it that way and the site
  209. // sets must say the same, or a member of the same name would paint the row.
  210. rowVars.push(n.row)
  211. sub(key, n.body, 'B', n.site)
  212. rowVars.pop()
  213. return 'i=await c.forAt(m,o,host,i,' + str(n.site) + ',' + str(n.row) + ',function(inst,r){return ' + rawRead(n.list) + ';},rows,rk,fill,cr,' + regionNamesAt(n.site) + ');'
  214. }
  215. if (n.k == 'if') {
  216. sub(key, n.then, 'T', n.site)
  217. sub(key, n.other, 'E', n.site)
  218. return 'i=await c.ifAt(m,o,host,i,' + str(n.site) + ',function(inst,r){return ' + rawRead(n.cond) + ';},rows,rk,fill,cr,' + regionNamesAt(n.site) + ');'
  219. }
  220. if (n.k == 'component') {
  221. if (n.fill != null && n.fill.length > 0) { sub(key, n.fill, 'F', n.site) }
  222. return 'i=await c.kidAt(m,host,i,' + kidSpec(n) + ',rows,rk,fill,cr,opt);'
  223. }
  224. return ''
  225. }
  226. // ---- an element --------------------------------------------------------------------
  227. elNode(key, n, top) {
  228. let src = '{let el=A(cr,host,i,' + str(n.tag) + ');if(!cr){i=i+1;}'
  229. src = src + 'if(el==null){await c.acqFail(' + str(n.tag) + ',host);}else{'
  230. // the attributes, on the walk that BUILDS the element (a claim takes the server's)
  231. src = src + 'if(cr){' + createAttrs(n) + (top ? 'await c.rootClasses(el,opt);' : '') + 'host.appendChild(el);}'
  232. src = src + 'el.__hlRows=rows;el.__hlSite=' + str(n.site) + ';el.__hlKey=' + str(key) + ';'
  233. // the element's own named entries, for the literal a handler on it fires
  234. src = src + 'el.__hlLit=function(inst,r){return ' + litNamed(n) + ';};'
  235. let names = siteNames(n)
  236. let valueMember = 'null'
  237. for (a of n.attrs) { if (a.member != null && a.name == 'value') { valueMember = str(a.member) } }
  238. let sited = ''
  239. if (names.length > 0 || valueMember != 'null') {
  240. sited = 'await c.siteAt(m,el,' + list(names) + ',' + str(n.site) + ',' + valueMember + ',' + (n.tag == 'select' ? 'true' : 'false') + ',rows);'
  241. }
  242. let kids = ''
  243. // AN ELEMENT THAT HOLDS ONLY TEXT is painted whole (`textContent`, see the paints):
  244. // built, it gets that text as one node; claimed, the server's is taken as it stands
  245. let whole = textOf(n)
  246. if (whole != null) {
  247. kids = 'if(cr){host.appendChild(document.createTextNode(' + whole + '));}'
  248. for (c of n.children) { if (c.k == 'on') { kids = kids + node(key, c, false, top) } }
  249. }
  250. else if (mixed(n)) { kids = mixedKids(key, n, top) }
  251. else { for (c of n.children) { kids = kids + node(key, c, false, top) } }
  252. let inner = '{let host=el;let i=0;' + kids + '}'
  253. // A SELECT IS SITED AFTER ITS OPTIONS: the member decides which option stands, and
  254. // the read-back that `siteAt` does reports nothing before they exist.
  255. if (n.tag == 'select') { src = src + inner + 'if(cr){await c.applySelectAt(m,el,' + selectValue(n) + ');}' + sited }
  256. else { src = src + sited + inner }
  257. return src + '}}'
  258. }
  259. // A TEXT LEAF BESIDE AN ELEMENT OR A REGION (ticket #86 follow-up): `td { d.n if (…) {…} }`
  260. // cannot be painted through `textContent`, which would wipe the branch. Each RUN of
  261. // adjacent leaves is one text node, held on the element (`__hlLeaves`, by the run's
  262. // index) where the walk builds or claims it, and the paints rewrite that node alone.
  263. mixed(n) {
  264. if (textOf(n) != null) { return false }
  265. for (c of n.children) { if ((c.k == 'member' || c.k == 'field') && c.name != 'slot') { return true } }
  266. return false
  267. }
  268. isLeaf(c) { return c.k == 'text' || ((c.k == 'member' || c.k == 'field') && c.name != 'slot') }
  269. // the runs of adjacent leaves, in order; an `on` renders nothing and splits none
  270. runsOf(n) {
  271. let out = []
  272. let run = []
  273. for (c of n.children) {
  274. if (isLeaf(c)) { run.push(c) }
  275. else if (c.k != 'on') {
  276. if (run.length > 0) { out.push(run) }
  277. run = []
  278. }
  279. }
  280. if (run.length > 0) { out.push(run) }
  281. return out
  282. }
  283. runText(run) {
  284. let src = '""'
  285. for (c of run) { src = src + '+(' + (c.k == 'text' ? str(c.text) : textRead(c)) + ')' }
  286. return src
  287. }
  288. runFeeds(run) {
  289. for (c of run) { if (c.k != 'text') { return true } }
  290. return false
  291. }
  292. mixedKids(key, n, top) {
  293. let kids = ''
  294. let run = []
  295. let k = 0
  296. for (c of n.children) {
  297. if (isLeaf(c)) { run.push(c) }
  298. else {
  299. if (c.k != 'on' && run.length > 0) { kids = kids + leafRun(run, k) k = k + 1 run = [] }
  300. kids = kids + node(key, c, false, top)
  301. }
  302. }
  303. if (run.length > 0) { kids = kids + leafRun(run, k) }
  304. return kids
  305. }
  306. // EVERY CALL INTO THE CLIENT IS AWAITED: its methods are async in the browser, and one
  307. // not awaited runs its DOM work after the walk has gone on — a built leaf then lands
  308. // after the elements that follow it (ticket #104).
  309. leafRun(run, k) {
  310. if (!runFeeds(run)) { return 'if(cr){host.appendChild(document.createTextNode(' + runText(run) + '));}else{await c.leafAt(host,false,i,null,' + runText(run) + ');}' }
  311. return 'await c.leafAt(host,cr,i,' + k + ',' + runText(run) + ');'
  312. }
  313. // the attributes an element is BUILT with — a bound one written in, a literal one as it
  314. // stands. `value` is a property once the page is live and an attribute as the control's
  315. // default; a textarea has no value attribute at all and a select's value is an option.
  316. createAttrs(n) {
  317. let src = ''
  318. for (a of n.attrs) {
  319. if (isBool(a)) {
  320. src = src + '{' + boolWrite(a, rawRead(a.member != null ? { k = 'member'; name = a.member; } : a.ref)) + '}'
  321. } else {
  322. let rd = a.member != null ? readOf({ k = 'member'; name = a.member; }) : (a.ref != null ? readOf(a.ref) : str(a.text))
  323. src = src + '{let v=' + rd + ';'
  324. if (a.extra != null) { src = src + 'v=v+" "+' + str(a.extra) + ';' }
  325. if (n.tag == 'select' && a.name == 'value') { src = src + '}' }
  326. else if (a.name == 'value') {
  327. src = src + 'if(el.value!=null){el.value=v;' + (n.tag == 'textarea' ? '' : 'el.setAttribute("value",v);') + '}else{el.setAttribute("value",v);}}'
  328. } else { src = src + 'el.setAttribute(' + str(a.name) + ',v);}' }
  329. }
  330. }
  331. if (boundValue(n)) { src = src + 'el.setAttribute("autocomplete","off");' }
  332. return src
  333. }
  334. // a BOUND boolean attribute — a literal one was decided at build (WebFramework `element`)
  335. isBool(a) {
  336. return (a.member != null || a.ref != null) && v.isBoolAttr(a.name)
  337. }
  338. // A BOUND BOOLEAN ATTRIBUTE, WRITTEN (ticket #33): set when the value is on, REMOVED when
  339. // it is off — the markup's rule (view.boolAttrs) — and the DOM property with it, because
  340. // once the page is live `checked` is the property and the attribute only its default.
  341. boolWrite(a, raw) {
  342. let prop = a.name == 'readonly' ? 'readOnly' : a.name
  343. return 'let on=!!' + raw + ';if(on){el.setAttribute(' + str(a.name) + ',"");}else if(el.removeAttribute){el.removeAttribute(' + str(a.name) + ');}if(' + str(prop) + ' in el&&el[' + str(prop) + ']!==on){el[' + str(prop) + ']=on;}'
  344. }
  345. // view.hl's `boundValue`, to the letter: a FORM CONTROL whose `value` reads a member or
  346. // a field, and not one the author gave an `autocomplete` of its own
  347. boundValue(n) {
  348. if (n.tag != 'input' && n.tag != 'textarea' && n.tag != 'select') { return false }
  349. let bound = false
  350. for (a of n.attrs) {
  351. if (a.name == 'autocomplete') { return false }
  352. if (a.name == 'value' && (a.member != null || a.ref != null)) { bound = true }
  353. }
  354. return bound
  355. }
  356. selectValue(n) {
  357. for (a of n.attrs) {
  358. if (a.name == 'value') {
  359. if (a.member != null) { return readOf({ k = 'member'; name = a.member; }) }
  360. if (a.ref != null) { return readOf(a.ref) }
  361. return str(a.text)
  362. }
  363. }
  364. return 'null'
  365. }
  366. // the element's own entries as the literal sees them — its attributes by name, then the
  367. // `for` row variables in scope (the row rides on the value)
  368. litNamed(n) {
  369. let src = '{'
  370. let first = true
  371. for (a of n.attrs) {
  372. if (!first) { src = src + ',' }
  373. first = false
  374. let v = a.member != null ? rawRead({ k = 'member'; name = a.member; }) : (a.ref != null ? rawRead(a.ref) : str(a.text))
  375. src = src + str(a.name) + ':' + v
  376. }
  377. return src + '}'
  378. }
  379. // the reference's own description, written at its use site: what it binds, what it
  380. // overwrites, the classes the host's `#Child` rule puts on it
  381. kidSpec(n) {
  382. let src = '{key:' + str(n.key) + ',kid:' + str(kidKeyOf(n)) + ',site:' + str(n.site) + ',fill:' + ((n.fill != null && n.fill.length > 0) ? 'true' : 'false')
  383. src = src + ',bindings:['
  384. let first = true
  385. for (b of n.bindings) {
  386. if (!first) { src = src + ',' }
  387. first = false
  388. src = src + '{name:' + str(b.name) + ',member:' + (b.member == null ? 'null' : str(b.member))
  389. src = src + ',ref:' + (b.ref == null ? 'null' : 'function(inst,r){return ' + rawRead(b.ref) + ';}')
  390. src = src + ',refName:' + (b.ref == null ? 'null' : str(b.ref.name))
  391. src = src + ',text:' + (b.text == null ? 'null' : 'true') + ',value:' + (b.text == null ? 'null' : refLiteral(b)) + '}'
  392. }
  393. src = src + '],ons:['
  394. first = true
  395. for (o of n.ons) {
  396. if (!first) { src = src + ',' }
  397. first = false
  398. src = src + '{event:' + str(o.event) + ',site:' + str(o.site) + ',sets:[' + handlerSets(o.site) + ']}'
  399. }
  400. src = src + '],classes:' + list(n.classes == null ? [] : n.classes) + '}'
  401. return src
  402. }
  403. // view.hl's kidKey: the reference's path, joined — the key the server mounted it under
  404. kidKeyOf(n) {
  405. let out = ''
  406. for (p of n.path) { out = out + '/' + p }
  407. return out
  408. }
  409. // the value a literal binding carries — view.refValue, folded at compile time
  410. refLiteral(b) {
  411. if (b.kind == 'folded') { return jsValue(b.value) }
  412. if (b.kind == 'number') { return '' + b.text }
  413. if (b.kind == 'boolean') { return b.text == 'true' ? 'true' : 'false' }
  414. if (b.kind == 'null_literal') { return 'null' }
  415. return str(b.text)
  416. }
  417. jsValue(v) {
  418. if (v == null) { return 'null' }
  419. if (hlTypeName(v) == 'String') { return str(v) }
  420. if (hlTypeName(v) == 'Boolean') { return v ? 'true' : 'false' }
  421. if (hlTypeName(v) == 'Number') { return '' + v }
  422. return JSON.stringify(v)
  423. }
  424. // ---- the site set: what this element SHOWS ------------------------------------------
  425. // The old hl:web asked the module's table which members the element at this site reads. The
  426. // answer is a fact about the View, so it is written in: the members its bound
  427. // attributes and its text leaves name. `Style` is folded into a class name at build and
  428. // the shell's `slot` is swapped by navigation — neither is ever painted, and neither is
  429. // a member read that reaches here (a `for` row variable resolves to its row).
  430. siteNames(n) {
  431. let out = []
  432. for (a of n.attrs) {
  433. if (a.member != null && a.member != 'Style' && !rowVars.includes(a.member) && !out.includes(a.member)) { out.push(a.member) }
  434. // A FIELD OF A MEMBER (`value = f.title`, ticket #93) is a read of the member:
  435. // a write to `f.title`, or a new `f`, moves `f`, and this element repaints
  436. if (a.ref != null && a.ref.name != 'Style' && !rowVars.includes(a.ref.name) && !out.includes(a.ref.name)) { out.push(a.ref.name) }
  437. }
  438. for (c of n.children) {
  439. if ((c.k == 'member' || c.k == 'field') && c.name != 'slot' && c.name != 'Style' && !rowVars.includes(c.name)) {
  440. if (!out.includes(c.name)) { out.push(c.name) }
  441. }
  442. }
  443. return out
  444. }
  445. rowVars = []
  446. // ---- the paints ---------------------------------------------------------------------
  447. collectPaints(&out, nodes) {
  448. for (n of nodes) {
  449. if (n.k == 'el') {
  450. let paint = paintFn(n)
  451. let row = rowFn(n)
  452. if (paint != null || row != 'null') {
  453. out.push({ site = n.site; paint = paint == null ? 'null' : paint; row = row; })
  454. }
  455. collectPaints(&out, n.children)
  456. } else if (n.k == 'for') { collectPaints(&out, n.body) }
  457. else if (n.k == 'if') { collectPaints(&out, n.then) collectPaints(&out, n.other) }
  458. else if (n.k == 'component') { if (n.fill != null) { collectPaints(&out, n.fill) } }
  459. }
  460. return null
  461. }
  462. // `paint(&m, s, name)`, compiled: the bound attributes under the member each reads, and
  463. // the text where the member that moved stands among the leaves.
  464. paintFn(n) {
  465. let body = ''
  466. for (a of n.attrs) {
  467. if (a.member != null) {
  468. body = body + 'if(n===' + str(a.member) + '){' + attrWrite(n, a, readOf({ k = 'member'; name = a.member; })) + '}'
  469. } else if (a.ref != null) {
  470. // a field of a member paints when the MEMBER moves (ticket #93)
  471. body = body + 'if(n===' + str(a.ref.name) + '){' + attrWrite(n, a, readOf(a.ref)) + '}'
  472. }
  473. }
  474. let text = textOf(n)
  475. if (text != null) {
  476. let guard = ''
  477. for (c of n.children) {
  478. if ((c.k == 'member' || c.k == 'field') && c.name != 'slot') {
  479. if (guard != '') { guard = guard + '||' }
  480. guard = guard + 'n===' + str(c.name)
  481. }
  482. }
  483. if (guard != '') { body = body + 'if(' + guard + '){el.textContent=' + text + ';}' }
  484. } else if (mixed(n)) {
  485. let k = 0
  486. for (run of runsOf(n)) {
  487. let guard = ''
  488. for (c of run) {
  489. if (c.k != 'text') {
  490. if (guard != '') { guard = guard + '||' }
  491. guard = guard + 'n===' + str(c.name)
  492. }
  493. }
  494. if (guard != '') { body = body + 'if((' + guard + ')&&el.__hlLeaves&&el.__hlLeaves[' + k + ']){el.__hlLeaves[' + k + '].data=' + runText(run) + ';}' }
  495. k = k + 1
  496. }
  497. }
  498. if (body == '') { return null }
  499. return 'function(el,inst,r,n){' + body + '}'
  500. }
  501. // `rowPaint(&m, el)`, compiled: every bound spot, written only where the DOM does not
  502. // already hold it — which is why a push into 2000 rows costs the 1999 nothing.
  503. rowFn(n) {
  504. let body = ''
  505. for (a of n.attrs) {
  506. if (isBool(a)) {
  507. body = body + '{' + boolWrite(a, rawRead(a.member != null ? { k = 'member'; name = a.member; } : a.ref)) + '}'
  508. } else if (a.member != null || a.ref != null) {
  509. let one = 'let v=' + readOf(a.member != null ? { k = 'member'; name = a.member; } : a.ref) + ';'
  510. if (a.extra != null) { one = one + 'v=v+" "+' + str(a.extra) + ';' }
  511. if (n.tag == 'select' && a.name == 'value') { one = one + 'if(el.value!==v){el.value=v;}' }
  512. else {
  513. one = one + 'if(el.getAttribute(' + str(a.name) + ')!==v){el.setAttribute(' + str(a.name) + ',v);}'
  514. if (a.name == 'value') { one = one + 'if(el.value!=null&&el.value!==v){el.value=v;}' }
  515. }
  516. body = body + '{' + one + '}'
  517. }
  518. }
  519. let text = textOf(n)
  520. let feeds = false
  521. for (c of n.children) { if ((c.k == 'member' || c.k == 'field') && c.name != 'slot') { feeds = true } }
  522. if (text != null && feeds) { body = body + '{let t=' + text + ';if(el.textContent!==t){el.textContent=t;}}' }
  523. if (text == null && feeds) {
  524. let k = 0
  525. for (run of runsOf(n)) {
  526. if (runFeeds(run)) { body = body + '{let x=el.__hlLeaves&&el.__hlLeaves[' + k + '];let t=' + runText(run) + ';if(x&&x.data!==t){x.data=t;}}' }
  527. k = k + 1
  528. }
  529. }
  530. if (body == '') { return 'null' }
  531. return 'function(el,inst,r){' + body + '}'
  532. }
  533. attrWrite(n, a, v) {
  534. if (isBool(a)) { return boolWrite(a, rawRead(a.member != null ? { k = 'member'; name = a.member; } : a.ref)) }
  535. let src = 'let v=' + v + ';'
  536. if (a.extra != null) { src = src + 'v=v+" "+' + str(a.extra) + ';' }
  537. if (n.tag == 'select' && a.name == 'value') { return src + 'el.value=v;' }
  538. src = src + 'el.setAttribute(' + str(a.name) + ',v);'
  539. if (a.name == 'value') { src = src + 'if(el.value!=null){el.value=v;}' }
  540. return src
  541. }
  542. textOf(n) {
  543. let parts = []
  544. let content = 0
  545. let feeds = false
  546. for (c of n.children) {
  547. if (c.k != 'on') {
  548. content = content + 1
  549. if (c.k == 'text') { parts.push(str(c.text)) }
  550. else if ((c.k == 'member' || c.k == 'field') && c.name != 'slot') { parts.push(textRead(c)) feeds = true }
  551. }
  552. }
  553. if (!feeds) { return null }
  554. if (parts.length != content) { return null }
  555. let src = '""'
  556. for (p of parts) { src = src + '+(' + p + ')' }
  557. return src
  558. }
  559. // ---- a read, written in ---------------------------------------------------------------
  560. // `view.value`'s own rule as an expression: a `for` row variable in scope wins over a
  561. // member of the same name, and a field path walks into it. `readOf` answers the STRING
  562. // the DOM takes; `rawRead` the value itself (a binding, a list, a condition).
  563. readOf(node) { return '(""+' + rawRead(node) + ')' }
  564. // A READ SHOWN AS TEXT: null is no text, as the server writes it (view.hl `escape`)
  565. textRead(node) { return 'S(' + rawRead(node) + ')' }
  566. rawRead(node) {
  567. if (node == null) { return 'null' }
  568. if (node.k == 'text') { return str(node.text) }
  569. if (node.k != 'field') { return 'RV(r,inst,' + str(node.name) + ')' }
  570. return 'RF(r,inst,' + str(node.name) + ',' + list(node.path) + ')'
  571. }
  572. list(names) {
  573. let src = '['
  574. let first = true
  575. for (n of names) {
  576. if (!first) { src = src + ',' }
  577. first = false
  578. src = src + str(n)
  579. }
  580. return src + ']'
  581. }
  582. // a JavaScript string literal — JSON's escaping is a subset of JavaScript's, so a
  583. // source text's quotes, newlines and backslashes survive into the statement unchanged
  584. str(s) {
  585. if (s == null) { return '""' }
  586. return JSON.stringify('' + s)
  587. }
  588. // ---- WHAT THE LANGUAGE SAYS ABOUT THIS FILE, read HERE and written into the code ----
  589. // The old hl:web shipped the language's answers to the browser as data: `hlJs` writes each file's
  590. // members, handlers and methods — every node of every initializer, with the names it
  591. // reads — into the module, and the client reduces that table on every boot. The answers
  592. // are facts about the source, so this file asks for them once, at compile time, and
  593. // writes the RESULT into the statements: a handler's write set stands at the listener
  594. // that needs it, a region's names at the region, the derivations as a list of what to
  595. // re-run. The module carries no table at all (WebFramework `module` drops the one the
  596. // emitter writes).
  597. //
  598. // The reflection is the framework's own analysis generation — the same `gen` the View
  599. // trees come from. Nothing starts a second one: a handle is only valid inside the
  600. // generation that made it.
  601. tableOf(gen, key) {
  602. let members = []
  603. let derivations = []
  604. let handlers = []
  605. let methods = []
  606. for (m of hlMembers(gen, key)) {
  607. let entry = { name = m.name; nodes = null; }
  608. if (m.hasValue) {
  609. let n = syntaxTree(gen, key, m.node)
  610. entry.nodes = n
  611. derivations.push({ name = m.name; site = n.site; reads = n.reads; })
  612. nestedHandlers(&handlers, n)
  613. }
  614. members.push(entry)
  615. }
  616. for (h of hlEvents(gen, key).handles) {
  617. let n = syntaxTree(gen, key, h.node)
  618. handlers.push({ event = h.event; site = n.site; reads = n.reads; writes = n.writes; nested = false; })
  619. }
  620. for (mm of hlMethods(gen, key)) {
  621. if (!mm.inlineHandler) {
  622. let n = syntaxTree(gen, key, mm.node)
  623. methods.push({ name = mm.name; reads = n.reads; writes = n.writes; })
  624. }
  625. }
  626. return { members = members; derivations = derivations; handlers = handlers; methods = methods; }
  627. }
  628. // A NESTED `on` IS A DOM HANDLER, filed under its own site — the id the View node
  629. // carries. Its body is not the literal's syntax, so the node answers for it and the
  630. // walk stops there.
  631. nestedHandlers(&into, n) {
  632. if (n == null) { return null }
  633. if (n.tag == 'on_statement') {
  634. into.push({ event = n.event; site = n.site; reads = n.reads; writes = n.writes; nested = true; })
  635. return null
  636. }
  637. for (c of [n.value, n.list, n.body, n.condition, n.consequent, n.alternate, n.object, n.property, n.callee, n.left, n.right, n.operand]) {
  638. if (c != null) { nestedHandlers(&into, c) }
  639. }
  640. for (l of [n.entries, n.args, n.elements]) {
  641. if (l != null) { for (c of l) { nestedHandlers(&into, c) } }
  642. }
  643. return null
  644. }
  645. shortSite(site) {
  646. if (site == null) { return null }
  647. let at = site.indexOf(':')
  648. if (at < 0) { return site }
  649. let path = site.slice(0, at)
  650. let cut = path.lastIndexOf('/')
  651. if (cut < 0) { return site }
  652. return path.slice(cut + 1) + site.slice(at)
  653. }
  654. // the syntax of one node, recursively — hl:core/AST's `tree`, read off the framework's
  655. // own generation instead of starting one of its own
  656. syntaxTree(gen, key, node) {
  657. let n = hlSyntax(gen, key, node)
  658. if (n == null) { return null }
  659. // A SITE IS NAMED BY THE FILE'S BASE NAME. The reflection answers with the project
  660. // key ('components/sort_list.hl:77:26'); the View tree, the emitter's own
  661. // `__derive__` branches and every site this framework writes use the base name
  662. // ('sort_list.hl:77:26'). One spelling, or a lookup by site finds nothing.
  663. let out = { tag = n.tag; site = shortSite(n.site); reads = n.reads; writes = n.writes; }
  664. if (n.tag == 'object_expression') {
  665. let kids = []
  666. for (h of n.entries) { kids.push(syntaxTree(gen, key, h)) }
  667. out.entries = kids
  668. } else if (n.tag == 'object_property') {
  669. out.key = n.key
  670. out.value = syntaxTree(gen, key, n.value)
  671. } else if (n.tag == 'view_for') {
  672. out.list = syntaxTree(gen, key, n.list)
  673. out.body = syntaxTree(gen, key, n.body)
  674. } else if (n.tag == 'view_if') {
  675. out.condition = syntaxTree(gen, key, n.condition)
  676. out.consequent = syntaxTree(gen, key, n.consequent)
  677. out.alternate = syntaxTree(gen, key, n.alternate)
  678. } else if (n.tag == 'on_statement') {
  679. out.event = n.event
  680. } else if (n.tag == 'member_expression') {
  681. out.object = syntaxTree(gen, key, n.object)
  682. out.property = syntaxTree(gen, key, n.property)
  683. } else if (n.tag == 'call_expression') {
  684. out.callee = syntaxTree(gen, key, n.callee)
  685. let args = []
  686. for (h of n.args) { args.push(syntaxTree(gen, key, h)) }
  687. out.args = args
  688. } else if (n.tag == 'binary_expression') {
  689. out.left = syntaxTree(gen, key, n.left)
  690. out.right = syntaxTree(gen, key, n.right)
  691. } else if (n.tag == 'unary_expression') {
  692. out.operand = syntaxTree(gen, key, n.operand)
  693. } else if (n.tag == 'conditional_expression') {
  694. out.condition = syntaxTree(gen, key, n.condition)
  695. out.consequent = syntaxTree(gen, key, n.consequent)
  696. out.alternate = syntaxTree(gen, key, n.alternate)
  697. } else if (n.tag == 'array_expression') {
  698. let els = []
  699. for (h of n.elements) { els.push(syntaxTree(gen, key, h)) }
  700. out.elements = els
  701. }
  702. return out
  703. }
  704. // ---- the reductions the browser used to make on every boot --------------------------
  705. // A `for`'s names are what its list reads, an `if`'s what its condition reads — filed
  706. // under the region's own site.
  707. regionIndex(t) {
  708. let idx = {}
  709. for (m of t.members) { if (m.nodes != null) { indexRegions(&idx, m.nodes) } }
  710. return idx
  711. }
  712. indexRegions(&idx, n) {
  713. if (n == null) { return null }
  714. if (n.tag == 'view_for') {
  715. idx[n.site] = memberNames(nodeReads(n.list))
  716. indexRegions(&idx, n.body)
  717. return null
  718. }
  719. if (n.tag == 'view_if') {
  720. idx[n.site] = memberNames(nodeReads(n.condition))
  721. indexRegions(&idx, n.consequent)
  722. indexRegions(&idx, n.alternate)
  723. return null
  724. }
  725. if (n.tag == 'on_statement') { return null }
  726. for (c of [n.value, n.list, n.body, n.condition, n.consequent, n.alternate, n.object, n.property, n.callee, n.left, n.right, n.operand]) {
  727. if (c != null) { indexRegions(&idx, c) }
  728. }
  729. for (l of [n.entries, n.args, n.elements]) {
  730. if (l != null) { for (c of l) { indexRegions(&idx, c) } }
  731. }
  732. return null
  733. }
  734. // the member reads of a read list — the language resolved every name, so this is a
  735. // filter and not a second resolution
  736. memberNames(reads) {
  737. let out = []
  738. if (reads == null) { return out }
  739. for (r of reads) {
  740. if (r.kind == 'member' && r.isStatic != true && !out.includes(r.name)) { out.push(r.name) }
  741. }
  742. return out
  743. }
  744. nodeReads(n) {
  745. if (n == null) { return [] }
  746. if (n.reads != null) { return n.reads }
  747. let out = []
  748. for (c of [n.value, n.list, n.body, n.condition, n.consequent, n.alternate, n.object, n.property, n.callee, n.left, n.right, n.operand]) {
  749. if (c != null) { for (r of nodeReads(c)) { out.push(r) } }
  750. }
  751. for (l of [n.entries, n.args, n.elements]) {
  752. if (l != null) { for (c of l) { for (r of nodeReads(c)) { out.push(r) } } }
  753. }
  754. return out
  755. }
  756. // A ROUTINE'S WRITES, PLUS THE WRITES OF EVERYTHING IT REACHES — the method it calls
  757. // and the handler it emits to, transitively. Both are READS the language resolved, so
  758. // this is a walk of its answer and never a guess.
  759. closeWrites(t, writes, reads) {
  760. let out = memberNames(writes)
  761. let queue = []
  762. reached(&queue, reads)
  763. let seen = {}
  764. while (queue.length > 0) {
  765. let at = queue.shift()
  766. if (seen[at] == null) {
  767. seen[at] = true
  768. let sp = at.indexOf(' ')
  769. let kind = at.slice(0, sp)
  770. let name = at.slice(sp + 1)
  771. for (mm of t.methods) {
  772. if (kind == 'method' && mm.name == name) {
  773. for (w of memberNames(mm.writes)) { if (!out.includes(w)) { out.push(w) } }
  774. reached(&queue, mm.reads)
  775. }
  776. }
  777. for (h of t.handlers) {
  778. if (kind == 'handler' && !h.nested && h.event == name) {
  779. for (w of memberNames(h.writes)) { if (!out.includes(w)) { out.push(w) } }
  780. reached(&queue, h.reads)
  781. }
  782. }
  783. for (mb of t.members) {
  784. if (kind == 'member' && mb.name == name && mb.nodes != null) {
  785. for (w of memberNames(mb.nodes.writes)) { if (!out.includes(w)) { out.push(w) } }
  786. reached(&queue, nodeReads(mb.nodes))
  787. }
  788. }
  789. }
  790. }
  791. return out
  792. }
  793. // the mirror: a handler's reads plus the reads of everything it reaches — the
  794. // candidates for a routine a host handed down, whose writes only the host's table names
  795. closeReads(t, reads) {
  796. let out = memberNames(reads)
  797. let queue = []
  798. reached(&queue, reads)
  799. let seen = {}
  800. while (queue.length > 0) {
  801. let at = queue.shift()
  802. if (seen[at] == null) {
  803. seen[at] = true
  804. let sp = at.indexOf(' ')
  805. let kind = at.slice(0, sp)
  806. let name = at.slice(sp + 1)
  807. for (mm of t.methods) {
  808. if (kind == 'method' && mm.name == name) {
  809. for (r of memberNames(mm.reads)) { if (!out.includes(r)) { out.push(r) } }
  810. reached(&queue, mm.reads)
  811. }
  812. }
  813. for (h of t.handlers) {
  814. if (kind == 'handler' && !h.nested && h.event == name) {
  815. for (r of memberNames(h.reads)) { if (!out.includes(r)) { out.push(r) } }
  816. reached(&queue, h.reads)
  817. }
  818. }
  819. for (mb of t.members) {
  820. if (kind == 'member' && mb.name == name && mb.nodes != null) {
  821. for (r of memberNames(nodeReads(mb.nodes))) { if (!out.includes(r)) { out.push(r) } }
  822. reached(&queue, nodeReads(mb.nodes))
  823. }
  824. }
  825. }
  826. }
  827. return out
  828. }
  829. reached(&queue, reads) {
  830. if (reads == null) { return null }
  831. for (r of reads) {
  832. if (r.kind == 'method' || r.kind == 'handler' || (r.kind == 'member' && r.isStatic != true)) { queue.push(r.kind + ' ' + r.name) }
  833. }
  834. return null
  835. }
  836. // ---- THE SERVER RENDER, COMPILED --------------------------------------------------
  837. // The old hl:web rendered a page by WALKING the View tree per request: for every node it asks
  838. // what kind it is, for every element it builds the attribute string, and it decides the
  839. // indentation of every line again, on every request, for every one of the 121 mounts a
  840. // page may hold. Every one of those decisions is a fact about the View.
  841. //
  842. // So a component's render is compiled ONCE into a list of STEPS — constant text, and a
  843. // read with its own closure — and a request is a concatenation. The structure (tags,
  844. // static attributes, the readable form's line breaks, which element is a block) is
  845. // decided at compile time; only the values are asked for per request.
  846. //
  847. // KEYED BY THE INDENT IT IS RENDERED AT, because that is the only thing about a
  848. // component's HTML that its caller decides: the shell renders at one tab, the page at
  849. // the depth the slot stands, a fill compactly (null). An app has a handful of those.
  850. serverSteps(key, nodes, indent, classes, slotted) {
  851. let ck = key + '|' + (indent == null ? '~' : indent) + '|' + joinList(classes) + '|' + (slotted ? '1' : '0')
  852. if (steps[ck] == null) {
  853. steps[ck] = blockSteps(classes == null || classes.length == 0 ? nodes : v.withRootClasses(nodes, classes), indent, slotted)
  854. }
  855. return steps[ck]
  856. }
  857. // A `slot` THE HOST FILLED COUNTS AS CONTENT, and an empty one does not — the one
  858. // thing about a component's shape its caller decides, so it is part of the key.
  859. staticVoid = [ 'area' 'base' 'br' 'col' 'embed' 'hr' 'img' 'input' 'link' 'meta' 'source' 'track' 'wbr' ]
  860. staticVerbatim = [ 'textarea' 'pre' 'script' 'style' ]
  861. blocky(n, slotted) {
  862. if (staticVerbatim.includes(n.tag)) { return false }
  863. if (n.children.length == 0) { return false }
  864. for (c of n.children) {
  865. if (c.k == 'text' || c.k == 'field') { return false }
  866. if (c.k == 'member' && !(c.name == 'slot' && slotted)) { return false }
  867. }
  868. return true
  869. }
  870. steps = {}
  871. v = new View
  872. joinList(l) {
  873. if (l == null) { return '' }
  874. let out = ''
  875. for (c of l) { out = out + ',' + c }
  876. return out
  877. }
  878. // `render(nodes, …, indent)`: the readable form puts each element on its own line at
  879. // `indent`, the compact one writes them end to end.
  880. blockSteps(nodes, indent, slotted) {
  881. let out = []
  882. for (n of nodes) {
  883. if (indent != null) {
  884. if (n.k == 'el') { push(&out, { k = 's'; v = newline + indent; }) nodeSteps(&out, n, indent, slotted) }
  885. else if (n.k == 'component') { nodeSteps(&out, n, indent, slotted) }
  886. else if (n.k == 'for') { forSteps(&out, n, indent, slotted) }
  887. else if (n.k == 'if') { ifSteps(&out, n, indent, slotted) }
  888. else if (n.k == 'member' && n.name == 'slot') { push(&out, { k = 'slotline'; v = newline + indent; }) }
  889. else if (n.k != 'on') { nodeSteps(&out, n, indent, slotted) }
  890. } else {
  891. nodeSteps(&out, n, null, slotted)
  892. }
  893. }
  894. return out
  895. }
  896. forSteps(&out, n, indent, slotted) {
  897. out.push({ k = 'for'; row = n.row; read = readFn(n.list); body = blockSteps(n.body, indent, slotted); })
  898. return null
  899. }
  900. ifSteps(&out, n, indent, slotted) {
  901. out.push({ k = 'if'; read = readFn(n.cond); then = blockSteps(n.then, indent, slotted); other = blockSteps(n.other, indent, slotted); })
  902. return null
  903. }
  904. // one node's steps — `html(node, …)`, decided once
  905. nodeSteps(&out, n, indent, slotted) {
  906. if (n.k == 'text') { push(&out, { k = 's'; v = v.escape(n.text); }) return null }
  907. if (n.k == 'on') { return null }
  908. if (n.k == 'member' && n.name == 'slot') { out.push({ k = 'slot'; }) return null }
  909. if (n.k == 'member' || n.k == 'field') { out.push({ k = 'r'; read = readFn(n); }) return null }
  910. if (n.k == 'for') { forSteps(&out, n, indent, slotted) return null }
  911. if (n.k == 'if') { ifSteps(&out, n, indent, slotted) return null }
  912. if (n.k == 'component') {
  913. out.push({ k = 'kid'; path = n.path; cls = n.cls; key = n.key; classes = n.classes; indent = indent;
  914. fill = (n.fill == null || n.fill.length == 0) ? null : blockSteps(n.fill, null, slotted); })
  915. return null
  916. }
  917. if (n.k == 'el') { elSteps(&out, n, indent, slotted) return null }
  918. push(&out, { k = 's'; v = '<!-- ' + n.tag + ' not rendered yet -->'; })
  919. return null
  920. }
  921. elSteps(&out, n, indent, slotted) {
  922. let attrs = []
  923. let valueRead = null // a textarea's value is its content
  924. let selRead = null // a select's value selects an option
  925. for (a of n.attrs) {
  926. if (n.tag == 'textarea' && a.name == 'value') { valueRead = attrRead(a) }
  927. else if (n.tag == 'select' && a.name == 'value') { selRead = attrRead(a) }
  928. // a bound BOOLEAN attribute is its presence (ticket #33): ` checked=""` or nothing
  929. else if ((a.member != null || a.ref != null) && v.isBoolAttr(a.name)) { attrs.push({ k = 'b'; name = a.name; read = attrRead(a); }) }
  930. else if (a.member != null || a.ref != null) {
  931. push(&attrs, { k = 's'; v = ' ' + a.name + '="'; })
  932. attrs.push({ k = 'r'; read = attrRead(a); })
  933. push(&attrs, { k = 's'; v = v.extraOf(a) + '"'; })
  934. } else {
  935. push(&attrs, { k = 's'; v = ' ' + a.name + '="' + v.escape(a.text) + '"'; })
  936. }
  937. }
  938. if (v.boundValue(n)) { push(&attrs, { k = 's'; v = ' autocomplete="off"'; }) }
  939. // the children, in the form this element takes: a block puts them one per line
  940. let kids = []
  941. if (indent != null && blocky(n, slotted)) {
  942. kids = blockSteps(n.children, indent + tab, slotted)
  943. push(&kids, { k = 's'; v = newline + indent; })
  944. } else {
  945. kids = blockSteps(n.children, null, slotted)
  946. }
  947. if (valueRead != null) { kids = [{ k = 'r'; read = valueRead; }] }
  948. // AN OPTION IS THE ONE ELEMENT WHOSE OWN TAG DEPENDS ON THE VALUE ABOVE IT: it
  949. // carries `selected` when its value is the select's. Everything else is settled.
  950. if (n.tag == 'option') {
  951. out.push({ k = 'opt'; attrs = attrs; body = kids; ov = optionRead(n); })
  952. return null
  953. }
  954. push(&out, { k = 's'; v = '<' + n.tag; })
  955. for (a of attrs) { push(&out, a) }
  956. if (staticVoid.includes(n.tag)) { push(&out, { k = 's'; v = '>'; }) return null }
  957. push(&out, { k = 's'; v = '>'; })
  958. // A SELECT HANDS ITS VALUE TO ITS OPTIONS, however deep they stand: its children
  959. // run under it, and it is the only element that changes anything for them.
  960. if (n.tag == 'select') { out.push({ k = 'sel'; read = selRead; body = kids; }) }
  961. else { for (kk of kids) { push(&out, kk) } }
  962. push(&out, { k = 's'; v = '</' + n.tag + '>'; })
  963. return null
  964. }
  965. // CONSTANT TEXT MERGES. Two literal steps in a row are one string decided at compile
  966. // time — which is what makes a request a concatenation of a handful of pieces instead
  967. // of one per node.
  968. push(&out, step) {
  969. if (step.k == 's' && out.length > 0 && out[out.length - 1].k == 's') {
  970. let last = out[out.length - 1]
  971. last.v = last.v + step.v
  972. out[out.length - 1] = last
  973. return null
  974. }
  975. out.push(step)
  976. return null
  977. }
  978. // ---- the reads, each with its own closure ------------------------------------------
  979. // `view.value`'s rule, split once: a `for` row variable in scope wins over a member of
  980. // the same name, and a field path walks into it. The closure holds the name — there is
  981. // no node to look at per request.
  982. readFn(node) {
  983. if (node == null) { return (inst, rows) => { return null } }
  984. if (node.k == 'text') { let t = v.literalValue(node.kind, node.text) return (inst, rows) => { return t } }
  985. let name = node.name
  986. if (node.k != 'field') {
  987. return (inst, rows) => { return rows[name] != null ? rows[name] : inst[name] }
  988. }
  989. let path = node.path
  990. return (inst, rows) => {
  991. let val = rows[name] != null ? rows[name] : inst[name]
  992. for (f of path) {
  993. if (val == null) { return null }
  994. val = val[f]
  995. }
  996. return val
  997. }
  998. }
  999. attrRead(a) {
  1000. if (a.member != null) { return readFn({ k = 'member'; name = a.member; }) }
  1001. if (a.ref != null) { return readFn(a.ref) }
  1002. let t = a.text
  1003. return (inst, rows) => { return t }
  1004. }
  1005. // an option's own value: its `value` attribute, or null when it has none (the browser
  1006. // then reports its text, and the run compares against what it rendered)
  1007. optionRead(n) {
  1008. for (a of n.attrs) {
  1009. if (a.name == 'value') { return attrRead(a) }
  1010. }
  1011. return null
  1012. }
  1013. // ---- the run: a concatenation ------------------------------------------------------
  1014. // EVERY REGION IS WRITTEN BETWEEN TWO MARKS (tickets #103, #104): an `if` around the
  1015. // branch that stands, a `for` around its rows and each row around its own nodes. The
  1016. // client claims them as they are and builds the same ones, and a region's nodes are
  1017. // exactly what stands between its two marks — nothing it holds has to be remembered
  1018. // node by node, and text beside a region can never run into the region's text.
  1019. markOpen = '<!--[-->'
  1020. markClose = '<!--]-->'
  1021. runSteps(list, &inst, &rows, &ctx) {
  1022. let out = ''
  1023. for (s of list) {
  1024. if (s.k == 's') { out = out + s.v }
  1025. else if (s.k == 'r') { let f = s.read out = out + v.escape(f(&inst, &rows)) }
  1026. else if (s.k == 'b') { let f = s.read if (v.boolOn(f(&inst, &rows))) { out = out + ' ' + s.name + '=""' } }
  1027. else if (s.k == 'slot') { if (ctx.slot != null) { out = out + ctx.slot } }
  1028. else if (s.k == 'slotline') { if (ctx.slot != null) { out = out + s.v + ctx.slot } }
  1029. else if (s.k == 'for') {
  1030. let f = s.read
  1031. let list2 = f(&inst, &rows)
  1032. out = out + markOpen
  1033. if (list2 != null) {
  1034. let ri = 0
  1035. for (entry of list2) {
  1036. let inner = rows + {}
  1037. inner[s.row] = entry
  1038. let rc = v.rowCtx(&ctx, &entry, ri)
  1039. out = out + markOpen + runSteps(s.body, &inst, &inner, &rc) + markClose
  1040. ri = ri + 1
  1041. }
  1042. }
  1043. out = out + markClose
  1044. }
  1045. else if (s.k == 'if') {
  1046. let f = s.read
  1047. if (f(&inst, &rows)) { out = out + markOpen + runSteps(s.then, &inst, &rows, &ctx) + markClose }
  1048. else { out = out + markOpen + runSteps(s.other, &inst, &rows, &ctx) + markClose }
  1049. }
  1050. else if (s.k == 'opt') {
  1051. let head = runSteps(s.attrs, &inst, &rows, &ctx)
  1052. let body = runSteps(s.body, &inst, &rows, &ctx)
  1053. let mark = ''
  1054. if (ctx.sel != null) {
  1055. let ov = body
  1056. if (s.ov != null) { let f = s.ov ov = '' + f(&inst, &rows) }
  1057. if (ov == ctx.sel) { mark = ' selected' }
  1058. }
  1059. out = out + '<option' + head + mark + '>' + body + '</option>'
  1060. }
  1061. else if (s.k == 'sel') {
  1062. let f = s.read
  1063. let inner = ctx + {}
  1064. inner.sel = f == null ? null : '' + f(&inst, &rows)
  1065. out = out + runSteps(s.body, &inst, &rows, &inner)
  1066. }
  1067. else if (s.k == 'kid') {
  1068. let kid = ctx.kids[v.kidKeyIn(s.path, ctx)]
  1069. if (kid == null) { out = out + '<!-- ' + s.cls + ' not mounted -->' }
  1070. else {
  1071. let fillText = s.fill == null ? null : runSteps(s.fill, &inst, &rows, &ctx)
  1072. let kidCtx = { slot = fillText; kids = kid.kids; }
  1073. let kidInst = kid.page
  1074. let empty = {}
  1075. out = out + runSteps(serverSteps(s.key, kid.view, s.indent, s.classes, fillText != null), &kidInst, &empty, &kidCtx)
  1076. }
  1077. }
  1078. }
  1079. return out
  1080. }

Branches

Latest commits

  • 6722b72ddeploy.sh: never send .git or .gitignore to Byrodinmre
  • be099807State of 2026-09-27, before the move to gitoriamre