Packages
lustre
3.1.0
5.7.1
5.7.0
5.6.0
5.5.2
5.5.1
5.5.0
5.4.0
5.3.5
5.3.4
5.3.3
5.3.2
5.3.1
5.3.0
5.2.1
5.2.0
5.1.1
5.1.0
5.0.3
5.0.2
5.0.1
5.0.0
4.6.4
4.6.3
4.6.2
4.6.1
4.6.0
4.5.1
4.5.0
4.4.4
4.4.3
4.4.1
4.4.0
4.3.6
4.3.5
4.3.4
4.3.3
4.3.2
4.3.1
4.3.0
4.2.6
4.2.5
4.2.4
4.2.3
4.2.2
4.2.1
4.2.0
4.1.8
4.1.7
4.1.6
4.1.5
4.1.4
4.1.3
4.1.2
4.1.1
4.1.0
4.0.0
4.0.0-rc1
4.0.0-rc.2
3.1.4
3.1.3
3.1.2
3.1.1
3.1.0
3.0.12
3.0.11
3.0.10
3.0.9
3.0.8
3.0.7
3.0.6
3.0.5
3.0.4
3.0.3
3.0.2
3.0.1
3.0.0
3.0.0-rc.8
3.0.0-rc.7
3.0.0-rc.6
3.0.0-rc.5
3.0.0-rc.4
3.0.0-rc.3
3.0.0-rc.2
3.0.0-rc.1
2.0.1
2.0.0
1.3.0
1.2.0
1.1.0
1.0.0
Create HTML templates, single page applications, Web Components, and real-time server components in Gleam!
Current section
Files
Jump to
Current section
Files
src/lustre@element.erl
-module(lustre@element).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).
-export([element/3, namespaced/4, text/1, map/2, to_string/1, to_string_builder/1]).
-export_type([element/1]).
-opaque element(FPI) :: {text, binary()} |
{element,
binary(),
list(lustre@attribute:attribute(FPI)),
list(element(FPI))} |
{element_ns,
binary(),
list(lustre@attribute:attribute(FPI)),
list(element(FPI)),
binary()}.
-spec element(
binary(),
list(lustre@attribute:attribute(FPJ)),
list(element(FPJ))
) -> element(FPJ).
element(Tag, Attrs, Children) ->
{element, Tag, Attrs, Children}.
-spec namespaced(
binary(),
binary(),
list(lustre@attribute:attribute(FPP)),
list(element(FPP))
) -> element(FPP).
namespaced(Namespace, Tag, Attrs, Children) ->
{element_ns, Tag, Attrs, Children, Namespace}.
-spec text(binary()) -> element(any()).
text(Content) ->
{text, Content}.
-spec escape(binary(), binary()) -> binary().
escape(Escaped, Content) ->
case Content of
<<"<"/utf8, Rest/binary>> ->
escape(<<Escaped/binary, "<"/utf8>>, Rest);
<<">"/utf8, Rest@1/binary>> ->
escape(<<Escaped/binary, ">"/utf8>>, Rest@1);
<<"&"/utf8, Rest@2/binary>> ->
escape(<<Escaped/binary, "&"/utf8>>, Rest@2);
<<"\""/utf8, Rest@3/binary>> ->
escape(<<Escaped/binary, """/utf8>>, Rest@3);
<<"'"/utf8, Rest@4/binary>> ->
escape(<<Escaped/binary, "'"/utf8>>, Rest@4);
_ ->
case gleam@string:pop_grapheme(Content) of
{ok, {X, Xs}} ->
escape(<<Escaped/binary, X/binary>>, Xs);
{error, _} ->
Escaped
end
end.
-spec map(element(FPX), fun((FPX) -> FPZ)) -> element(FPZ).
map(Element, F) ->
case Element of
{text, Content} ->
{text, Content};
{element, Tag, Attrs, Children} ->
{element,
Tag,
gleam@list:map(
Attrs,
fun(_capture) -> lustre@attribute:map(_capture, F) end
),
gleam@list:map(
Children,
fun(_capture@1) -> map(_capture@1, F) end
)};
{element_ns, Tag@1, Attrs@1, Children@1, Namespace} ->
{element_ns,
Tag@1,
gleam@list:map(
Attrs@1,
fun(_capture@2) -> lustre@attribute:map(_capture@2, F) end
),
gleam@list:map(
Children@1,
fun(_capture@3) -> map(_capture@3, F) end
),
Namespace}
end.
-spec attrs_to_string_builder(list(lustre@attribute:attribute(any()))) -> {gleam@string_builder:string_builder(),
binary()}.
attrs_to_string_builder(Attrs) ->
{Html@1, Class@1, Style@1, Inner_html@1} = begin
Init = {gleam@string_builder:new(),
<<""/utf8>>,
<<""/utf8>>,
<<""/utf8>>},
gleam@list:fold(
Attrs,
Init,
fun(_use0, Attr) ->
{Html, Class, Style, Inner_html} = _use0,
case lustre@attribute:to_string_parts(Attr) of
{ok, {<<"dangerous-unescaped-html"/utf8>>, Val}} ->
{Html, Class, Style, <<Inner_html/binary, Val/binary>>};
{ok, {<<"class"/utf8>>, Val@1}} when Class =:= <<""/utf8>> ->
{Html, Val@1, Style, Inner_html};
{ok, {<<"class"/utf8>>, Val@2}} ->
{Html,
<<<<Class/binary, " "/utf8>>/binary, Val@2/binary>>,
Style,
Inner_html};
{ok, {<<"style"/utf8>>, Val@3}} when Style =:= <<""/utf8>> ->
{Html, Class, Val@3, Inner_html};
{ok, {<<"style"/utf8>>, Val@4}} ->
{Html,
Class,
<<<<Style/binary, " "/utf8>>/binary, Val@4/binary>>,
Inner_html};
{ok, {Key, Val@5}} ->
{gleam@string_builder:append(
Html,
<<<<<<<<" "/utf8, Key/binary>>/binary,
"=\""/utf8>>/binary,
Val@5/binary>>/binary,
"\""/utf8>>
),
Class,
Style,
Inner_html};
{error, _} ->
{Html, Class, Style, Inner_html}
end
end
)
end,
{case {Class@1, Style@1} of
{<<""/utf8>>, <<""/utf8>>} ->
Html@1;
{_, <<""/utf8>>} ->
gleam@string_builder:append(
Html@1,
<<<<" class=\""/utf8, Class@1/binary>>/binary, "\""/utf8>>
);
{<<""/utf8>>, _} ->
gleam@string_builder:append(
Html@1,
<<<<" style=\""/utf8, Style@1/binary>>/binary, "\""/utf8>>
);
{_, _} ->
gleam@string_builder:append(
Html@1,
<<<<<<<<" class=\""/utf8, Class@1/binary>>/binary,
"\" style=\""/utf8>>/binary,
Style@1/binary>>/binary,
"\""/utf8>>
)
end, Inner_html@1}.
-spec children_to_string_builder(
gleam@string_builder:string_builder(),
list(element(any())),
boolean()
) -> gleam@string_builder:string_builder().
children_to_string_builder(Html, Children, Raw_text) ->
gleam@list:fold(
Children,
Html,
fun(Html@1, Child) ->
gleam@string_builder:append_builder(
Html@1,
to_string_builder_helper(Child, Raw_text)
)
end
).
-spec to_string_builder_helper(element(any()), boolean()) -> gleam@string_builder:string_builder().
to_string_builder_helper(Element, Raw_text) ->
case Element of
{text, Content} when Raw_text ->
gleam@string_builder:from_string(Content);
{text, Content@1} ->
gleam@string_builder:from_string(escape(<<""/utf8>>, Content@1));
{element, <<"area"/utf8>> = Tag, Attrs, _} ->
Html = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
{Attrs@1, _} = attrs_to_string_builder(Attrs),
_pipe = Html,
_pipe@1 = gleam@string_builder:append_builder(_pipe, Attrs@1),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"base"/utf8>> = Tag, Attrs, _} ->
Html = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
{Attrs@1, _} = attrs_to_string_builder(Attrs),
_pipe = Html,
_pipe@1 = gleam@string_builder:append_builder(_pipe, Attrs@1),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"br"/utf8>> = Tag, Attrs, _} ->
Html = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
{Attrs@1, _} = attrs_to_string_builder(Attrs),
_pipe = Html,
_pipe@1 = gleam@string_builder:append_builder(_pipe, Attrs@1),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"col"/utf8>> = Tag, Attrs, _} ->
Html = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
{Attrs@1, _} = attrs_to_string_builder(Attrs),
_pipe = Html,
_pipe@1 = gleam@string_builder:append_builder(_pipe, Attrs@1),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"embed"/utf8>> = Tag, Attrs, _} ->
Html = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
{Attrs@1, _} = attrs_to_string_builder(Attrs),
_pipe = Html,
_pipe@1 = gleam@string_builder:append_builder(_pipe, Attrs@1),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"hr"/utf8>> = Tag, Attrs, _} ->
Html = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
{Attrs@1, _} = attrs_to_string_builder(Attrs),
_pipe = Html,
_pipe@1 = gleam@string_builder:append_builder(_pipe, Attrs@1),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"img"/utf8>> = Tag, Attrs, _} ->
Html = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
{Attrs@1, _} = attrs_to_string_builder(Attrs),
_pipe = Html,
_pipe@1 = gleam@string_builder:append_builder(_pipe, Attrs@1),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"input"/utf8>> = Tag, Attrs, _} ->
Html = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
{Attrs@1, _} = attrs_to_string_builder(Attrs),
_pipe = Html,
_pipe@1 = gleam@string_builder:append_builder(_pipe, Attrs@1),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"link"/utf8>> = Tag, Attrs, _} ->
Html = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
{Attrs@1, _} = attrs_to_string_builder(Attrs),
_pipe = Html,
_pipe@1 = gleam@string_builder:append_builder(_pipe, Attrs@1),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"meta"/utf8>> = Tag, Attrs, _} ->
Html = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
{Attrs@1, _} = attrs_to_string_builder(Attrs),
_pipe = Html,
_pipe@1 = gleam@string_builder:append_builder(_pipe, Attrs@1),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"param"/utf8>> = Tag, Attrs, _} ->
Html = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
{Attrs@1, _} = attrs_to_string_builder(Attrs),
_pipe = Html,
_pipe@1 = gleam@string_builder:append_builder(_pipe, Attrs@1),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"source"/utf8>> = Tag, Attrs, _} ->
Html = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
{Attrs@1, _} = attrs_to_string_builder(Attrs),
_pipe = Html,
_pipe@1 = gleam@string_builder:append_builder(_pipe, Attrs@1),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"track"/utf8>> = Tag, Attrs, _} ->
Html = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
{Attrs@1, _} = attrs_to_string_builder(Attrs),
_pipe = Html,
_pipe@1 = gleam@string_builder:append_builder(_pipe, Attrs@1),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"wbr"/utf8>> = Tag, Attrs, _} ->
Html = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
{Attrs@1, _} = attrs_to_string_builder(Attrs),
_pipe = Html,
_pipe@1 = gleam@string_builder:append_builder(_pipe, Attrs@1),
gleam@string_builder:append(_pipe@1, <<">"/utf8>>);
{element, <<"style"/utf8>> = Tag@1, Attrs@2, Children} ->
Html@1 = gleam@string_builder:from_string(
<<"<"/utf8, Tag@1/binary>>
),
{Attrs@3, _} = attrs_to_string_builder(Attrs@2),
_pipe@2 = Html@1,
_pipe@3 = gleam@string_builder:append_builder(_pipe@2, Attrs@3),
_pipe@4 = gleam@string_builder:append(_pipe@3, <<">"/utf8>>),
_pipe@5 = children_to_string_builder(_pipe@4, Children, true),
gleam@string_builder:append(
_pipe@5,
<<<<"</"/utf8, Tag@1/binary>>/binary, ">"/utf8>>
);
{element, <<"script"/utf8>> = Tag@1, Attrs@2, Children} ->
Html@1 = gleam@string_builder:from_string(
<<"<"/utf8, Tag@1/binary>>
),
{Attrs@3, _} = attrs_to_string_builder(Attrs@2),
_pipe@2 = Html@1,
_pipe@3 = gleam@string_builder:append_builder(_pipe@2, Attrs@3),
_pipe@4 = gleam@string_builder:append(_pipe@3, <<">"/utf8>>),
_pipe@5 = children_to_string_builder(_pipe@4, Children, true),
gleam@string_builder:append(
_pipe@5,
<<<<"</"/utf8, Tag@1/binary>>/binary, ">"/utf8>>
);
{element, Tag@2, Attrs@4, Children@1} ->
Html@2 = gleam@string_builder:from_string(
<<"<"/utf8, Tag@2/binary>>
),
{Attrs@5, Inner_html} = attrs_to_string_builder(Attrs@4),
case Inner_html of
<<""/utf8>> ->
_pipe@6 = Html@2,
_pipe@7 = gleam@string_builder:append_builder(
_pipe@6,
Attrs@5
),
_pipe@8 = gleam@string_builder:append(_pipe@7, <<">"/utf8>>),
_pipe@9 = children_to_string_builder(
_pipe@8,
Children@1,
Raw_text
),
gleam@string_builder:append(
_pipe@9,
<<<<"</"/utf8, Tag@2/binary>>/binary, ">"/utf8>>
);
_ ->
_pipe@10 = Html@2,
_pipe@11 = gleam@string_builder:append_builder(
_pipe@10,
Attrs@5
),
gleam@string_builder:append(
_pipe@11,
<<<<<<<<">"/utf8, Inner_html/binary>>/binary,
"</"/utf8>>/binary,
Tag@2/binary>>/binary,
">"/utf8>>
)
end;
{element_ns, Tag@3, Attrs@6, Children@2, Namespace} ->
Html@3 = gleam@string_builder:from_string(
<<"<"/utf8, Tag@3/binary>>
),
{Attrs@7, Inner_html@1} = attrs_to_string_builder(Attrs@6),
case Inner_html@1 of
<<""/utf8>> ->
_pipe@12 = Html@3,
_pipe@13 = gleam@string_builder:append_builder(
_pipe@12,
Attrs@7
),
_pipe@14 = gleam@string_builder:append(
_pipe@13,
<<<<" xmlns=\""/utf8, Namespace/binary>>/binary,
"\""/utf8>>
),
_pipe@15 = gleam@string_builder:append(
_pipe@14,
<<">"/utf8>>
),
_pipe@16 = children_to_string_builder(
_pipe@15,
Children@2,
Raw_text
),
gleam@string_builder:append(
_pipe@16,
<<<<"</"/utf8, Tag@3/binary>>/binary, ">"/utf8>>
);
_ ->
_pipe@17 = Html@3,
_pipe@18 = gleam@string_builder:append_builder(
_pipe@17,
Attrs@7
),
_pipe@19 = gleam@string_builder:append(
_pipe@18,
<<<<" xmlns=\""/utf8, Namespace/binary>>/binary,
"\""/utf8>>
),
gleam@string_builder:append(
_pipe@19,
<<<<<<<<">"/utf8, Inner_html@1/binary>>/binary,
"</"/utf8>>/binary,
Tag@3/binary>>/binary,
">"/utf8>>
)
end
end.
-spec to_string(element(any())) -> binary().
to_string(Element) ->
_pipe = to_string_builder_helper(Element, false),
gleam@string_builder:to_string(_pipe).
-spec to_string_builder(element(any())) -> gleam@string_builder:string_builder().
to_string_builder(Element) ->
to_string_builder_helper(Element, false).