Merge pull request #659 from trheyi/main
[add] sui recursive build detected
This commit is contained in:
commit
5178e4a417
3 changed files with 45 additions and 3 deletions
|
|
@ -25,6 +25,19 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
|
||||||
ctx = NewBuildContext(nil)
|
ctx = NewBuildContext(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Push the current page onto the stack and increment the visit counter
|
||||||
|
ctx.stack = append(ctx.stack, page.Route)
|
||||||
|
ctx.visited[page.Route]++
|
||||||
|
defer func() {
|
||||||
|
ctx.stack = ctx.stack[:len(ctx.stack)-1] // Pop the stack
|
||||||
|
ctx.visited[page.Route]--
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Check for recursive calls
|
||||||
|
if ctx.visited[page.Route] > 1 {
|
||||||
|
return nil, ctx.warnings, fmt.Errorf("recursive build detected for page %s", page.Route)
|
||||||
|
}
|
||||||
|
|
||||||
ctx.sequence++
|
ctx.sequence++
|
||||||
html, err := page.BuildHTML(option)
|
html, err := page.BuildHTML(option)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -68,6 +81,19 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op
|
||||||
return "", fmt.Errorf("The parent page is not set")
|
return "", fmt.Errorf("The parent page is not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Push the current page onto the stack and increment the visit counter
|
||||||
|
ctx.stack = append(ctx.stack, page.Route)
|
||||||
|
ctx.visited[page.Route]++
|
||||||
|
defer func() {
|
||||||
|
ctx.stack = ctx.stack[:len(ctx.stack)-1] // Pop the stack
|
||||||
|
ctx.visited[page.Route]--
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Check for recursive calls
|
||||||
|
if ctx.visited[page.Route] > 1 {
|
||||||
|
return "", fmt.Errorf("recursive build detected for page %s", page.Route)
|
||||||
|
}
|
||||||
|
|
||||||
name, exists := sel.Attr("is")
|
name, exists := sel.Attr("is")
|
||||||
if !exists {
|
if !exists {
|
||||||
return "", fmt.Errorf("The component tag must have an is attribute")
|
return "", fmt.Errorf("The component tag must have an is attribute")
|
||||||
|
|
@ -148,6 +174,7 @@ func (page *Page) copySlots(ctx *BuildContext, from *goquery.Selection, to *goqu
|
||||||
html, err := slot.Html()
|
html, err := slot.Html()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.warnings = append(ctx.warnings, err.Error())
|
ctx.warnings = append(ctx.warnings, err.Error())
|
||||||
|
setError(slotSel, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
slotSel.SetHtml(html)
|
slotSel.SetHtml(html)
|
||||||
|
|
@ -172,6 +199,7 @@ func (page *Page) copyProps(ctx *BuildContext, from *goquery.Selection, to *goqu
|
||||||
val, err := data.Exec(fmt.Sprintf("{{ %s }}", attr.Key[3:]))
|
val, err := data.Exec(fmt.Sprintf("{{ %s }}", attr.Key[3:]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.warnings = append(ctx.warnings, err.Error())
|
ctx.warnings = append(ctx.warnings, err.Error())
|
||||||
|
setError(to, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
switch value := val.(type) {
|
switch value := val.(type) {
|
||||||
|
|
@ -245,21 +273,26 @@ func (page *Page) buildComponents(doc *goquery.Document, ctx *BuildContext, opti
|
||||||
|
|
||||||
ipage, err := tmpl.Page(name)
|
ipage, err := tmpl.Page(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sel.ReplaceWith(fmt.Sprintf("<!-- %s -->", err.Error()))
|
setError(sel, err)
|
||||||
log.Warn("Page %s/%s/%s: %s", page.SuiID, page.TemplateID, page.Route, err.Error())
|
log.Warn("Page %s/%s/%s: %s", page.SuiID, page.TemplateID, page.Route, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ipage.Load()
|
err = ipage.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sel.ReplaceWith(fmt.Sprintf("<!-- %s -->", err.Error()))
|
setError(sel, err)
|
||||||
log.Warn("Page %s/%s/%s: %s", page.SuiID, page.TemplateID, page.Route, err.Error())
|
log.Warn("Page %s/%s/%s: %s", page.SuiID, page.TemplateID, page.Route, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
component := ipage.Get()
|
component := ipage.Get()
|
||||||
component.parent = page
|
component.parent = page
|
||||||
component.BuildAsComponent(sel, ctx, option)
|
_, err = component.BuildAsComponent(sel, ctx, option)
|
||||||
|
if err != nil {
|
||||||
|
setError(sel, err)
|
||||||
|
log.Warn("Page %s/%s/%s: %s", page.SuiID, page.TemplateID, page.Route, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
return
|
return
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -424,6 +457,11 @@ func (page *Page) BuildHTML(option *BuildOption) (string, error) {
|
||||||
return string(res), nil
|
return string(res), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setError(sel *goquery.Selection, err error) {
|
||||||
|
html := `<div style="color:red; margin:10px 0px; font-size: 12px; font-family: monospace; padding: 10px; border: 1px solid red; background-color: #f8d7da;">%s</div>`
|
||||||
|
sel.SetHtml(fmt.Sprintf(html, err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
func addTabToEachLine(input string, prefix ...string) string {
|
func addTabToEachLine(input string, prefix ...string) string {
|
||||||
var lines []string
|
var lines []string
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ func NewBuildContext(global *GlobalBuildContext) *BuildContext {
|
||||||
jitComponents: map[string]bool{},
|
jitComponents: map[string]bool{},
|
||||||
global: global,
|
global: global,
|
||||||
warnings: []string{},
|
warnings: []string{},
|
||||||
|
visited: map[string]int{},
|
||||||
|
stack: []string{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,8 @@ type BuildContext struct {
|
||||||
global *GlobalBuildContext
|
global *GlobalBuildContext
|
||||||
translations []Translation
|
translations []Translation
|
||||||
warnings []string
|
warnings []string
|
||||||
|
visited map[string]int // Keep a counter for each page
|
||||||
|
stack []string // Stack to manage build states
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScriptNode is the struct for the script node
|
// ScriptNode is the struct for the script node
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue