From 49c66870f82ead825e21f2b2e5b83dee9ff4c4ff Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 18 Jul 2024 18:45:38 +0800 Subject: [PATCH 1/3] Refactor copySlots method to remove unnecessary context parameter --- sui/core/build.go | 27 +++++++++++++++------------ sui/core/jit.go | 2 +- sui/storages/local/local_test.go | 4 ++-- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/sui/core/build.go b/sui/core/build.go index f1efafaae..2cf25df09 100644 --- a/sui/core/build.go +++ b/sui/core/build.go @@ -180,7 +180,8 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op first := body.Children().First() page.copyProps(ctx, sel, first, attrs...) - page.copySlots(ctx, sel, first) + page.copySlots(sel, first) + page.copyChildren(sel, first) page.buildComponents(doc, ctx, &opt) data := Data{"$props": page.Attrs} data.ReplaceSelectionUse(slotRe, first) @@ -222,7 +223,7 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op return source, nil } -func (page *Page) copySlots(ctx *BuildContext, from *goquery.Selection, to *goquery.Selection) error { +func (page *Page) copySlots(from *goquery.Selection, to *goquery.Selection) error { slots := from.Find("slot") if slots.Length() == 0 { return nil @@ -236,25 +237,27 @@ func (page *Page) copySlots(ctx *BuildContext, from *goquery.Selection, to *goqu } // Get the slot - slotSel := to.Find(fmt.Sprintf("slot[name='%s']", name)) - + slotSel := to.Find(name) if slotSel.Length() == 0 { continue } - // Copy the slot - html, err := slot.Html() - if err != nil { - ctx.warnings = append(ctx.warnings, err.Error()) - setError(slotSel, err) - continue - } - slotSel.SetHtml(html) + slotSel.ReplaceWithSelection(slot.Children()) } return nil } +func (page *Page) copyChildren(from *goquery.Selection, to *goquery.Selection) error { + children := from.Children() + if children.Length() == 0 { + return nil + } + children.Find("slot").Remove() + to.Find("children").ReplaceWithSelection(children) + return nil +} + func (page *Page) copyProps(ctx *BuildContext, from *goquery.Selection, to *goquery.Selection, extra ...html.Attribute) error { attrs := from.Get(0).Attr prefix := "s:prop" diff --git a/sui/core/jit.go b/sui/core/jit.go index add3dd9ff..a91bc3971 100644 --- a/sui/core/jit.go +++ b/sui/core/jit.go @@ -93,7 +93,7 @@ func (parser *TemplateParser) RenderComponent(comp *JitComponent, props map[stri } // Find the slot - slotSel := root.Find(fmt.Sprintf("slot[name='%s']", name)) + slotSel := root.Find(name) if slotSel.Length() == 0 { return } diff --git a/sui/storages/local/local_test.go b/sui/storages/local/local_test.go index 68d901103..fa669f4ba 100644 --- a/sui/storages/local/local_test.go +++ b/sui/storages/local/local_test.go @@ -57,9 +57,9 @@ func TestGetTemplates(t *testing.T) { assert.Equal(t, "default", webTmpls[0].(*Template).ID) assert.Equal(t, "Yao Startup Webapp", webTmpls[0].(*Template).Name) assert.Len(t, webTmpls[0].Themes(), 2) - assert.Len(t, webTmpls[0].Locales(), 4) + assert.Len(t, webTmpls[0].Locales(), 5) assert.Len(t, webTmpls[0].(*Template).Template.Themes, 2) - assert.Len(t, webTmpls[0].(*Template).Template.Locales, 4) + assert.Len(t, webTmpls[0].(*Template).Template.Locales, 5) } func TestGetTemplate(t *testing.T) { From 58534f532fc8721d9c5037fae30d17302a340012 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 18 Jul 2024 19:13:22 +0800 Subject: [PATCH 2/3] Refactor copySlots method to use slot.Contents() instead of slot.Children() --- sui/core/build.go | 2 +- sui/core/jit.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sui/core/build.go b/sui/core/build.go index 2cf25df09..cf14b90c2 100644 --- a/sui/core/build.go +++ b/sui/core/build.go @@ -242,7 +242,7 @@ func (page *Page) copySlots(from *goquery.Selection, to *goquery.Selection) erro continue } - slotSel.ReplaceWithSelection(slot.Children()) + slotSel.ReplaceWithSelection(slot.Contents()) } return nil diff --git a/sui/core/jit.go b/sui/core/jit.go index a91bc3971..fa9f85e83 100644 --- a/sui/core/jit.go +++ b/sui/core/jit.go @@ -99,7 +99,7 @@ func (parser *TemplateParser) RenderComponent(comp *JitComponent, props map[stri } // Replace the slot - slotSel.ReplaceWithNodes(s.Contents().Nodes...) + slotSel.ReplaceWithSelection(s.Contents()) }) option := *parser.option From 3014d6f2e96a79250f674779ec029fa7eab2af9f Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 18 Jul 2024 19:28:04 +0800 Subject: [PATCH 3/3] Refactor copySlots method to use slot.Contents() instead of slot.Children() --- sui/core/build.go | 2 +- sui/core/jit.go | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/sui/core/build.go b/sui/core/build.go index cf14b90c2..bccfc5dfb 100644 --- a/sui/core/build.go +++ b/sui/core/build.go @@ -249,7 +249,7 @@ func (page *Page) copySlots(from *goquery.Selection, to *goquery.Selection) erro } func (page *Page) copyChildren(from *goquery.Selection, to *goquery.Selection) error { - children := from.Children() + children := from.Contents() if children.Length() == 0 { return nil } diff --git a/sui/core/jit.go b/sui/core/jit.go index fa9f85e83..73d8e7b67 100644 --- a/sui/core/jit.go +++ b/sui/core/jit.go @@ -48,14 +48,14 @@ func init() { // parseComponent parse the component func (parser *TemplateParser) parseComponent(sel *goquery.Selection) { parser.parsed(sel) - comp, props, slots, err := parser.getComponent(sel) + comp, props, slots, children, err := parser.getComponent(sel) if err != nil { parser.errors = append(parser.errors, err) setError(sel, err) return } - html, _, err := parser.RenderComponent(comp, props, slots) + html, _, err := parser.RenderComponent(comp, props, slots, children) if err != nil { parser.errors = append(parser.errors, err) setError(sel, err) @@ -66,7 +66,7 @@ func (parser *TemplateParser) parseComponent(sel *goquery.Selection) { } // RenderComponent render the component -func (parser *TemplateParser) RenderComponent(comp *JitComponent, props map[string]interface{}, slots *goquery.Selection) (string, string, error) { +func (parser *TemplateParser) RenderComponent(comp *JitComponent, props map[string]interface{}, slots *goquery.Selection, children *goquery.Selection) (string, string, error) { html := comp.html randvar := fmt.Sprintf("__%s_$props", time.Now().Format("20060102150405")) html = replaceRandVar(html, randvar) @@ -102,6 +102,10 @@ func (parser *TemplateParser) RenderComponent(comp *JitComponent, props map[stri slotSel.ReplaceWithSelection(s.Contents()) }) + // Replace the children + children.Find("slot").Remove() + root.Find("children").ReplaceWithSelection(children) + option := *parser.option option.Route = comp.route compParser := NewTemplateParser(data, &option) @@ -173,26 +177,27 @@ func (parser *TemplateParser) addStyles(sel *goquery.Selection, styles []StyleNo } } -func (parser *TemplateParser) getComponent(sel *goquery.Selection) (*JitComponent, map[string]interface{}, *goquery.Selection, error) { +func (parser *TemplateParser) getComponent(sel *goquery.Selection) (*JitComponent, map[string]interface{}, *goquery.Selection, *goquery.Selection, error) { slots := sel.Find("slot") + children := sel.Contents() props, err := parser.componentProps(sel) if err != nil { - return nil, nil, slots, err + return nil, nil, slots, children, err } file, route, err := parser.componentFile(sel, props) if err != nil { - return nil, props, slots, err + return nil, props, slots, children, err } comp, err := getComponent(route, file, parser.disableCache()) if err != nil { - return nil, props, slots, err + return nil, props, slots, children, err } - return comp, props, slots, nil + return comp, props, slots, children, nil } // isComponent check if the selection is a component