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: removing first child node of pre causes doubling of newline at beginning of following text node #64481

Closed
matloob opened this issue Nov 30, 2023 · 3 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@matloob
Copy link
Contributor

matloob commented Nov 30, 2023

Go version

go version go1.21.4 darwin/amd64

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

GO111MODULE='on'
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/matloob/Library/Caches/go-build'
GOENV='/Users/matloob/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/matloob/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/matloob/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.21.4'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/matloob/pkgsite/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/qb/r47yxsb14sx6nz0m6_1pglm8004_sl/T/go-build411072747=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I ran the following program: https://go.dev/play/p/ywoeY3SDD87 pasted below

package main

import (
	"bytes"
	"fmt"
	"log"
	"strings"

	"golang.org/x/net/html"
)

func main() {
	doc, err := html.Parse(strings.NewReader("<pre><img/>\ntext\n</pre>"))
	if err != nil {
		log.Fatal(err)
	}
	pre := doc.FirstChild.LastChild.FirstChild

	fmt.Println("before:")
	var buf bytes.Buffer
	html.Render(&buf, pre)
	fmt.Println(buf.String())

	pre.RemoveChild(pre.FirstChild) // Remove the <img/> tag

	fmt.Println("after:")
	buf.Reset()
	html.Render(&buf, pre)
	fmt.Println(buf.String())
}

What did you expect to see?

before:
<pre><img/>
text
</pre>
after:
<pre>
text
</pre>

What did you see instead?

before:
<pre><img/>
text
</pre>
after:
<pre>

text
</pre>
@gopherbot gopherbot added this to the Unreleased milestone Nov 30, 2023
@dmitshur
Copy link
Contributor

dmitshur commented Dec 1, 2023

CC @neild, @ianlancetaylor.

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 1, 2023
@rsc
Copy link
Contributor

rsc commented Dec 5, 2023

CC @nigeltao if you are interested

@nigeltao
Copy link
Contributor

nigeltao commented Dec 6, 2023

Yeah, in the "after" case, inserting the newline is deliberate (Working As Intended):

https://github.com/golang/net/blob/08a78b1eeae5f15e658ca8972aa74b6857e3b37b/html/render.go#L187-L195

If you delete those lines of code, the tests will fail. For example, this WebKit test case should produce a "\nfoo" text node with exactly one leading '\n', even though the input has two.


Per the WHATWG HTML spec (see: A start tag whose tag name is one of: "pre", "listing"), a '\n' immediately after a <pre> would be ignored. In your example code, to keep the Text Node's text being "\ntext\n" (with a leading '\n'), the renderer inserts an extra one.

@nigeltao nigeltao closed this as completed Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants