-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathself_closing.go
66 lines (49 loc) · 1.65 KB
/
self_closing.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package gophront
import "fmt"
// SelfClosingElement is an HTML element with a self-closing tag
type SelfClosingElement struct {
TagName string
Props map[string]string
Style Style
}
// NewSelfClosingElement creates a new SelfClosingElement
func NewSelfClosingElement(tagName string, props map[string]string, style Style) SelfClosingElement {
return SelfClosingElement{tagName, props, style}
}
func (e SelfClosingElement) Render() (string, error) {
if e.TagName == "" {
return "", fmt.Errorf("tag name is empty")
}
if e.Props == nil {
e.Props = make(map[string]string)
}
if e.Style != (Style{}) {
e.Props["style"] = e.Props["style"] + e.Style.ToString()
}
element := "<" + e.TagName
for key, value := range e.Props {
element += " " + key + "=\"" + value + "\""
}
return element + " />", nil
}
func Br(props map[string]string, style Style) SelfClosingElement {
return NewSelfClosingElement("br", props, style)
}
func Col(props map[string]string, style Style) SelfClosingElement {
return NewSelfClosingElement("col", props, style)
}
func Hr(props map[string]string, style Style) SelfClosingElement {
return NewSelfClosingElement("hr", props, style)
}
func Img(props map[string]string, style Style) SelfClosingElement {
return NewSelfClosingElement("img", props, style)
}
func Input(props map[string]string, style Style) SelfClosingElement {
return NewSelfClosingElement("input", props, style)
}
func Link(props map[string]string, style Style) SelfClosingElement {
return NewSelfClosingElement("link", props, style)
}
func Meta(props map[string]string, style Style) SelfClosingElement {
return NewSelfClosingElement("meta", props, style)
}