]> Cypherpunks repositories - gostls13.git/commitdiff
xml: allow attributes without value in non-strict mode.
authorVolker Dobler <dr.volker.dobler@gmail.com>
Thu, 16 Jun 2011 16:56:49 +0000 (12:56 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 16 Jun 2011 16:56:49 +0000 (12:56 -0400)
Attributes without value are commen in html and the xml
parser will accept them in non-strict mode and use the
attribute name as value. Thus parsing <p nowrap> as
<p norwar="nowrap">.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/4601053

src/pkg/xml/xml.go
src/pkg/xml/xml_test.go

index 2cebbce75aeaa032b9c17abdba3f945386d1b5df..e5d73dd02008858a54fd0f7c31df04bead4154db 100644 (file)
@@ -659,17 +659,22 @@ func (p *Parser) RawToken() (Token, os.Error) {
                        return nil, p.err
                }
                if b != '=' {
-                       p.err = p.syntaxError("attribute name without = in element")
-                       return nil, p.err
-               }
-               p.space()
-               data := p.attrval()
-               if data == nil {
-                       return nil, p.err
+                       if p.Strict {
+                               p.err = p.syntaxError("attribute name without = in element")
+                               return nil, p.err
+                       } else {
+                               p.ungetc(b)
+                               a.Value = a.Name.Local
+                       }
+               } else {
+                       p.space()
+                       data := p.attrval()
+                       if data == nil {
+                               return nil, p.err
+                       }
+                       a.Value = string(data)
                }
-               a.Value = string(data)
        }
-
        if empty {
                p.needClose = true
                p.toClose = name
index 4e51cd53af1538543babab48c1e86bc065ffcbc7..aba21a2b447c3780dfcce8f1a9772f42ec4eeece 100644 (file)
@@ -445,6 +445,33 @@ func TestUnquotedAttrs(t *testing.T) {
        }
 }
 
+func TestValuelessAttrs(t *testing.T) {
+       tests := [][3]string{
+               {"<p nowrap>", "p", "nowrap"},
+               {"<p nowrap >", "p", "nowrap"},
+               {"<input checked/>", "input", "checked"},
+               {"<input checked />", "input", "checked"},
+       }
+       for _, test := range tests {
+               p := NewParser(StringReader(test[0]))
+               p.Strict = false
+               token, err := p.Token()
+               if _, ok := err.(*SyntaxError); ok {
+                       t.Errorf("Unexpected error: %v", err)
+               }
+               if token.(StartElement).Name.Local != test[1] {
+                       t.Errorf("Unexpected tag name: %v", token.(StartElement).Name.Local)
+               }
+               attr := token.(StartElement).Attr[0]
+               if attr.Value != test[2] {
+                       t.Errorf("Unexpected attribute value: %v", attr.Value)
+               }
+               if attr.Name.Local != test[2] {
+                       t.Errorf("Unexpected attribute name: %v", attr.Name.Local)
+               }
+       }
+}
+
 func TestCopyTokenCharData(t *testing.T) {
        data := []byte("same data")
        var tok1 Token = CharData(data)