~repos /gromer

#golang#htmx#ssr

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
Files changed (2) hide show
  1. gsx/gsx.go +17 -2
  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 := strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(tpl, "\n", ""), "\r", ""), "\t", "")
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(name string, f interface{}, args ...string) {
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("todo", Todo, "todo")
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
- <Todo key="todo">
49
+ <Todo key="todo">
49
- <div class="container">
50
+ <div class="container">
50
- <h2>Title</h2>
51
+ <h2>Title</h2>
51
- <h3>Sub title</h3>
52
+ <h3>Sub title</h3>
52
- </div>
53
+ </div>
53
- </Todo>
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 := "<div><div>123<todo key=\"todo\"><li id=\"todo-b1a7359c-ebb4-11ec-8ea0-0242ac120002\" class=\"completed\"><div class=\"view\"><span>My first todo</span></div><div class=\"container\"><h2>Title</h2><h3>Sub title</h3></div><div class=\"count\"><span>true</span></div></li></todo></div><div>Test<button>My Website</button></div></div>"
62
- // actual := Html(ctx).Render(`
63
- // <ul id="todo-list" class="relative">
64
- // <For key="todos" itemKey="todo">
65
- // <Todo key="todo">
66
- // <div>"Todo123"</div>
67
- // </Todo>
68
- // </For>
69
- // <span>{WebsiteName}</span>
70
- // </ul>
71
- // `)
72
- // expected := `<ul id="todo-list" class="relative">
73
- // <For key="todos" itemKey="todo">
74
- // <Todo key="todo">
75
- // <li id="b1a7359c-ebb4-11ec-8ea0-0242ac120002" class="completed">
76
- // <div>
77
- // Todo123
78
- // </div>
79
- // <div class="view">
80
- // <span>
81
- // My first todo
82
- // </span>
83
- // </div>
84
- // </li>
85
- // </Todo>
86
- // </For>
87
- // <span>
88
- // My Website
89
- // </span>
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
  }