]> Cypherpunks repositories - gostls13.git/commitdiff
html: ingore newline at the start of a <pre> block
authorAndrew Balholm <andybalholm@gmail.com>
Thu, 24 Nov 2011 02:15:09 +0000 (13:15 +1100)
committerNigel Tao <nigeltao@golang.org>
Thu, 24 Nov 2011 02:15:09 +0000 (13:15 +1100)
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

src/pkg/html/parse.go
src/pkg/html/parse_test.go
src/pkg/html/render.go

index 041c5390edaa7b5ccb12beddc81feaf0b443f4d9..36a5fd2fdc6b87b8a3a6f77081cfd62de51021f2 100644 (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
index 90d3f46c61b9935080dd3fe297f6d6d3681baf6c..cb1559169ee4978e734fa06330700264cddea9a8 100644 (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)
index 57d78beef1c9e82accf61e503451a60681f02734..2c868f511d7082a3d43a2d9ea201ab4f24e46c8f 100644 (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":