-
Notifications
You must be signed in to change notification settings - Fork 67
/
element_basic_tag.erl
65 lines (57 loc) · 1.57 KB
/
element_basic_tag.erl
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
% vim: sw=4 ts=4 et ft=erlang
% Nitrogen Web Framework for Erlang
% See MIT-LICENSE for licensing information.
-module(element_basic_tag).
-include("wf.hrl").
-export([
reflect/0,
render_element/1
]).
-spec reflect() -> [atom()].
reflect() -> record_info(fields, basic_tag).
-spec render_element(tuple()) -> body().
render_element(Rec0) ->
ElementName = element(1, Rec0),
Tag = tagname(ElementName),
Rec = setelement(1, Rec0, basic_tag),
case is_record(Rec, basic_tag) of
true -> ok;
false -> throw({unable_to_convert_to_basic_tag, Rec0})
end,
#basic_tag{
text=Text,
body=Body0,
html_encode=HtmlEncode,
html_id=HtmlID,
class=Class,
title=Title,
data_fields=DataFields,
aria=Aria,
role=Role,
style=Style
} = Rec,
Body = [
wf:html_encode(Text, HtmlEncode),
Body0
],
wf_tags:emit_tag(Tag, Body, [
{id, HtmlID},
{class, ?ADD_ELEMENT_CLASS(ElementName, Class)},
{title, Title},
{data_fields, DataFields},
{aria, Aria},
{role, Role},
{style, Style}
]).
%% For the most part, the name of the HTML tag name and the record will be the same.
%% For example, #span{} will become <span>.
%% But for some, we may change the tag name from the element name.
%% The most common example would be #panel{} becomes <div> (since div is a keyword in Erlang)
tagname(panel) ->
'div';
tagname(html5_header) ->
'header';
tagname(html5_footer) ->
'footer';
tagname(Element) ->
Element.