Skip to content

Commit

Permalink
Use html.Parse rather than html.ParseFragment (#16223) (#16225)
Browse files Browse the repository at this point in the history
* 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 <art27@cantab.net>

Co-authored-by: zeripath <art27@cantab.net>
  • Loading branch information
6543 and zeripath committed Jun 22, 2021
1 parent e898590 commit 8ac4858
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,40 +334,37 @@ func (ctx *postProcessCtx) postProcess(rawHTML []byte) ([]byte, error) {
_, _ = res.WriteString("</body></html>")

// 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.
Expand Down

0 comments on commit 8ac4858

Please sign in to comment.