From f691ade61eff9d7521796ea4c30a0ab201d787ef Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 21 Jun 2021 20:51:12 +0100 Subject: [PATCH 1/2] Use html.Parse rather than html.ParseFragment There have been a few issues with html.ParseFragment - just use html.Parse instead. Signed-off-by: Andrew Thornton --- modules/markup/html.go | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index edf860da4510d..851a99ab523ff 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -304,27 +304,22 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output _, _ = res.WriteString("") // parse the HTML - nodes, err := html.ParseFragment(res, nil) + node, err := html.Parse(res) if err != nil { return &postProcessError{"invalid HTML", err} } - for _, node := range nodes { - visitNode(ctx, procs, node, true) - } + visitNode(ctx, procs, node, true) - newNodes := make([]*html.Node, 0, len(nodes)) + newNodes := make([]*html.Node, 0, 5) - for _, node := range nodes { - if node.Data == "html" { - node = node.FirstChild - for node != nil && node.Data != "body" { - node = node.NextSibling - } - } - if node == nil { - continue + 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 { From e94fa878a78ded380060e04f45036566256fcc08 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 21 Jun 2021 21:25:47 +0100 Subject: [PATCH 2/2] skip document node Signed-off-by: Andrew Thornton --- modules/markup/html.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/markup/html.go b/modules/markup/html.go index 851a99ab523ff..0cc0e23b5c57d 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -309,6 +309,10 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output return &postProcessError{"invalid HTML", err} } + if node.Type == html.DocumentNode { + node = node.FirstChild + } + visitNode(ctx, procs, node, true) newNodes := make([]*html.Node, 0, 5)