Skip to content

Commit

Permalink
Use multi reader instead to concat strings (#22099)
Browse files Browse the repository at this point in the history
extract from #20326
  • Loading branch information
lunny authored Dec 12, 2022
1 parent 352a50d commit 3e8285b
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,18 +305,15 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
return err
}

res := bytes.NewBuffer(make([]byte, 0, len(rawHTML)+50))
// prepend "<html><body>"
_, _ = res.WriteString("<html><body>")

// Strip out nuls - they're always invalid
_, _ = res.Write(tagCleaner.ReplaceAll([]byte(nulCleaner.Replace(string(rawHTML))), []byte("&lt;$1")))

// close the tags
_, _ = res.WriteString("</body></html>")

// parse the HTML
node, err := html.Parse(res)
node, err := html.Parse(io.MultiReader(
// prepend "<html><body>"
strings.NewReader("<html><body>"),
// Strip out nuls - they're always invalid
bytes.NewReader(tagCleaner.ReplaceAll([]byte(nulCleaner.Replace(string(rawHTML))), []byte("&lt;$1"))),
// close the tags
strings.NewReader("</body></html>"),
))
if err != nil {
return &postProcessError{"invalid HTML", err}
}
Expand Down

0 comments on commit 3e8285b

Please sign in to comment.