]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
markup/goldmark: Fix panic on stray attribute nodes
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 17 Feb 2025 15:06:54 +0000 (16:06 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 17 Feb 2025 16:19:53 +0000 (17:19 +0100)
markup/goldmark/internal/extensions/attributes/attributes.go
markup/goldmark/internal/extensions/attributes/attributes_integration_test.go

index f5f6f97b4debf6ba588185a16e18228f4b01dbe7..50ccb2ed4695e3906c01650596e0216c2b4fab32 100644 (file)
@@ -117,8 +117,9 @@ func (a *transformer) isFragmentNode(n ast.Node) bool {
 
 func (a *transformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
        var attributes []ast.Node
+       var solitaryAttributeNodes []ast.Node
        if a.cfg.Attribute.Block {
-               attributes = make([]ast.Node, 0, 500)
+               attributes = make([]ast.Node, 0, 100)
        }
        ast.Walk(node, func(node ast.Node, entering bool) (ast.WalkStatus, error) {
                if !entering {
@@ -141,8 +142,7 @@ func (a *transformer) Transform(node *ast.Document, reader text.Reader, pc parse
                                attributes = append(attributes, node)
                                return ast.WalkSkipChildren, nil
                        } else {
-                               // remove attributes node
-                               node.Parent().RemoveChild(node.Parent(), node)
+                               solitaryAttributeNodes = append(solitaryAttributeNodes, node)
                        }
                }
 
@@ -161,6 +161,11 @@ func (a *transformer) Transform(node *ast.Document, reader text.Reader, pc parse
                // remove attributes node
                attr.Parent().RemoveChild(attr.Parent(), attr)
        }
+
+       // Remove any solitary attribute nodes.
+       for _, n := range solitaryAttributeNodes {
+               n.Parent().RemoveChild(n.Parent(), n)
+       }
 }
 
 func (a *transformer) generateAutoID(n ast.Node, reader text.Reader, pc parser.Context) {
index 90cf3d08419d91d55cd59bf05e3fa6dfab9c0584..e56c5255029184a01692b9803bfd9c9d43ccd3e7 100644 (file)
@@ -75,3 +75,36 @@ Second line
                "|Identifiers: [a-a-a-a-a-a-c-c-c-c-c-c-c-c-d base-name base-name-1 example-hyperlink-in-a-header foo-something-bar foobar my-title my-title-1 second-line term title-with-id title-with-id]|",
        )
 }
+
+func TestSolitaryAttributesCrash(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+[markup.goldmark.parser.attribute]
+block = true
+-- layouts/_default/single.html --
+Content: {{ .Content }}
+-- content/p1.md --
+---
+title: "Title"
+---
+
+1. a
+
+{.x}
+
+1. b
+
+{.x}
+
+
+
+`
+
+       b := hugolib.Test(t, files)
+
+       b.AssertFileContent("public/p1/index.html",
+               ` <li>a</li>`,
+       )
+}