-
Notifications
You must be signed in to change notification settings - Fork 20
/
tag.go
45 lines (38 loc) 路 809 Bytes
/
tag.go
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
package core
import (
"fmt"
"io"
"sort"
"strings"
)
type Tag struct {
tag string
attributes map[string]string
body Component
}
func NewTag(tag string, attributes map[string]string, body Component) *Tag {
return &Tag{
tag: tag,
attributes: attributes,
body: body,
}
}
func (c *Tag) WriteHTMLTo(w io.Writer) (int64, error) {
names := []string{}
for name := range c.attributes {
names = append(names, name)
}
sort.Strings(names)
attributes := " "
for _, name := range names {
value := c.attributes[name]
if value != "" {
attributes += fmt.Sprintf(`%s="%s" `, name, value)
}
}
return NewComponents(
NewHTML(fmt.Sprintf(`<%s%s>`, c.tag, strings.TrimRight(attributes, " "))),
c.body,
NewHTML(fmt.Sprintf(`</%s>`, c.tag)),
).WriteHTMLTo(w)
}