Refactor copySlots method to remove unnecessary context parameter
This commit is contained in:
parent
57b59d6d45
commit
49c66870f8
3 changed files with 18 additions and 15 deletions
|
|
@ -180,7 +180,8 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op
|
||||||
first := body.Children().First()
|
first := body.Children().First()
|
||||||
|
|
||||||
page.copyProps(ctx, sel, first, attrs...)
|
page.copyProps(ctx, sel, first, attrs...)
|
||||||
page.copySlots(ctx, sel, first)
|
page.copySlots(sel, first)
|
||||||
|
page.copyChildren(sel, first)
|
||||||
page.buildComponents(doc, ctx, &opt)
|
page.buildComponents(doc, ctx, &opt)
|
||||||
data := Data{"$props": page.Attrs}
|
data := Data{"$props": page.Attrs}
|
||||||
data.ReplaceSelectionUse(slotRe, first)
|
data.ReplaceSelectionUse(slotRe, first)
|
||||||
|
|
@ -222,7 +223,7 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op
|
||||||
return source, nil
|
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")
|
slots := from.Find("slot")
|
||||||
if slots.Length() == 0 {
|
if slots.Length() == 0 {
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -236,25 +237,27 @@ func (page *Page) copySlots(ctx *BuildContext, from *goquery.Selection, to *goqu
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the slot
|
// Get the slot
|
||||||
slotSel := to.Find(fmt.Sprintf("slot[name='%s']", name))
|
slotSel := to.Find(name)
|
||||||
|
|
||||||
if slotSel.Length() == 0 {
|
if slotSel.Length() == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy the slot
|
slotSel.ReplaceWithSelection(slot.Children())
|
||||||
html, err := slot.Html()
|
|
||||||
if err != nil {
|
|
||||||
ctx.warnings = append(ctx.warnings, err.Error())
|
|
||||||
setError(slotSel, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
slotSel.SetHtml(html)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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 {
|
func (page *Page) copyProps(ctx *BuildContext, from *goquery.Selection, to *goquery.Selection, extra ...html.Attribute) error {
|
||||||
attrs := from.Get(0).Attr
|
attrs := from.Get(0).Attr
|
||||||
prefix := "s:prop"
|
prefix := "s:prop"
|
||||||
|
|
|
||||||
|
|
@ -93,7 +93,7 @@ func (parser *TemplateParser) RenderComponent(comp *JitComponent, props map[stri
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the slot
|
// Find the slot
|
||||||
slotSel := root.Find(fmt.Sprintf("slot[name='%s']", name))
|
slotSel := root.Find(name)
|
||||||
if slotSel.Length() == 0 {
|
if slotSel.Length() == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,9 +57,9 @@ func TestGetTemplates(t *testing.T) {
|
||||||
assert.Equal(t, "default", webTmpls[0].(*Template).ID)
|
assert.Equal(t, "default", webTmpls[0].(*Template).ID)
|
||||||
assert.Equal(t, "Yao Startup Webapp", webTmpls[0].(*Template).Name)
|
assert.Equal(t, "Yao Startup Webapp", webTmpls[0].(*Template).Name)
|
||||||
assert.Len(t, webTmpls[0].Themes(), 2)
|
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.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) {
|
func TestGetTemplate(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue