From 669ccfb1e1343feb9d41eb049d4358607fc14f34 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 10 Jan 2024 13:20:22 +0800 Subject: [PATCH 1/2] Refactor document creation in build.go and parser.go --- sui/core/build.go | 20 +++++++++++++++++--- sui/core/parser.go | 6 ++---- sui/core/utils.go | 17 +++++++++++++---- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/sui/core/build.go b/sui/core/build.go index 4c49d497dd..12857c1a23 100644 --- a/sui/core/build.go +++ b/sui/core/build.go @@ -25,7 +25,7 @@ func (page *Page) Build(option *BuildOption) (*goquery.Document, []string, error } // Add Style & Script & Warning - doc, err := NewDocument([]byte(html)) + doc, err := NewDocumentString(html) if err != nil { warnings = append(warnings, err.Error()) } @@ -81,7 +81,7 @@ func (page *Page) BuildForImport(option *BuildOption, slots map[string]interface } // Add Style & Script & Warning - doc, err := NewDocument([]byte(html)) + doc, err := NewDocumentString(html) if err != nil { warnings = append(warnings, err.Error()) } @@ -121,7 +121,21 @@ func (page *Page) BuildForImport(option *BuildOption, slots map[string]interface } func (page *Page) parse(doc *goquery.Document, option *BuildOption, warnings []string) error { - pages := doc.Find("page") + + pages := doc.Find("*").FilterFunction(func(i int, sel *goquery.Selection) bool { + tagName := sel.Get(0).Data + if tagName == "page" { + return true + } + + if tagName == "slot" { + return false + } + + _, has := sel.Attr("is") + return has + }) + sui := SUIs[page.SuiID] if sui == nil { return fmt.Errorf("SUI %s not found", page.SuiID) diff --git a/sui/core/parser.go b/sui/core/parser.go index ddd9aa45ac..7a7490a975 100644 --- a/sui/core/parser.go +++ b/sui/core/parser.go @@ -1,7 +1,6 @@ package core import ( - "bytes" "fmt" "strings" @@ -58,8 +57,7 @@ func (parser *TemplateParser) Render(html string) (string, error) { html = fmt.Sprintf(`%s`, html) } - reader := bytes.NewReader([]byte(html)) - doc, err := goquery.NewDocumentFromReader(reader) + doc, err := NewDocumentString(html) if err != nil { return "", err } @@ -145,7 +143,7 @@ func (parser *TemplateParser) parseElementNode(sel *goquery.Selection) { parser.forStatementNode(sel) } - if sel.Get(0).Data == "s:set" { + if _, exist := sel.Attr("s:set"); exist || sel.Get(0).Data == "s:set" { parser.setStatementNode(sel) } diff --git a/sui/core/utils.go b/sui/core/utils.go index 67dc28defc..13d2be3972 100644 --- a/sui/core/utils.go +++ b/sui/core/utils.go @@ -5,14 +5,23 @@ import ( "strings" "github.com/PuerkitoBio/goquery" + "golang.org/x/net/html" ) // NewDocument create a new document -func NewDocument(html []byte) (*goquery.Document, error) { - return goquery.NewDocumentFromReader(bytes.NewReader(html)) +func NewDocument(htmlContent []byte) (*goquery.Document, error) { + docNode, err := html.Parse(bytes.NewReader(htmlContent)) + if err != nil { + return nil, err + } + return goquery.NewDocumentFromNode(docNode), nil } // NewDocumentString create a new document -func NewDocumentString(html string) (*goquery.Document, error) { - return goquery.NewDocumentFromReader(strings.NewReader(html)) +func NewDocumentString(htmlContent string) (*goquery.Document, error) { + docNode, err := html.Parse(strings.NewReader(htmlContent)) + if err != nil { + return nil, err + } + return goquery.NewDocumentFromNode(docNode), nil } From cb2f6e874856d2e173111749702a59fdd509490a Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 10 Jan 2024 14:51:16 +0800 Subject: [PATCH 2/2] Refactor attribute handling in build and parser functions --- sui/core/build.go | 21 ++++++++------------- sui/core/parser.go | 14 +++++++++----- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/sui/core/build.go b/sui/core/build.go index 12857c1a23..a72838abb3 100644 --- a/sui/core/build.go +++ b/sui/core/build.go @@ -189,21 +189,16 @@ func (page *Page) parse(doc *goquery.Document, option *BuildOption, warnings []s // Set Attrs attrs := map[string]string{} if sel.Length() > 0 { - if page.Attrs != nil { - parentProps := Data{"$prop": page.Attrs} - for k, v := range page.Attrs { - if k == "is" { - continue - } - attrs[k], _ = parentProps.ReplaceUse(slotRe, v) + for _, attr := range sel.Nodes[0].Attr { + if attr.Key == "is" || attr.Key == "parsed" { + continue } - } else { - for _, attr := range sel.Nodes[0].Attr { - if attr.Key == "is" { - continue - } - attrs[attr.Key] = attr.Val + val := attr.Val + if page.Attrs != nil { + parentProps := Data{"$prop": page.Attrs} + val, _ = parentProps.ReplaceUse(slotRe, val) } + attrs[attr.Key] = val } } diff --git a/sui/core/parser.go b/sui/core/parser.go index 7a7490a975..0dd0c9518d 100644 --- a/sui/core/parser.go +++ b/sui/core/parser.go @@ -159,14 +159,18 @@ func (parser *TemplateParser) setStatementNode(sel *goquery.Selection) { } valueExp := sel.AttrOr("value", "") - val, err := parser.data.Exec(valueExp) - if err != nil { - log.Warn("Set %s: %s", valueExp, err) - parser.data[name] = nil + if stmtRe.MatchString(valueExp) { + val, err := parser.data.Exec(valueExp) + if err != nil { + log.Warn("Set %s: %s", valueExp, err) + parser.data[name] = valueExp + return + } + parser.data[name] = val return } - parser.data[name] = val + parser.data[name] = valueExp } func (parser *TemplateParser) parseElementAttrs(sel *goquery.Selection) {