Skip to content

Commit

Permalink
Merge pull request #744 from trheyi/main
Browse files Browse the repository at this point in the history
Refactor component attribute handling to support spread operator
  • Loading branch information
trheyi authored Aug 30, 2024
2 parents 704f8b2 + a3d2bae commit 253dee8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
40 changes: 35 additions & 5 deletions sui/core/jit.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,47 @@ func (parser *TemplateParser) newJitComponentSel(sel *goquery.Selection, comp *J
"s:cn": cn,
"s:ready": cn + "()",
}

doc, err := NewDocumentString(comp.html)
if err != nil {
return nil, fmt.Errorf("Component %s failed to load, please recompile the component. %s", comp.route, err.Error())
}
compSel := doc.Find("body").Children().First()
data := Data{}
for _, attr := range sel.Nodes[0].Attr {
if attr.Key == "is" || attr.Key == "s:jit" {
continue
}

// ...variable
if strings.HasPrefix(attr.Key, "...") {
key := attr.Key[3:]
if parser.data != nil {
if values, ok := parser.data[key].(map[string]any); ok {
for name, value := range values {
switch v := value.(type) {
case string:
props[name] = v
case bool, int, float64:
props[name] = fmt.Sprintf("%v", v)

case nil:
props[name] = ""

default:
str, err := jsoniter.MarshalToString(value)
if err != nil {
continue
}
props[name] = str
props[fmt.Sprintf("json-attr-%s", name)] = "true"
}
}
}
}
continue
}

val, values := parser.data.Replace(attr.Val)
if HasJSON(values) {
props[fmt.Sprintf("json-attr-%s", attr.Key)] = "true"
Expand All @@ -90,11 +125,6 @@ func (parser *TemplateParser) newJitComponentSel(sel *goquery.Selection, comp *J
data[attr.Key] = val
}

doc, err := NewDocumentString(comp.html)
if err != nil {
return nil, fmt.Errorf("Component %s failed to load, please recompile the component. %s", comp.route, err.Error())
}
compSel := doc.Find("body").Children().First()
data.replaceNodeUse(propTokens, compSel.Nodes[0])
for key, val := range props {
if strings.HasPrefix(key, "s:") || key == "parsed" {
Expand Down
29 changes: 29 additions & 0 deletions sui/core/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,35 @@ func (parser *TemplateParser) parseElementAttrs(sel *goquery.Selection, force ..
continue
}

// ...variable
if strings.HasPrefix(attr.Key, "...") {
key := attr.Key[3:]
if parser.data != nil {
if values, ok := parser.data[key].(map[string]any); ok {
for name, value := range values {
switch v := value.(type) {
case string:
sel.SetAttr(name, v)
case bool, int, float64:
sel.SetAttr(name, fmt.Sprintf("%v", v))

case nil:
sel.SetAttr(name, "")

default:
str, err := jsoniter.MarshalToString(value)
if err != nil {
continue
}
sel.SetAttr(name, str)
sel.SetAttr(fmt.Sprintf("json-attr-%s", name), "true")
}
}
}
continue
}
}

parser.sequence = parser.sequence + 1
res, values := parser.data.Replace(attr.Val)
if values != nil && len(values) > 0 {
Expand Down

0 comments on commit 253dee8

Please sign in to comment.