Skip to content

Commit 8ac4858

Browse files
6543zeripath
andauthored
Use html.Parse rather than html.ParseFragment (#16223) (#16225)
* Use html.Parse rather than html.ParseFragment There have been a few issues with html.ParseFragment - just use html.Parse instead. * Skip document node Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent e898590 commit 8ac4858

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

modules/markup/html.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -334,40 +334,37 @@ func (ctx *postProcessCtx) postProcess(rawHTML []byte) ([]byte, error) {
334334
_, _ = res.WriteString("</body></html>")
335335

336336
// parse the HTML
337-
nodes, err := html.ParseFragment(res, nil)
337+
node, err := html.Parse(res)
338338
if err != nil {
339339
return nil, &postProcessError{"invalid HTML", err}
340340
}
341341

342-
for _, node := range nodes {
343-
ctx.visitNode(node, true)
342+
if node.Type == html.DocumentNode {
343+
node = node.FirstChild
344344
}
345345

346-
newNodes := make([]*html.Node, 0, len(nodes))
346+
ctx.visitNode(node, true)
347347

348-
for _, node := range nodes {
349-
if node.Data == "html" {
350-
node = node.FirstChild
351-
for node != nil && node.Data != "body" {
352-
node = node.NextSibling
353-
}
354-
}
355-
if node == nil {
356-
continue
348+
nodes := make([]*html.Node, 0, 5)
349+
350+
if node.Data == "html" {
351+
node = node.FirstChild
352+
for node != nil && node.Data != "body" {
353+
node = node.NextSibling
357354
}
355+
}
356+
if node != nil {
358357
if node.Data == "body" {
359358
child := node.FirstChild
360359
for child != nil {
361-
newNodes = append(newNodes, child)
360+
nodes = append(nodes, child)
362361
child = child.NextSibling
363362
}
364363
} else {
365-
newNodes = append(newNodes, node)
364+
nodes = append(nodes, node)
366365
}
367366
}
368367

369-
nodes = newNodes
370-
371368
// Create buffer in which the data will be placed again. We know that the
372369
// length will be at least that of res; to spare a few alloc+copy, we
373370
// reuse res, resetting its length to 0.

0 commit comments

Comments
 (0)