1
0
mirror of https://github.com/golang/go synced 2024-11-20 09:14:46 -07:00

html: ingore newline at the start of a <pre> block

Pass tests3.dat, test 4:
<!DOCTYPE html><html><head></head><body><pre>\n</pre></body></html>

| <!DOCTYPE html>
| <html>
|   <head>
|   <body>
|     <pre>

Also pass tests through test 11:
<!DOCTYPE html><pre>&#x0a;&#x0a;A</pre>

R=nigeltao
CC=golang-dev
https://golang.org/cl/5437051
This commit is contained in:
Andrew Balholm 2011-11-24 13:15:09 +11:00 committed by Nigel Tao
parent 63e48ccd8e
commit af081cd43e
3 changed files with 28 additions and 1 deletions

View File

@ -628,6 +628,23 @@ func copyAttributes(dst *Node, src Token) {
func inBodyIM(p *parser) bool {
switch p.tok.Type {
case TextToken:
switch n := p.oe.top(); n.Data {
case "pre", "listing", "textarea":
if len(n.Child) == 0 {
// Ignore a newline at the start of a <pre> block.
d := p.tok.Data
if d != "" && d[0] == '\r' {
d = d[1:]
}
if d != "" && d[0] == '\n' {
d = d[1:]
}
if d == "" {
return true
}
p.tok.Data = d
}
}
p.reconstructActiveFormattingElements()
p.addText(p.tok.Data)
p.framesetOK = false

View File

@ -152,7 +152,7 @@ func TestParser(t *testing.T) {
{"doctype01.dat", -1},
{"tests1.dat", -1},
{"tests2.dat", -1},
{"tests3.dat", 0},
{"tests3.dat", 12},
}
for _, tf := range testFiles {
f, err := os.Open("testdata/webkit/" + tf.filename)

View File

@ -173,6 +173,16 @@ func render1(w writer, n *Node) error {
return err
}
// Add initial newline where there is danger of a newline beging ignored.
if len(n.Child) > 0 && n.Child[0].Type == TextNode && strings.HasPrefix(n.Child[0].Data, "\n") {
switch n.Data {
case "pre", "listing", "textarea":
if err := w.WriteByte('\n'); err != nil {
return err
}
}
}
// Render any child nodes.
switch n.Data {
case "noembed", "noframes", "noscript", "plaintext", "script", "style":