Skip to content
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

x/net/html: wrong parse result for html with clauses inside table elements #48363

Closed
jialiangs opened this issue Sep 13, 2021 · 3 comments
Closed

Comments

@jialiangs
Copy link

What version of Go are you using (go version)?

1.15.4

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOARCH="amd64"

What did you do?

func getHTMLBody(doc *html.Node) (*html.Node, error) {
	var b *html.Node
	var f func(*html.Node)
	f = func(n *html.Node) {
		if n.Type == html.ElementNode && n.Data == "body" {
			b = n
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			f(c)
		}
	}
	f(doc)
	if b != nil {
		return b, nil
	}
	return nil, nil
}

func renderHTMLBody(n *html.Node) string {
	var buf bytes.Buffer
	w := io.Writer(&buf)
	html.Render(w, n)
	return buf.String()
}

func Test(t *testing.T) {
	reqContents := "{{#if items}} <table><tbody> <tr><td><strong>header</strong></td></tr> {{#each items}} <tr><td>{{this.name}}</td></tr> {{/each}} </tbody></table> {{/if}}"

	html, _ := html.Parse(strings.NewReader(reqContents))
	body, _ := getHTMLBody(html)
	fmt.Println(renderHTMLBody(body))
}

What did you expect to see?

{{#if items}} <table><tbody> <tr><td><strong>header</strong></td></tr> {{#each items}} <tr><td>{{this.name}}</td></tr> {{/each}} </tbody></table> {{/if}}

What did you see instead?

The sequence of elements has been changed after parsing the HTML content. I tried the java HTML parser jsoup, it works as expected, so I guess there is an issue with the library golang.org/x/net/html.

<body>{{#if items}}  {{#each items}}  {{/each}} <table><tbody> <tr><td><strong>header</strong></td></tr><tr><td>{{this.name}}</td></tr></tbody></table> {{/if}}</body>
@gopherbot gopherbot added this to the Unreleased milestone Sep 13, 2021
@seankhliao
Copy link
Member

Duplicate of #7232

@seankhliao seankhliao marked this as a duplicate of #7232 Sep 15, 2021
@seankhliao
Copy link
Member

Note this is the same result as rendered by Chrome

@jialiangs
Copy link
Author

@seankhliao thanks for your reply! Yes, the rendered results are the same by Chrome. But we need to use this if-each clause to automatically generate the table elements. Now since after parsing, the sequence of the elements has been changed, it can be used to automatically generate table elements anymore.

@golang golang locked and limited conversation to collaborators Sep 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants