fix(core): harden remaining nil-safe rendering paths
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
cae46f9c61
commit
c6fd135239
2 changed files with 23 additions and 1 deletions
13
node.go
13
node.go
|
|
@ -60,6 +60,9 @@ func Raw(content string) Node {
|
|||
}
|
||||
|
||||
func (n *rawNode) Render(_ *Context) string {
|
||||
if n == nil {
|
||||
return ""
|
||||
}
|
||||
return n.content
|
||||
}
|
||||
|
||||
|
|
@ -103,6 +106,10 @@ func Attr(n Node, key, value string) Node {
|
|||
}
|
||||
|
||||
func (n *elNode) Render(ctx *Context) string {
|
||||
if n == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
b := newTextBuilder()
|
||||
|
||||
b.WriteByte('<')
|
||||
|
|
@ -126,6 +133,9 @@ func (n *elNode) Render(ctx *Context) string {
|
|||
}
|
||||
|
||||
for i := range len(n.children) {
|
||||
if n.children[i] == nil {
|
||||
continue
|
||||
}
|
||||
b.WriteString(n.children[i].Render(ctx))
|
||||
}
|
||||
|
||||
|
|
@ -158,6 +168,9 @@ func Text(key string, args ...any) Node {
|
|||
}
|
||||
|
||||
func (n *textNode) Render(ctx *Context) string {
|
||||
if n == nil {
|
||||
return ""
|
||||
}
|
||||
return escapeHTML(translateText(ctx, n.key, n.args...))
|
||||
}
|
||||
|
||||
|
|
|
|||
11
pipeline.go
11
pipeline.go
|
|
@ -51,7 +51,10 @@ func Imprint(node Node, ctx *Context) reversal.GrammarImprint {
|
|||
if ctx == nil {
|
||||
ctx = NewContext()
|
||||
}
|
||||
rendered := node.Render(ctx)
|
||||
rendered := ""
|
||||
if node != nil {
|
||||
rendered = node.Render(ctx)
|
||||
}
|
||||
text := StripTags(rendered)
|
||||
tok := reversal.NewTokeniser()
|
||||
tokens := tok.Tokenise(text)
|
||||
|
|
@ -65,6 +68,9 @@ func CompareVariants(r *Responsive, ctx *Context) map[string]float64 {
|
|||
if ctx == nil {
|
||||
ctx = NewContext()
|
||||
}
|
||||
if r == nil {
|
||||
return make(map[string]float64)
|
||||
}
|
||||
|
||||
type named struct {
|
||||
name string
|
||||
|
|
@ -73,6 +79,9 @@ func CompareVariants(r *Responsive, ctx *Context) map[string]float64 {
|
|||
|
||||
var imprints []named
|
||||
for _, v := range r.variants {
|
||||
if v.layout == nil {
|
||||
continue
|
||||
}
|
||||
imp := Imprint(v.layout, ctx)
|
||||
imprints = append(imprints, named{name: v.name, imp: imp})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue