Packages

A DSL for generating HTML in Gleam

Current section

Files

Jump to
html_dsl src html_dsl@types@attribute.erl
Raw

src/html_dsl@types@attribute.erl

-module(html_dsl@types@attribute).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new/0, attribute_to_string/1, add/3, class/2, id/2]).
-export_type([attribute/0]).
-opaque attribute() :: {attribute, binary()} | nil.
-spec new() -> gleam@option:option(attribute()).
new() ->
{some, nil}.
-spec attribute_to_string(gleam@option:option(attribute())) -> binary().
attribute_to_string(Attribute) ->
case Attribute of
{some, Attribute@1} ->
case Attribute@1 of
{attribute, Value} ->
Value;
nil ->
<<""/utf8>>
end;
none ->
<<""/utf8>>
end.
-spec add(gleam@option:option(attribute()), binary(), binary()) -> gleam@option:option(attribute()).
add(Root, Key, Value) ->
Key@1 = html_dsl@utils@check:illegal_string_check(Key),
Value@1 = html_dsl@utils@check:illegal_string_check(Value),
case [Key@1, Value@1] of
[<<""/utf8>>, <<""/utf8>>] ->
Root;
[<<""/utf8>>, _] ->
Root;
[_, <<""/utf8>>] ->
Root;
_ ->
{some,
{attribute,
<<<<<<<<<<(attribute_to_string(Root))/binary, " "/utf8>>/binary,
Key@1/binary>>/binary,
"=\""/utf8>>/binary,
Value@1/binary>>/binary,
"\""/utf8>>}}
end.
-spec class(gleam@option:option(attribute()), binary()) -> gleam@option:option(attribute()).
class(Root, Classes) ->
add(Root, <<"class"/utf8>>, Classes).
-spec id(gleam@option:option(attribute()), binary()) -> gleam@option:option(attribute()).
id(Root, Id) ->
add(Root, <<"id"/utf8>>, Id).