golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123k stars 17.53k forks source link

x/net/html: text node is moved outside <table> #37260

Open pierrre opened 4 years ago

pierrre commented 4 years ago

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

Playground, Go 1.13

Does this issue reproduce with the latest release?

Yes

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

Playground

What did you do?

https://play.golang.org/p/UmY2tC6CML5

What did you expect to see?

The output HTML should be identical to the input.

What did you see instead?

The test text node is moved before <table>.

Yes, I know that it's not correct to have a text node directly inside a <table>. However, I'm manipulating an HTML document containing templating instructions from https://github.com/flosch/pongo2 . My goal is to do some changes in the HTML before rendering the template.

Is it possible to not move these text nodes, even if the HTML is not strictly valid ? Is there another workaround ?

toothrot commented 4 years ago

/cc @nigeltao

namusyaka commented 3 years ago

The behavior looks working as expected because our parser tries to parse the given string according to the whatwg parsing/tokenization algorithm strictly. You can also check it by using Google Chrome and you'll see the text node is moved before the table element as well.

I may not understand what the workaround, but does the following example meet your expectation? https://play.golang.org/p/duwjtPFno0A

This will adjust the text node in the parsed tree directly.