From 8ac48584ecfcafdf0e154e292309972b613ec496 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 22 Jun 2021 03:46:39 +0200 Subject: [PATCH] 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 Co-authored-by: zeripath --- modules/markup/html.go | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index 81e10df452..216a1f20a3 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -334,40 +334,37 @@ func (ctx *postProcessCtx) postProcess(rawHTML []byte) ([]byte, error) { _, _ = res.WriteString("") // parse the HTML - nodes, err := html.ParseFragment(res, nil) + node, err := html.Parse(res) if err != nil { return nil, &postProcessError{"invalid HTML", err} } - for _, node := range nodes { - ctx.visitNode(node, true) + if node.Type == html.DocumentNode { + node = node.FirstChild } - newNodes := make([]*html.Node, 0, len(nodes)) + ctx.visitNode(node, true) - for _, node := range nodes { - if node.Data == "html" { - node = node.FirstChild - for node != nil && node.Data != "body" { - node = node.NextSibling - } - } - if node == nil { - continue + nodes := make([]*html.Node, 0, 5) + + if node.Data == "html" { + node = node.FirstChild + for node != nil && node.Data != "body" { + node = node.NextSibling } + } + if node != nil { if node.Data == "body" { child := node.FirstChild for child != nil { - newNodes = append(newNodes, child) + nodes = append(nodes, child) child = child.NextSibling } } else { - newNodes = append(newNodes, node) + nodes = append(nodes, node) } } - nodes = newNodes - // Create buffer in which the data will be placed again. We know that the // length will be at least that of res; to spare a few alloc+copy, we // reuse res, resetting its length to 0.