Refactor component attribute handling to support spread operator
This commit is contained in:
parent
a5150d038e
commit
a3d2baef40
2 changed files with 64 additions and 5 deletions
|
|
@ -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"
|
||||
|
|
@ -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" {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue