~repos /gromer
git clone https://pyrossh.dev/repos/gromer.git
gromer is a framework and cli to build multipage web apps in golang using htmx and alpinejs.
d7b76213
—
Peter John 3 years ago
add name check
- gsx/gsx.go +17 -2
- gsx/gsx_test.go +40 -38
gsx/gsx.go
CHANGED
|
@@ -38,7 +38,7 @@ type (
|
|
|
38
38
|
)
|
|
39
39
|
|
|
40
40
|
func (h Html) Render(tpl string) Node {
|
|
41
|
-
newTpl :=
|
|
41
|
+
newTpl := stripWhitespace(tpl)
|
|
42
42
|
doc, err := html.ParseFragment(bytes.NewBuffer([]byte(newTpl)), contextNode)
|
|
43
43
|
if err != nil {
|
|
44
44
|
panic(err)
|
|
@@ -57,7 +57,21 @@ func (n Node) String() string {
|
|
|
57
57
|
return b.String()
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
func stripWhitespace(s string) string {
|
|
61
|
+
return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, "\n", ""), "\r", ""), "\t", "")
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
func assertName(t, name string) {
|
|
65
|
+
for _, v := range htmlTags {
|
|
66
|
+
if name == v {
|
|
67
|
+
panic(fmt.Sprintf("%s '%s' name cannot be the same as a html tag", t, name))
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
60
|
-
func RegisterComponent(
|
|
72
|
+
func RegisterComponent(f interface{}, args ...string) {
|
|
73
|
+
name := strings.ToLower(getFunctionName(f))
|
|
74
|
+
assertName("component", name)
|
|
61
75
|
compMap[name] = ComponentFunc{
|
|
62
76
|
Func: f,
|
|
63
77
|
Args: args,
|
|
@@ -66,6 +80,7 @@ func RegisterComponent(name string, f interface{}, args ...string) {
|
|
|
66
80
|
|
|
67
81
|
func RegisterFunc(f interface{}) {
|
|
68
82
|
name := getFunctionName(f)
|
|
83
|
+
assertName("function", name)
|
|
69
84
|
funcMap[name] = f
|
|
70
85
|
}
|
|
71
86
|
|
gsx/gsx_test.go
CHANGED
|
@@ -32,7 +32,7 @@ func WebsiteName() string {
|
|
|
32
32
|
|
|
33
33
|
func TestHtml(t *testing.T) {
|
|
34
34
|
r := require.New(t)
|
|
35
|
-
RegisterComponent(
|
|
35
|
+
RegisterComponent(Todo, "todo")
|
|
36
36
|
RegisterFunc(WebsiteName)
|
|
37
37
|
h := Html(map[string]interface{}{
|
|
38
38
|
"todos": []*TodoData{
|
|
@@ -40,17 +40,20 @@ func TestHtml(t *testing.T) {
|
|
|
40
40
|
},
|
|
41
41
|
})
|
|
42
42
|
h["todo"] = &TodoData{ID: "b1a7359c-ebb4-11ec-8ea0-0242ac120002", Text: "My first todo", Completed: true}
|
|
43
|
-
// <template x-for="todo in todos">
|
|
44
43
|
actual := h.Render(`
|
|
45
44
|
<div>
|
|
46
45
|
<div>
|
|
47
46
|
123
|
|
47
|
+
<ul id="todo-list" class="relative">
|
|
48
|
+
<template x-for="todo in todos">
|
|
48
|
-
|
|
49
|
+
<Todo key="todo">
|
|
49
|
-
|
|
50
|
+
<div class="container">
|
|
50
|
-
|
|
51
|
+
<h2>Title</h2>
|
|
51
|
-
|
|
52
|
+
<h3>Sub title</h3>
|
|
52
|
-
|
|
53
|
+
</div>
|
|
53
|
-
|
|
54
|
+
</Todo>
|
|
55
|
+
</template>
|
|
56
|
+
</ul>
|
|
54
57
|
</div>
|
|
55
58
|
<div>
|
|
56
59
|
Test
|
|
@@ -58,35 +61,34 @@ func TestHtml(t *testing.T) {
|
|
|
58
61
|
</div>
|
|
59
62
|
</div>
|
|
60
63
|
`).String()
|
|
61
|
-
expected :=
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
// </ul>`
|
|
64
|
+
expected := stripWhitespace(`
|
|
65
|
+
<div>
|
|
66
|
+
<div>
|
|
67
|
+
123
|
|
68
|
+
<ul id="todo-list" class="relative">
|
|
69
|
+
<template x-for="todo in todos">
|
|
70
|
+
<todo key="todo">
|
|
71
|
+
<li id="todo-b1a7359c-ebb4-11ec-8ea0-0242ac120002" class="completed">
|
|
72
|
+
<div class="view">
|
|
73
|
+
<span>My first todo</span>
|
|
74
|
+
</div>
|
|
75
|
+
<div class="container">
|
|
76
|
+
<h2>Title</h2>
|
|
77
|
+
<h3>Sub title</h3>
|
|
78
|
+
</div>
|
|
79
|
+
<div class="count">
|
|
80
|
+
<span>true</span>
|
|
81
|
+
</div>
|
|
82
|
+
</li>
|
|
83
|
+
</todo>
|
|
84
|
+
</template>
|
|
85
|
+
</ul>
|
|
86
|
+
</div>
|
|
87
|
+
<div>
|
|
88
|
+
Test
|
|
89
|
+
<button>My Website</button>
|
|
90
|
+
</div>
|
|
91
|
+
</div>
|
|
92
|
+
`)
|
|
91
93
|
r.Equal(expected, actual)
|
|
92
94
|
}
|