Refactor dynamic component parsing in SUI core

This commit is contained in:
Max 2024-09-11 09:24:34 +08:00
parent fb6518e91f
commit 67ad169bf5
2 changed files with 34 additions and 11 deletions

View file

@ -56,6 +56,9 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
// Parse the imports
page.parseImports(doc)
// Parse the dynamic components
page.parseDynamics(ctx, doc.Selection)
body := doc.Find("body")
body.SetAttr("s:ns", namespace)
body.SetAttr("s:public", option.PublicRoot) // Save public root
@ -195,6 +198,9 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op
// Parse the imports
page.parseImports(doc)
// Parse the dynamic components
page.parseDynamics(ctx, doc.Selection)
// Bind the component events
page.BindEvent(ctx, doc.Selection, component, false)
@ -266,6 +272,21 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op
return source, nil
}
// Parse the dynamic components, which are the is attribute is variable
func (page *Page) parseDynamics(ctx *BuildContext, sel *goquery.Selection) {
if ctx == nil {
ctx = NewBuildContext(nil)
}
sel.Find("dynamic").Each(func(i int, s *goquery.Selection) {
defer s.Remove()
route := s.AttrOr("route", "")
if route == "" {
return
}
ctx.addJitComponent(route)
})
}
func (page *Page) parseImports(doc *goquery.Document) {
imports := doc.Find("import")
mapping := map[string]PageImport{}

View file

@ -60,16 +60,17 @@ type ParserOption struct {
Request *Request `json:"request,omitempty"`
}
var keepWords = map[string]bool{
"s:if": true,
"s:for": true,
"s:for-item": true,
"s:for-index": true,
"s:elif": true,
"s:else": true,
"s:set": true,
"s:bind": true,
}
// var keepWords = map[string]bool{
// "s:if": true,
// "s:for": true,
// "s:for-item": true,
// "s:for-index": true,
// "s:elif": true,
// "s:else": true,
// "s:set": true,
// "set": true,
// "s:bind": true,
// }
var allowUsePropAttrs = map[string]bool{
"s:if": true,
@ -268,7 +269,8 @@ func (parser *TemplateParser) parseElementNode(sel *goquery.Selection) {
parser.ifStatementNode(sel)
}
if _, exist := sel.Attr("s:set"); exist || node.Data == "s:set" {
// keep the node if the editor is enabled
if _, exist := sel.Attr("s:set"); exist || node.Data == "s:set" || node.Data == "set" {
parser.setStatementNode(sel)
}