-module(lustre@element). -compile([no_auto_import, nowarn_unused_vars]). -export([element/3, namespaced/4, text/1, map/2, to_string_builder_helper/2, to_string/1, to_string_builder/1]). -export_type([element/1]). -opaque element(FKP) :: {text, binary()} | {element, binary(), list(lustre@attribute:attribute(FKP)), list(element(FKP))} | {element_ns, binary(), list(lustre@attribute:attribute(FKP)), list(element(FKP)), binary()}. -spec element( binary(), list(lustre@attribute:attribute(FKQ)), list(element(FKQ)) ) -> element(FKQ). element(Tag, Attrs, Children) -> {element, Tag, Attrs, Children}. -spec namespaced( binary(), binary(), list(lustre@attribute:attribute(FKW)), list(element(FKW)) ) -> element(FKW). 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(<>, Rest); <<">"/utf8, Rest@1/binary>> -> escape(<>, Rest@1); <<"&"/utf8, Rest@2/binary>> -> escape(<>, Rest@2); <<"\""/utf8, Rest@3/binary>> -> escape(<>, Rest@3); <<"'"/utf8, Rest@4/binary>> -> escape(<>, Rest@4); _ -> case gleam@string:pop_grapheme(Content) of {ok, {X, Xs}} -> escape(<>, Xs); {error, _} -> Escaped end end. -spec map(element(FLE), fun((FLE) -> FLG)) -> element(FLG). 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( gleam@string_builder:string_builder(), list(lustre@attribute:attribute(any())) ) -> gleam@string_builder:string_builder(). attrs_to_string_builder(Html, Attrs) -> {Html@2, Class@1, Style@1} = (gleam@list:fold( Attrs, {Html, <<""/utf8>>, <<""/utf8>>}, fun(_use0, Attr) -> {Html@1, Class, Style} = _use0, case lustre@attribute:to_string_parts(Attr) of {ok, {<<"class"/utf8>>, Val}} when Class =:= <<""/utf8>> -> {Html@1, Val, Style}; {ok, {<<"class"/utf8>>, Val@1}} -> {Html@1, <<<>/binary, Val@1/binary>>, Style}; {ok, {<<"style"/utf8>>, Val@2}} when Style =:= <<""/utf8>> -> {Html@1, Class, Val@2}; {ok, {<<"style"/utf8>>, Val@3}} -> {Html@1, Class, <<<