-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
element.gleam
126 lines (111 loc) · 3.31 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
// IMPORTS ---------------------------------------------------------------------
import gleam/list
import gleam/string
import gleam/string_builder.{StringBuilder}
import lustre/attribute.{Attribute}
// TYPES -----------------------------------------------------------------------
///
///
pub opaque type Element(msg) {
Text(String)
Element(String, List(Attribute(msg)), List(Element(msg)))
ElementNs(String, List(Attribute(msg)), List(Element(msg)), String)
}
// CONSTRUCTORS ----------------------------------------------------------------
///
///
pub fn element(
tag: String,
attrs: List(Attribute(msg)),
children: List(Element(msg)),
) -> Element(msg) {
Element(tag, attrs, children)
}
///
///
pub fn namespaced(
namespace: String,
tag: String,
attrs: List(Attribute(msg)),
children: List(Element(msg)),
) -> Element(msg) {
ElementNs(tag, attrs, children, namespace)
}
///
///
pub fn text(content: String) -> Element(msg) {
Text(content)
}
fn escape(escaped: String, content: String) -> String {
case string.pop_grapheme(content) {
Ok(#("<", xs)) -> escape(escaped <> "<", xs)
Ok(#(">", xs)) -> escape(escaped <> ">", xs)
Ok(#("&", xs)) -> escape(escaped <> "&", xs)
Ok(#("\"", xs)) -> escape(escaped <> """, xs)
Ok(#("'", xs)) -> escape(escaped <> "'", xs)
Ok(#(x, xs)) -> escape(escaped <> x, xs)
Error(_) -> escaped <> content
}
}
// MANIPULATIONS ---------------------------------------------------------------
///
///
pub fn map(element: Element(a), f: fn(a) -> b) -> Element(b) {
case element {
Text(content) -> Text(content)
Element(tag, attrs, children) ->
Element(
tag,
list.map(attrs, attribute.map(_, f)),
list.map(children, map(_, f)),
)
ElementNs(tag, attrs, children, namespace) ->
ElementNs(
tag,
list.map(attrs, attribute.map(_, f)),
list.map(children, map(_, f)),
namespace,
)
}
}
// CONVERSIONS -----------------------------------------------------------------
///
///
pub fn to_string(element: Element(msg)) -> String {
to_string_builder(element)
|> string_builder.to_string
}
///
///
pub fn to_string_builder(element: Element(msg)) -> StringBuilder {
case element {
Text(content) -> string_builder.from_string(escape("", content))
Element(tag, attrs, children) ->
string_builder.from_string("<" <> tag)
|> attrs_to_string_builder(attrs)
|> string_builder.append(">")
|> children_to_string_builder(children)
|> string_builder.append("</" <> tag <> ">")
ElementNs(tag, attrs, children, namespace) ->
string_builder.from_string("<" <> tag)
|> attrs_to_string_builder(attrs)
|> string_builder.append(" xmlns=\"" <> namespace <> "\"")
|> string_builder.append(">")
|> children_to_string_builder(children)
|> string_builder.append("</" <> tag <> ">")
}
}
fn attrs_to_string_builder(
html: StringBuilder,
attrs: List(Attribute(msg)),
) -> StringBuilder {
use html, attr <- list.fold(attrs, html)
string_builder.append_builder(html, attribute.to_string_builder(attr))
}
fn children_to_string_builder(
html: StringBuilder,
children: List(Element(msg)),
) -> StringBuilder {
use html, child <- list.fold(children, html)
string_builder.append_builder(html, to_string_builder(child))
}