]> Cypherpunks repositories - gostls13.git/commitdiff
html: ignore whitespace before <head> element
authorAndrew Balholm <andybalholm@gmail.com>
Mon, 21 Nov 2011 22:27:27 +0000 (09:27 +1100)
committerNigel Tao <nigeltao@golang.org>
Mon, 21 Nov 2011 22:27:27 +0000 (09:27 +1100)
Pass tests2.dat, test 47:
" \n "
(That is, two spaces separated by a newline)

| <html>
|   <head>
|   <body>

Also pass tests through test 49:
<!DOCTYPE html><script>
</script>  <title>x</title>  </head>

R=nigeltao
CC=golang-dev
https://golang.org/cl/5422043

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

index 9b7e934ac343e27b1ac9e6f55d587a421e0b729d..b74831b34e540c70eabea78989b7b269ccd69c4e 100644 (file)
@@ -319,9 +319,17 @@ func (p *parser) resetInsertionMode() {
        p.im = inBodyIM
 }
 
+const whitespace = " \t\r\n\f"
+
 // Section 11.2.5.4.1.
 func initialIM(p *parser) bool {
        switch p.tok.Type {
+       case TextToken:
+               p.tok.Data = strings.TrimLeft(p.tok.Data, whitespace)
+               if len(p.tok.Data) == 0 {
+                       // It was all whitespace, so ignore it.
+                       return true
+               }
        case CommentToken:
                p.doc.Add(&Node{
                        Type: CommentNode,
@@ -345,6 +353,12 @@ func initialIM(p *parser) bool {
 // Section 11.2.5.4.2.
 func beforeHTMLIM(p *parser) bool {
        switch p.tok.Type {
+       case TextToken:
+               p.tok.Data = strings.TrimLeft(p.tok.Data, whitespace)
+               if len(p.tok.Data) == 0 {
+                       // It was all whitespace, so ignore it.
+                       return true
+               }
        case StartTagToken:
                if p.tok.Data == "html" {
                        p.addElement(p.tok.Data, p.tok.Attr)
@@ -383,7 +397,11 @@ func beforeHeadIM(p *parser) bool {
        case ErrorToken:
                implied = true
        case TextToken:
-               // TODO: distinguish whitespace text from others.
+               p.tok.Data = strings.TrimLeft(p.tok.Data, whitespace)
+               if len(p.tok.Data) == 0 {
+                       // It was all whitespace, so ignore it.
+                       return true
+               }
                implied = true
        case StartTagToken:
                switch p.tok.Data {
@@ -417,8 +435,6 @@ func beforeHeadIM(p *parser) bool {
        return !implied
 }
 
-const whitespace = " \t\r\n\f"
-
 // Section 11.2.5.4.4.
 func inHeadIM(p *parser) bool {
        var (
index 48918947fc9734638a08ece408418e5d913e10d6..808300a289140c6cae15e7aa19e4cff67e4f0400 100644 (file)
@@ -134,7 +134,7 @@ func TestParser(t *testing.T) {
        }{
                // TODO(nigeltao): Process all the test cases from all the .dat files.
                {"tests1.dat", -1},
-               {"tests2.dat", 47},
+               {"tests2.dat", 50},
                {"tests3.dat", 0},
        }
        for _, tf := range testFiles {