-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
element.gleam
297 lines (268 loc) · 7.61 KB
/
element.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//// To read the full documentation for this module, please visit
//// [https://lustre.build/api/lustre/element](https://lustre.build/api/lustre/element)
// IMPORTS ---------------------------------------------------------------------
import gleam/list
import gleam/string
import gleam/string_builder.{type StringBuilder}
import lustre/attribute.{type Attribute, attribute}
// TYPES -----------------------------------------------------------------------
///
pub opaque type Element(msg) {
Text(content: String)
Element(
namespace: String,
tag: String,
attrs: List(Attribute(msg)),
children: List(Element(msg)),
self_closing: Bool,
void: Bool,
)
}
// CONSTRUCTORS ----------------------------------------------------------------
///
///
/// 🚨 Because Lustre is primarily used to create HTML, this function spcieal-cases
/// the following tags to be self-closing:
///
/// - area
/// - base
/// - br
/// - col
/// - embed
/// - hr
/// - img
/// - input
/// - link
/// - meta
/// - param
/// - source
/// - track
/// - wbr
///
/// This will only affect the output of `to_string` and `to_string_builder`!
/// If you need to render any of these tags with children, *or* you want to
/// render some other tag as self-closing or void, use [`advanced`](#advanced)
/// instead.
///
pub fn element(
tag: String,
attrs: List(Attribute(msg)),
children: List(Element(msg)),
) -> Element(msg) {
case tag {
"area"
| "base"
| "br"
| "col"
| "embed"
| "hr"
| "img"
| "input"
| "link"
| "meta"
| "param"
| "source"
| "track"
| "wbr" -> Element("", tag, attrs, [], False, True)
_ ->
Element(
namespace: "",
tag: tag,
attrs: attrs,
children: children,
self_closing: False,
void: False,
)
}
}
///
pub fn namespaced(
namespace: String,
tag: String,
attrs: List(Attribute(msg)),
children: List(Element(msg)),
) -> Element(msg) {
Element(
namespace: namespace,
tag: tag,
attrs: attrs,
children: children,
self_closing: False,
void: False,
)
}
///
pub fn advanced(
namespace: String,
tag: String,
attrs: List(Attribute(msg)),
children: List(Element(msg)),
self_closing: Bool,
void: Bool,
) -> Element(msg) {
Element(tag, namespace, attrs, children, self_closing, void)
}
///
pub fn text(content: String) -> Element(msg) {
Text(content)
}
///
pub fn none() -> Element(msg) {
Text("")
}
fn escape(escaped: String, content: String) -> String {
case content {
"<" <> rest -> escape(escaped <> "<", rest)
">" <> rest -> escape(escaped <> ">", rest)
"&" <> rest -> escape(escaped <> "&", rest)
"\"" <> rest -> escape(escaped <> """, rest)
"'" <> rest -> escape(escaped <> "'", rest)
_ ->
case string.pop_grapheme(content) {
Ok(#(x, xs)) -> escape(escaped <> x, xs)
Error(_) -> escaped
}
}
}
// MANIPULATIONS ---------------------------------------------------------------
///
pub fn map(element: Element(a), f: fn(a) -> b) -> Element(b) {
case element {
Text(content) -> Text(content)
Element(namespace, tag, attrs, children, self_closing, void) ->
Element(
namespace,
tag,
list.map(attrs, attribute.map(_, f)),
list.map(children, map(_, f)),
self_closing,
void,
)
}
}
// CONVERSIONS -----------------------------------------------------------------
///
pub fn to_string(element: Element(msg)) -> String {
to_string_builder_helper(element, False)
|> string_builder.to_string
}
pub fn to_string_builder(element: Element(msg)) -> StringBuilder {
to_string_builder_helper(element, False)
}
fn to_string_builder_helper(
element: Element(msg),
raw_text: Bool,
) -> StringBuilder {
case element {
Text("") -> string_builder.new()
Text(content) if raw_text -> string_builder.from_string(content)
Text(content) -> string_builder.from_string(escape("", content))
Element(namespace, tag, attrs, _, self_closing, _) if self_closing -> {
let html = string_builder.from_string("<" <> tag)
let #(attrs, _) =
attrs_to_string_builder(case namespace {
"" -> attrs
_ -> [attribute("xmlns", namespace), ..attrs]
})
html
|> string_builder.append_builder(attrs)
|> string_builder.append("/>")
}
Element(namespace, tag, attrs, _, _, void) if void -> {
let html = string_builder.from_string("<" <> tag)
let #(attrs, _) =
attrs_to_string_builder(case namespace {
"" -> attrs
_ -> [attribute("xmlns", namespace), ..attrs]
})
html
|> string_builder.append_builder(attrs)
|> string_builder.append(">")
}
// Style and script tags are special beacuse they need to contain unescape
// text content and not escaped HTML content.
Element("", "style" as tag, attrs, children, False, False)
| Element("", "script" as tag, attrs, children, False, False) -> {
let html = string_builder.from_string("<" <> tag)
let #(attrs, _) = attrs_to_string_builder(attrs)
html
|> string_builder.append_builder(attrs)
|> string_builder.append(">")
|> children_to_string_builder(children, True)
|> string_builder.append("</" <> tag <> ">")
}
Element(namespace, tag, attrs, children, _, _) -> {
let html = string_builder.from_string("<" <> tag)
let #(attrs, inner_html) =
attrs_to_string_builder(case namespace {
"" -> attrs
_ -> [attribute("xmlns", namespace), ..attrs]
})
case inner_html {
"" ->
html
|> string_builder.append_builder(attrs)
|> string_builder.append(">")
|> children_to_string_builder(children, raw_text)
|> string_builder.append("</" <> tag <> ">")
_ ->
html
|> string_builder.append_builder(attrs)
|> string_builder.append(">" <> inner_html <> "</" <> tag <> ">")
}
}
}
}
fn attrs_to_string_builder(
attrs: List(Attribute(msg)),
) -> #(StringBuilder, String) {
let #(html, class, style, inner_html) = {
let init = #(string_builder.new(), "", "", "")
use #(html, class, style, inner_html), attr <- list.fold(attrs, init)
case attribute.to_string_parts(attr) {
Ok(#("dangerous-unescaped-html", val)) -> #(
html,
class,
style,
inner_html
<> val,
)
Ok(#("class", val)) if class == "" -> #(html, val, style, inner_html)
Ok(#("class", val)) -> #(html, class <> " " <> val, style, inner_html)
Ok(#("style", val)) if style == "" -> #(html, class, val, inner_html)
Ok(#("style", val)) -> #(html, class, style <> " " <> val, inner_html)
Ok(#(key, val)) -> #(
string_builder.append(html, " " <> key <> "=\"" <> val <> "\""),
class,
style,
inner_html,
)
Error(_) -> #(html, class, style, inner_html)
}
}
#(
case class, style {
"", "" -> html
_, "" -> string_builder.append(html, " class=\"" <> class <> "\"")
"", _ -> string_builder.append(html, " style=\"" <> style <> "\"")
_, _ ->
string_builder.append(
html,
" class=\""
<> class
<> "\" style=\""
<> style
<> "\"",
)
},
inner_html,
)
}
fn children_to_string_builder(
html: StringBuilder,
children: List(Element(msg)),
raw_text: Bool,
) -> StringBuilder {
use html, child <- list.fold(children, html)
string_builder.append_builder(html, to_string_builder_helper(child, raw_text))
}