From 67ad169bf5fceb49818fefc0c8f277350f37d81e Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 11 Sep 2024 09:24:34 +0800 Subject: [PATCH] Refactor dynamic component parsing in SUI core --- sui/core/build.go | 21 +++++++++++++++++++++ sui/core/parser.go | 24 +++++++++++++----------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/sui/core/build.go b/sui/core/build.go index 8098581274..6119b22514 100644 --- a/sui/core/build.go +++ b/sui/core/build.go @@ -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{} diff --git a/sui/core/parser.go b/sui/core/parser.go index 6904a6d766..aa46f20f98 100644 --- a/sui/core/parser.go +++ b/sui/core/parser.go @@ -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) }