]> Cypherpunks repositories - gostls13.git/commitdiff
exp/html: check the context node for consistency when parsing fragments.
authorNigel Tao <nigeltao@golang.org>
Fri, 8 Jun 2012 03:55:15 +0000 (13:55 +1000)
committerNigel Tao <nigeltao@golang.org>
Fri, 8 Jun 2012 03:55:15 +0000 (13:55 +1000)
R=rsc
CC=golang-dev
https://golang.org/cl/6303053

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

index 918a212deac051fb8ccbfd9601723425471fc388..6f09745a10493c86f9e3484a0303ad48ef22cca9 100644 (file)
@@ -5,7 +5,9 @@
 package html
 
 import (
+       "errors"
        a "exp/html/atom"
+       "fmt"
        "io"
        "strings"
 )
@@ -2013,6 +2015,15 @@ func ParseFragment(r io.Reader, context *Node) ([]*Node, error) {
        }
 
        if context != nil {
+               if context.Type != ElementNode {
+                       return nil, errors.New("html: ParseFragment of non-element Node")
+               }
+               // The next check isn't just context.DataAtom.String() == context.Data because
+               // it is valid to pass an element whose tag isn't a known atom. For example,
+               // DataAtom == 0 and Data = "tagfromthefuture" is perfectly consistent.
+               if context.DataAtom != a.Lookup([]byte(context.Data)) {
+                       return nil, fmt.Errorf("html: inconsistent Node: DataAtom=%q, Data=%q", context.DataAtom, context.Data)
+               }
                switch context.DataAtom {
                case a.Iframe, a.Noembed, a.Noframes, a.Noscript, a.Plaintext, a.Script, a.Style, a.Title, a.Textarea, a.Xmp:
                        p.tokenizer.rawTag = context.DataAtom.String()
index 234191ef14cc16eed7b7c17ec067663e4d559877..18389b27d6f726e6e623bce8470c44f4ffe291bf 100644 (file)
@@ -391,6 +391,19 @@ var renderTestBlacklist = map[string]bool{
        `<table><plaintext><td>`: true,
 }
 
+func TestNodeConsistency(t *testing.T) {
+       // inconsistentNode is a Node whose DataAtom and Data do not agree.
+       inconsistentNode := &Node{
+               Type:     ElementNode,
+               DataAtom: atom.Frameset,
+               Data:     "table",
+       }
+       _, err := ParseFragment(strings.NewReader("<p>hello</p>"), inconsistentNode)
+       if err == nil {
+               t.Errorf("got nil error, want non-nil")
+       }
+}
+
 func BenchmarkParser(b *testing.B) {
        buf, err := ioutil.ReadFile("testdata/go1.html")
        if err != nil {