Skip to content

A new implementation for body tag on html detection #14402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,48 +317,52 @@ func RenderEmoji(
return ctx.postProcess(rawHTML)
}

var byteBodyTag = []byte("<body>")
var byteBodyTagClosing = []byte("</body>")

func (ctx *postProcessCtx) postProcess(rawHTML []byte) ([]byte, error) {
if ctx.procs == nil {
ctx.procs = defaultProcessors
}

// give a generous extra 50 bytes
res := make([]byte, 0, len(rawHTML)+50)
res = append(res, byteBodyTag...)
res = append(res, rawHTML...)
res = append(res, byteBodyTagClosing...)

// parse the HTML
nodes, err := html.ParseFragment(bytes.NewReader(res), nil)
nodes, err := html.ParseFragment(bytes.NewReader(rawHTML), nil)
if err != nil {
return nil, &postProcessError{"invalid HTML", err}
}

var newNodes = make([]*html.Node, 0, len(nodes))
for _, node := range nodes {
ctx.visitNode(node, true)
if node.Data == "html" {
node = node.FirstChild
for node != nil && node.Data != "body" {
node = node.NextSibling
}
}
if node.Data == "body" {
node = node.FirstChild
for node != nil {
newNodes = append(newNodes, node)
node = node.NextSibling
}
} else {
newNodes = append(newNodes, node)
}
}

// 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.
buf := bytes.NewBuffer(res[:0])
buf := bytes.NewBuffer([]byte{})
Comment on lines 350 to +353
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://golang.org/pkg/bytes/#NewBuffer

NewBuffer creates and initializes a new Buffer using buf as its initial contents. The new Buffer takes ownership of buf, and the caller should not use buf after this call. NewBuffer is intended to prepare a Buffer to read existing data. It can also be used to set the initial size of the internal buffer for writing. To do that, buf should have the desired capacity but a length of zero.

In most cases, new(Buffer) (or just declaring a Buffer variable) is sufficient to initialize a Buffer.

Suggested change
// 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.
buf := bytes.NewBuffer(res[:0])
buf := bytes.NewBuffer([]byte{})
// Create buffer in which the data will be placed again.
buf := new(bytes.Buffer)

// Render everything to buf.
for _, node := range nodes {
for _, node := range newNodes {
ctx.visitNode(node, true)
err = html.Render(buf, node)
if err != nil {
return nil, &postProcessError{"error rendering processed HTML", err}
}
}

// remove initial parts - because Render creates a whole HTML page.
res = buf.Bytes()
res = res[bytes.Index(res, byteBodyTag)+len(byteBodyTag) : bytes.LastIndex(res, byteBodyTagClosing)]

// Everything done successfully, return parsed data.
return res, nil
return buf.Bytes(), nil
}

func (ctx *postProcessCtx) visitNode(node *html.Node, visitText bool) {
Expand Down
28 changes: 28 additions & 0 deletions modules/markup/markup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,31 @@ func TestMisc_IsReadmeFile(t *testing.T) {
assert.False(t, IsReadmeFile(testCase[0], testCase[1]))
}
}

func TestPostProcess_RawHTML(t *testing.T) {
var localMetas = map[string]string{
"user": "go-gitea",
"repo": "gitea",
}
test := func(input, expected string) {
result, err := PostProcess([]byte(input), "https://example.com", localMetas, false)
assert.NoError(t, err)
assert.Equal(t, expected, string(result))
}
var kases = []struct {
Input string
Expected string
}{
{
"<A><maTH><tr><MN><bodY ÿ><temPlate></template><tH><tr></A><tH><d<bodY ",
`<a><math><tr><mn><template></template></mn><th></th><tr></tr></tr></math></a>`,
},
{
"<html><head></head><body><div></div></bodY></html>",
`<div></div>`,
},
}
for _, kase := range kases {
test(kase.Input, kase.Expected)
}
}