~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.


3144a80d Peter John

3 years ago
fix parser
Files changed (3) hide show
  1. gsx/context.go +7 -8
  2. gsx/parser.go +12 -8
  3. gsx/parser_test.go +17 -3
gsx/context.go CHANGED
@@ -2,8 +2,6 @@ package gsx
2
2
 
3
3
  import (
4
4
  "context"
5
-
6
- "github.com/jinzhu/copier"
7
5
  )
8
6
 
9
7
  type HX struct {
@@ -74,11 +72,12 @@ func (c *Context) Render(tpl string) []*Tag {
74
72
  }
75
73
 
76
74
  func (c *Context) Clone(name string) *Context {
77
- clone := NewContext(c.Context, c.hx)
75
+ newCtx := &Context{
76
+ data: M{},
77
+ }
78
- err := copier.Copy(clone, c)
78
+ for k, v := range c.data {
79
- if err != nil {
79
+ newCtx.data[k] = v
80
- panic("Failed to copy")
81
80
  }
82
- c.Set("funcName", name)
81
+ newCtx.Set("funcName", name)
83
- return clone
82
+ return newCtx
84
83
  }
gsx/parser.go CHANGED
@@ -6,7 +6,6 @@ import (
6
6
 
7
7
  "github.com/alecthomas/participle/v2"
8
8
  "github.com/alecthomas/participle/v2/lexer"
9
- "github.com/alecthomas/repr"
10
9
  "github.com/goneric/stack"
11
10
  )
12
11
 
@@ -80,13 +79,17 @@ func renderTagString(x *Tag, space string) string {
80
79
  return space + "{" + *x.Text.Ref + "}"
81
80
  }
82
81
  }
83
- s := space + "<" + x.Name
82
+ if x.Name == "fragment" {
84
- if len(x.Attributes) > 0 {
85
- s += " "
83
+ s := ""
84
+ for _, c := range x.Children {
85
+ s += renderTagString(c, space) + "\n"
86
+ }
87
+ return s
86
88
  }
89
+ s := space + "<" + x.Name
87
90
  for _, a := range x.Attributes {
88
- if a.Value.Str != nil {
91
+ if a.Value.Str != nil && *a.Value.Str != "" {
89
- s += a.Key + "=" + *a.Value.Str
92
+ s += " " + a.Key + `="` + *a.Value.Str + `"`
90
93
  }
91
94
  }
92
95
  if x.SelfClosing {
@@ -122,10 +125,11 @@ func processTree(module *Module) []*Tag {
122
125
  }
123
126
  } else {
124
127
  tags = append(tags, newTag)
128
+ if !newTag.SelfClosing {
125
- prevTag = newTag
129
+ prevTag = newTag
130
+ }
126
131
  }
127
132
  } else if n.Close != nil {
128
- repr.Println("close", n.Close.Name, prevTag.Name)
129
133
  if n.Close.Name == prevTag.Name {
130
134
  prevTag, _ = stack.Pop()
131
135
  } else {
gsx/parser_test.go CHANGED
@@ -25,13 +25,13 @@ func TestParse(t *testing.T) {
25
25
  `))
26
26
  println(actual)
27
27
  expected := strings.TrimLeft(`
28
- <ul id="todo-list"class="relative">
28
+ <ul id=""todo-list"" class=""relative"">
29
- <Todo >
29
+ <Todo>
30
30
  <div>
31
31
  Todo123
32
32
  </div>
33
33
  </Todo>
34
- <img src="123" />
34
+ <img src=""123"" />
35
35
  <span>
36
36
  {WebsiteName}
37
37
  </span>
@@ -44,3 +44,17 @@ func TestParse(t *testing.T) {
44
44
  `, "\n")
45
45
  r.Equal(expected, actual)
46
46
  }
47
+
48
+ func TestSelfClose(t *testing.T) {
49
+ r := require.New(t)
50
+ actual := renderString(parse("test", `
51
+ <Todo />
52
+ <TodoCount />
53
+ `))
54
+ println(actual)
55
+ expected := strings.TrimLeft(`
56
+ <Todo />
57
+ <TodoCount />
58
+ `, "\n")
59
+ r.Equal(expected, actual)
60
+ }