Packages
lustre
3.1.4
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, nowarn_nomatch]).
-export([element/3, namespaced/4, advanced/6, text/1, none/0, map/2, to_string/1, to_string_builder/1]).
-export_type([element/1]).
-opaque element(HNO) :: {text, binary()} |
{element,
binary(),
binary(),
list(lustre@attribute:attribute(HNO)),
list(element(HNO)),
boolean(),
boolean()}.
-spec element(
binary(),
list(lustre@attribute:attribute(HNP)),
list(element(HNP))
) -> element(HNP).
element(Tag, Attrs, Children) ->
case Tag of
<<"area"/utf8>> ->
{element, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"base"/utf8>> ->
{element, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"br"/utf8>> ->
{element, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"col"/utf8>> ->
{element, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"embed"/utf8>> ->
{element, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"hr"/utf8>> ->
{element, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"img"/utf8>> ->
{element, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"input"/utf8>> ->
{element, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"link"/utf8>> ->
{element, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"meta"/utf8>> ->
{element, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"param"/utf8>> ->
{element, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"source"/utf8>> ->
{element, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"track"/utf8>> ->
{element, <<""/utf8>>, Tag, Attrs, [], false, true};
<<"wbr"/utf8>> ->
{element, <<""/utf8>>, Tag, Attrs, [], false, true};
_ ->
{element, <<""/utf8>>, Tag, Attrs, Children, false, false}
end.
-spec namespaced(
binary(),
binary(),
list(lustre@attribute:attribute(HNV)),
list(element(HNV))
) -> element(HNV).
namespaced(Namespace, Tag, Attrs, Children) ->
{element, Namespace, Tag, Attrs, Children, false, false}.
-spec advanced(
binary(),
binary(),
list(lustre@attribute:attribute(HOB)),
list(element(HOB)),
boolean(),
boolean()
) -> element(HOB).
advanced(Namespace, Tag, Attrs, Children, Self_closing, Void) ->
{element, Tag, Namespace, Attrs, Children, Self_closing, Void}.
-spec text(binary()) -> element(any()).
text(Content) ->
{text, Content}.
-spec none() -> element(any()).
none() ->
{text, <<""/utf8>>}.
-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(HOL), fun((HOL) -> HON)) -> element(HON).
map(Element, F) ->
case Element of
{text, Content} ->
{text, Content};
{element, Namespace, Tag, Attrs, Children, Self_closing, Void} ->
{element,
Namespace,
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
),
Self_closing,
Void}
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, <<""/utf8>>} ->
gleam@string_builder:new();
{text, Content} when Raw_text ->
gleam@string_builder:from_string(Content);
{text, Content@1} ->
gleam@string_builder:from_string(escape(<<""/utf8>>, Content@1));
{element, Namespace, Tag, Attrs, _, Self_closing, _} when Self_closing ->
Html = gleam@string_builder:from_string(<<"<"/utf8, Tag/binary>>),
{Attrs@1, _} = attrs_to_string_builder(case Namespace of
<<""/utf8>> ->
Attrs;
_ ->
[lustre@attribute:attribute(<<"xmlns"/utf8>>, Namespace) |
Attrs]
end),
_pipe = Html,
_pipe@1 = gleam@string_builder:append_builder(_pipe, Attrs@1),
gleam@string_builder:append(_pipe@1, <<"/>"/utf8>>);
{element, Namespace@1, Tag@1, Attrs@2, _, _, Void} when Void ->
Html@1 = gleam@string_builder:from_string(
<<"<"/utf8, Tag@1/binary>>
),
{Attrs@3, _} = attrs_to_string_builder(case Namespace@1 of
<<""/utf8>> ->
Attrs@2;
_ ->
[lustre@attribute:attribute(
<<"xmlns"/utf8>>,
Namespace@1
) |
Attrs@2]
end),
_pipe@2 = Html@1,
_pipe@3 = gleam@string_builder:append_builder(_pipe@2, Attrs@3),
gleam@string_builder:append(_pipe@3, <<">"/utf8>>);
{element,
<<""/utf8>>,
<<"style"/utf8>> = Tag@2,
Attrs@4,
Children,
false,
false} ->
Html@2 = gleam@string_builder:from_string(
<<"<"/utf8, Tag@2/binary>>
),
{Attrs@5, _} = attrs_to_string_builder(Attrs@4),
_pipe@4 = Html@2,
_pipe@5 = gleam@string_builder:append_builder(_pipe@4, Attrs@5),
_pipe@6 = gleam@string_builder:append(_pipe@5, <<">"/utf8>>),
_pipe@7 = children_to_string_builder(_pipe@6, Children, true),
gleam@string_builder:append(
_pipe@7,
<<<<"</"/utf8, Tag@2/binary>>/binary, ">"/utf8>>
);
{element,
<<""/utf8>>,
<<"script"/utf8>> = Tag@2,
Attrs@4,
Children,
false,
false} ->
Html@2 = gleam@string_builder:from_string(
<<"<"/utf8, Tag@2/binary>>
),
{Attrs@5, _} = attrs_to_string_builder(Attrs@4),
_pipe@4 = Html@2,
_pipe@5 = gleam@string_builder:append_builder(_pipe@4, Attrs@5),
_pipe@6 = gleam@string_builder:append(_pipe@5, <<">"/utf8>>),
_pipe@7 = children_to_string_builder(_pipe@6, Children, true),
gleam@string_builder:append(
_pipe@7,
<<<<"</"/utf8, Tag@2/binary>>/binary, ">"/utf8>>
);
{element, Namespace@2, Tag@3, Attrs@6, Children@1, _, _} ->
Html@3 = gleam@string_builder:from_string(
<<"<"/utf8, Tag@3/binary>>
),
{Attrs@7, Inner_html} = attrs_to_string_builder(case Namespace@2 of
<<""/utf8>> ->
Attrs@6;
_ ->
[lustre@attribute:attribute(
<<"xmlns"/utf8>>,
Namespace@2
) |
Attrs@6]
end),
case Inner_html of
<<""/utf8>> ->
_pipe@8 = Html@3,
_pipe@9 = gleam@string_builder:append_builder(
_pipe@8,
Attrs@7
),
_pipe@10 = gleam@string_builder:append(
_pipe@9,
<<">"/utf8>>
),
_pipe@11 = children_to_string_builder(
_pipe@10,
Children@1,
Raw_text
),
gleam@string_builder:append(
_pipe@11,
<<<<"</"/utf8, Tag@3/binary>>/binary, ">"/utf8>>
);
_ ->
_pipe@12 = Html@3,
_pipe@13 = gleam@string_builder:append_builder(
_pipe@12,
Attrs@7
),
gleam@string_builder:append(
_pipe@13,
<<<<<<<<">"/utf8, Inner_html/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).