Skip to content

Commit

Permalink
Merge pull request #751 from trheyi/main
Browse files Browse the repository at this point in the history
Refactor dynamic component parsing in SUI core
  • Loading branch information
trheyi authored Sep 11, 2024
2 parents 56ef909 + 67ad169 commit b88ff67
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
21 changes: 21 additions & 0 deletions sui/core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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{}
Expand Down
24 changes: 13 additions & 11 deletions sui/core/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit b88ff67

Please sign in to comment.