
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#1197 · main · 10 min 35 s
1Étoiles
0Forks
0Issues
| 1 | /// Server-rendered HTML. |
| 2 | /// |
| 3 | /// Inline literals such as `<p class="note">{name}</p>` build `Element`s; |
| 4 | /// `render` turns them into HTML text, escaping text and attribute values. |
| 5 | /// See docs/adr/0009-html-literals.md for the typing rules. |
| 6 | |
| 7 | import std/bytes |
| 8 | |
| 9 | /// An HTML tree. `msg` is the type of messages event handlers send on the |
| 10 | /// client target (after v0); server-rendered pages use `Html`. |
| 11 | pub type Element<msg> |
| 12 | { Node(tag: String, attributes: List<Attribute>, children: List<Element<msg>>) |
| 13 | , Text(String) |
| 14 | , Raw(String) |
| 15 | , Fragment(List<Element<msg>>) |
| 16 | } |
| 17 | |
| 18 | /// `name="value"`, a boolean attribute present when `True`, or an |
| 19 | /// optional value (`None` leaves the attribute out). |
| 20 | pub type Attribute |
| 21 | { Attribute(String, String) |
| 22 | , Flag(String, Bool) |
| 23 | , Optional(String, Option<String>) |
| 24 | } |
| 25 | |
| 26 | pub type Html = Element<Never> |
| 27 | |
| 28 | /// Text, escaped when rendered. |
| 29 | pub fn text :: value: String -> Element<msg> |
| 30 | { |
| 31 | Text(value) |
| 32 | } |
| 33 | |
| 34 | /// Markup inserted without escaping. Only for trusted HTML. |
| 35 | pub fn raw :: markup: String -> Element<msg> |
| 36 | { |
| 37 | Raw(markup) |
| 38 | } |
| 39 | |
| 40 | pub fn render :: element: Element<msg> -> String |
| 41 | { |
| 42 | bytes.build_string (render_into element (bytes.builder ())) |
| 43 | } |
| 44 | |
| 45 | // Everything is appended into one builder, which stays unique the whole |
| 46 | // way: appends are in place (amortized), escaping writes straight into |
| 47 | // the buffer, and `build_string` hands the buffer over without copying. |
| 48 | fn render_into :: element b |
| 49 | { |
| 50 | match element |
| 51 | { Text(t) -> append_escaped b t |
| 52 | , Raw(markup) -> bytes.append_string b markup |
| 53 | , Fragment(children) -> render_all children b |
| 54 | , Node(tag, attributes, children) -> |
| 55 | { |
| 56 | let opened = |
| 57 | render_attributes |
| 58 | attributes |
| 59 | (b |> bytes.append_string "<" |> bytes.append_string tag) |
| 60 | |> bytes.append_string ">" |
| 61 | if is_void tag |
| 62 | { |
| 63 | opened |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | render_all children opened |
| 68 | |> bytes.append_string "</" |
| 69 | |> bytes.append_string tag |
| 70 | |> bytes.append_string ">" |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | fn render_all :: children b |
| 77 | { |
| 78 | match children |
| 79 | { [] -> b, [child, ..rest] -> render_all rest (render_into child b) } |
| 80 | } |
| 81 | |
| 82 | fn render_attributes :: attributes b |
| 83 | { |
| 84 | match attributes |
| 85 | { [] -> b |
| 86 | , [Attribute(name, value), ..rest] -> |
| 87 | render_attributes |
| 88 | rest |
| 89 | ( b |
| 90 | |> bytes.append_string " " |
| 91 | |> bytes.append_string name |
| 92 | |> bytes.append_string "=\"" |
| 93 | |> append_escaped value |
| 94 | |> bytes.append_string "\"" |
| 95 | ) |
| 96 | , [ Flag(name, on) |
| 97 | , ..rest |
| 98 | ] -> if on |
| 99 | { |
| 100 | render_attributes |
| 101 | rest |
| 102 | (b |> bytes.append_string " " |> bytes.append_string name) |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | render_attributes rest b |
| 107 | } |
| 108 | , [Optional(name, value), ..rest] -> match value |
| 109 | { Some(v) -> |
| 110 | render_attributes |
| 111 | rest |
| 112 | ( b |
| 113 | |> bytes.append_string " " |
| 114 | |> bytes.append_string name |
| 115 | |> bytes.append_string "=\"" |
| 116 | |> append_escaped v |
| 117 | |> bytes.append_string "\"" |
| 118 | ) |
| 119 | , None -> render_attributes rest b |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /// Elements that never have content or a closing tag. |
| 125 | fn is_void :: tag |
| 126 | { |
| 127 | match tag |
| 128 | { "area" |
| 129 | | "base" |
| 130 | | "br" |
| 131 | | "col" |
| 132 | | "embed" |
| 133 | | "hr" |
| 134 | | "img" |
| 135 | | "input" |
| 136 | | "link" |
| 137 | | "meta" |
| 138 | | "source" |
| 139 | | "track" |
| 140 | | "wbr" -> True |
| 141 | , _ -> False |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /// Escape `& < > " '` for text and attribute values. A string without |
| 146 | /// metacharacters is returned as is. |
| 147 | @extern c "tl_html_escape" owned |
| 148 | pub fn escape :: s: String -> String |
| 149 | |
| 150 | /// `b` with `s` appended, escaped: the escaped form is written straight |
| 151 | /// into the buffer, without an intermediate string. |
| 152 | @extern c "tl_bytes_append_escaped" owned |
| 153 | fn append_escaped :: b: bytes.Builder s: String -> bytes.Builder |