
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1199 · main · 10 min 33 s
1Étoiles
0Forks
0Issues
| 1 | // Precompiled renderers (ADR 0054): a render of a statically known tree |
| 2 | // appends static chunks and escapes holes straight into the builder; |
| 3 | // the Element tree is never built. Behaviour is unchanged, on both |
| 4 | // backends — the holes' side effects keep their order, and a tree that |
| 5 | // escapes (matched on, rendered twice) is still built. |
| 6 | |
| 7 | import std/html.{type Element, type Html, raw, text} |
| 8 | import std/io |
| 9 | import std/list |
| 10 | |
| 11 | effect log |
| 12 | { |
| 13 | fn log :: msg: String -> () |
| 14 | } |
| 15 | |
| 16 | fn with_log :: action: fn() -> a / <log, io | e> -> a / <io | e> |
| 17 | { |
| 18 | handle |
| 19 | { |
| 20 | action () |
| 21 | } |
| 22 | with |
| 23 | { log m -> io.println ("log: " <> m) } |
| 24 | } |
| 25 | |
| 26 | fn traced :: mark: String value: String -> String / <log> |
| 27 | { |
| 28 | log mark |
| 29 | value |
| 30 | } |
| 31 | |
| 32 | // A row with a side effect: the map fusion keeps the rows' effects in |
| 33 | // order (the renders they interleave with are pure). |
| 34 | fn row :: name: String -> Html / <log> |
| 35 | { |
| 36 | log ("row " <> name) |
| 37 | <tr><td>{text name}</td></tr> |
| 38 | } |
| 39 | |
| 40 | fn table :: names: List<String> -> Html / <log> |
| 41 | { |
| 42 | <table><tbody>{list.map names row}</tbody></table> |
| 43 | } |
| 44 | |
| 45 | // A component whose body is a match: each arm renders in place. |
| 46 | fn badge :: fresh: Bool -> Html |
| 47 | { |
| 48 | match fresh |
| 49 | { True -> <span class="badge">new</span> |
| 50 | , False -> <span class="badge used">used</span> |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // A tree in a wrapper: matched on, it escapes the render pipeline. |
| 55 | type Wrapped { Wrapped(Html) } |
| 56 | |
| 57 | fn unwrap :: w: Wrapped -> Html |
| 58 | { |
| 59 | match w { Wrapped(t) -> t } |
| 60 | } |
| 61 | |
| 62 | pub fn main :: -> () / <io> |
| 63 | { |
| 64 | use <- with_log |
| 65 | // A direct literal at the render site. |
| 66 | io.println (html.render (<p>static & {traced "hole" "escaped <here>"}</p>)) |
| 67 | // A component call at the render site (a match body). |
| 68 | io.println (html.render (badge True)) |
| 69 | io.println (html.render (badge False)) |
| 70 | // The same component twice in one literal: each call renders its own. |
| 71 | io.println (html.render (<div>{badge True}{badge False}</div>)) |
| 72 | // The rows of a table, fused: the row effects stay in order. |
| 73 | io.println (html.render (table ["a", "b"])) |
| 74 | // But not across an effect: the map's evaluation may not move past |
| 75 | // the log — the list is built first, as today. |
| 76 | let rows = list.map ["c", "d"] row |
| 77 | log "built" |
| 78 | io.println (html.render (<table>{rows}</table>)) |
| 79 | // A tree that escapes is still built: wrapped, matched on, then |
| 80 | // rendered (the projection is no longer a construction site). |
| 81 | let w = Wrapped(<div class="kept">body</div>) |
| 82 | io.println (html.render (unwrap w)) |
| 83 | // Rendered twice: the same output twice. |
| 84 | let twice = <p>again</p> |
| 85 | io.println (html.render twice) |
| 86 | io.println (html.render twice) |
| 87 | // Every void element, with children (dropped) and no closing tag. |
| 88 | io.println |
| 89 | ( html.render |
| 90 | ( <div> |
| 91 | <area>{text "x"}</area> |
| 92 | <base/> |
| 93 | <br>{text "x"}</br> |
| 94 | <col/> |
| 95 | <embed/> |
| 96 | <hr/> |
| 97 | <img src="a.png"/> |
| 98 | <input type="checkbox" checked/> |
| 99 | <link href="x"/> |
| 100 | <meta charset="utf-8"/> |
| 101 | <source src="x"/> |
| 102 | <track/> |
| 103 | <wbr/> |
| 104 | </div> |
| 105 | ) |
| 106 | ) |
| 107 | // A flag that is off renders nothing; a raw hole is not escaped. |
| 108 | io.println |
| 109 | (html.render (<input type="text" disabled={False} required={True}/>)) |
| 110 | io.println (html.render (<p>{raw "<em>trusted</em>"}</p>)) |
| 111 | // A lambda as the row: the fold step renders its body directly. |
| 112 | io.println |
| 113 | (html.render (<ul>{list.map ["x", "y"] fn n { <li>{text n}</li> }}</ul>)) |
| 114 | } |
| 115 | //> log: hole |
| 116 | //> <p>static & escaped <here></p> |
| 117 | //> <span class="badge">new</span> |
| 118 | //> <span class="badge used">used</span> |
| 119 | //> <div><span class="badge">new</span><span class="badge used">used</span></div> |
| 120 | //> log: row a |
| 121 | //> log: row b |
| 122 | //> <table><tbody><tr><td>a</td></tr><tr><td>b</td></tr></tbody></table> |
| 123 | //> log: row c |
| 124 | //> log: row d |
| 125 | //> log: built |
| 126 | //> <table><tr><td>c</td></tr><tr><td>d</td></tr></table> |
| 127 | //> <div class="kept">body</div> |
| 128 | //> <p>again</p> |
| 129 | //> <p>again</p> |
| 130 | //> <div><area><base><br><col><embed><hr><img src="a.png"><input type="checkbox" checked><link href="x"><meta charset="utf-8"><source src="x"><track><wbr></div> |
| 131 | //> <input type="text" required> |
| 132 | //> <p><em>trusted</em></p> |
| 133 | //> <ul><li>x</li><li>y</li></ul> |