tobogganSe connecter

packages

The Louise packages, each released on its own

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

89 lignes · 3152 octetsBrut · Historique
1/// The markup of a streamed page (ADR 0066's `async`, toboggan's `htms`):
2/// a page goes out with a placeholder where each slow part will go, then
3/// each part as it finishes, as a `<template>` and a `<swap-in>` marker
4/// that moves the template's content into its placeholder, then the end
5/// of the document. One inline script in the head defines the marker; an
6/// app may put it in every page's head itself (`script`), and `opened`
7/// then leaves it there.
8
9import std/html.{type Html, Attribute, Node}
10import std/list
11import std/string
12
13/// `skeleton` shown until the part `id` arrives: its root element gets the
14/// id and `aria-busy`; anything else is wrapped in a `<div>` that has them.
15pub fn placeholder :: id: String skeleton: Html -> Html
16{
17 let marks = [Attribute("id", id), Attribute("aria-busy", "true")]
18 match skeleton
19 { Node(tag, attributes, children) ->
20 Node(tag, list.append attributes marks, children)
21 , _ -> Node("div", marks, [skeleton])
22 }
23}
24
25/// The part `id`, rendered: its content in a template, then the marker
26/// that swaps it in when the browser reads it.
27pub fn part :: id: String content: String -> String
28{
29 "<template>{content}</template><swap-in for=\"{html.escape id}\"></swap-in>"
30}
31
32/// The script that defines `<swap-in>`, allowed by `nonce`. It replaces
33/// the placeholder with the template just before the marker, removes
34/// both, and announces each new element with a `swap-in:swapped` event
35/// (htmx can process it there).
36pub fn script :: nonce: String -> String
37{
38 "<script nonce=\"{html.escape nonce}\">{behaviour ()}</script>"
39}
40
41fn behaviour :: -> String
42{
43 string.join
44 [ "customElements.define(\"swap-in\",class extends HTMLElement\{connectedCallback()\{"
45 , "const t=this.previousElementSibling,s=document.getElementById(this.getAttribute(\"for\"));"
46 , "if(s&&t instanceof HTMLTemplateElement)\{const n=[...t.content.children];s.replaceWith(t.content);"
47 , "for(const e of n)e.dispatchEvent(new CustomEvent(\"swap-in:swapped\",\{bubbles:true\}))\}"
48 , "if(t instanceof HTMLTemplateElement)t.remove();this.remove()\}\})"
49 ]
50 ""
51}
52
53/// A whole page opened for streaming: up to its last `</body>`, with the
54/// script at the end of its head, and the rest, to send last. A head that
55/// already holds `script nonce` (an app that puts it in every page) is
56/// left as it is. `None` when the page has no head or no body end to
57/// split at.
58pub fn opened :: page: String nonce: String -> Option<(String, String)>
59{
60 match string.split page "</head>"
61 { [before, after, ..more] ->
62 {
63 let tail = string.join [after, ..more] "</head>"
64 match list.reverse (string.split tail "</body>")
65 { [last, kept, ..earlier] ->
66 {
67 let body = string.join (list.reverse [kept, ..earlier]) "</body>"
68 let head = scripted before nonce
69 Some((head <> "</head>" <> body, "</body>" <> last))
70 }
71 , _ -> None
72 }
73 }
74 , _ -> None
75 }
76}
77
78fn scripted :: head: String nonce: String -> String
79{
80 let defined = script nonce
81 if string.contains head defined
82 {
83 head
84 }
85 else
86 {
87 head <> defined
88 }
89}