OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [exp/] [html/] [parse_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2010 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package html
6
 
7
import (
8
        "bufio"
9
        "bytes"
10
        "errors"
11
        "fmt"
12
        "io"
13
        "os"
14
        "strings"
15
        "testing"
16
)
17
 
18
// readParseTest reads a single test case from r.
19
func readParseTest(r *bufio.Reader) (text, want, context string, err error) {
20
        line, err := r.ReadSlice('\n')
21
        if err != nil {
22
                return "", "", "", err
23
        }
24
        var b []byte
25
 
26
        // Read the HTML.
27
        if string(line) != "#data\n" {
28
                return "", "", "", fmt.Errorf(`got %q want "#data\n"`, line)
29
        }
30
        for {
31
                line, err = r.ReadSlice('\n')
32
                if err != nil {
33
                        return "", "", "", err
34
                }
35
                if line[0] == '#' {
36
                        break
37
                }
38
                b = append(b, line...)
39
        }
40
        text = strings.TrimRight(string(b), "\n")
41
        b = b[:0]
42
 
43
        // Skip the error list.
44
        if string(line) != "#errors\n" {
45
                return "", "", "", fmt.Errorf(`got %q want "#errors\n"`, line)
46
        }
47
        for {
48
                line, err = r.ReadSlice('\n')
49
                if err != nil {
50
                        return "", "", "", err
51
                }
52
                if line[0] == '#' {
53
                        break
54
                }
55
        }
56
 
57
        if string(line) == "#document-fragment\n" {
58
                line, err = r.ReadSlice('\n')
59
                if err != nil {
60
                        return "", "", "", err
61
                }
62
                context = strings.TrimSpace(string(line))
63
                line, err = r.ReadSlice('\n')
64
                if err != nil {
65
                        return "", "", "", err
66
                }
67
        }
68
 
69
        // Read the dump of what the parse tree should be.
70
        if string(line) != "#document\n" {
71
                return "", "", "", fmt.Errorf(`got %q want "#document\n"`, line)
72
        }
73
        for {
74
                line, err = r.ReadSlice('\n')
75
                if err != nil && err != io.EOF {
76
                        return "", "", "", err
77
                }
78
                if len(line) == 0 || len(line) == 1 && line[0] == '\n' {
79
                        break
80
                }
81
                b = append(b, line...)
82
        }
83
        return text, string(b), context, nil
84
}
85
 
86
func dumpIndent(w io.Writer, level int) {
87
        io.WriteString(w, "| ")
88
        for i := 0; i < level; i++ {
89
                io.WriteString(w, "  ")
90
        }
91
}
92
 
93
func dumpLevel(w io.Writer, n *Node, level int) error {
94
        dumpIndent(w, level)
95
        switch n.Type {
96
        case ErrorNode:
97
                return errors.New("unexpected ErrorNode")
98
        case DocumentNode:
99
                return errors.New("unexpected DocumentNode")
100
        case ElementNode:
101
                if n.Namespace != "" {
102
                        fmt.Fprintf(w, "<%s %s>", n.Namespace, n.Data)
103
                } else {
104
                        fmt.Fprintf(w, "<%s>", n.Data)
105
                }
106
                attr := n.Attr
107
                if len(attr) == 2 && attr[0].Namespace == "xml" && attr[1].Namespace == "xlink" {
108
                        // Some of the test cases in tests10.dat change the order of adjusted
109
                        // foreign attributes, but that behavior is not in the spec, and could
110
                        // simply be an implementation detail of html5lib's python map ordering.
111
                        attr[0], attr[1] = attr[1], attr[0]
112
                }
113
                for _, a := range attr {
114
                        io.WriteString(w, "\n")
115
                        dumpIndent(w, level+1)
116
                        if a.Namespace != "" {
117
                                fmt.Fprintf(w, `%s %s="%s"`, a.Namespace, a.Key, a.Val)
118
                        } else {
119
                                fmt.Fprintf(w, `%s="%s"`, a.Key, a.Val)
120
                        }
121
                }
122
        case TextNode:
123
                fmt.Fprintf(w, `"%s"`, n.Data)
124
        case CommentNode:
125
                fmt.Fprintf(w, "", n.Data)
126
        case DoctypeNode:
127
                fmt.Fprintf(w, "
128
                if n.Attr != nil {
129
                        var p, s string
130
                        for _, a := range n.Attr {
131
                                switch a.Key {
132
                                case "public":
133
                                        p = a.Val
134
                                case "system":
135
                                        s = a.Val
136
                                }
137
                        }
138
                        if p != "" || s != "" {
139
                                fmt.Fprintf(w, ` "%s"`, p)
140
                                fmt.Fprintf(w, ` "%s"`, s)
141
                        }
142
                }
143
                io.WriteString(w, ">")
144
        case scopeMarkerNode:
145
                return errors.New("unexpected scopeMarkerNode")
146
        default:
147
                return errors.New("unknown node type")
148
        }
149
        io.WriteString(w, "\n")
150
        for _, c := range n.Child {
151
                if err := dumpLevel(w, c, level+1); err != nil {
152
                        return err
153
                }
154
        }
155
        return nil
156
}
157
 
158
func dump(n *Node) (string, error) {
159
        if n == nil || len(n.Child) == 0 {
160
                return "", nil
161
        }
162
        var b bytes.Buffer
163
        for _, child := range n.Child {
164
                if err := dumpLevel(&b, child, 0); err != nil {
165
                        return "", err
166
                }
167
        }
168
        return b.String(), nil
169
}
170
 
171
func TestParser(t *testing.T) {
172
        testFiles := []struct {
173
                filename string
174
                // n is the number of test cases to run from that file.
175
                // -1 means all test cases.
176
                n int
177
        }{
178
                // TODO(nigeltao): Process all the test cases from all the .dat files.
179
                {"adoption01.dat", -1},
180
                {"doctype01.dat", -1},
181
                {"tests1.dat", -1},
182
                {"tests2.dat", -1},
183
                {"tests3.dat", -1},
184
                {"tests4.dat", -1},
185
                {"tests5.dat", -1},
186
                {"tests6.dat", -1},
187
                {"tests10.dat", 35},
188
        }
189
        for _, tf := range testFiles {
190
                f, err := os.Open("testdata/webkit/" + tf.filename)
191
                if err != nil {
192
                        t.Fatal(err)
193
                }
194
                defer f.Close()
195
                r := bufio.NewReader(f)
196
                for i := 0; i != tf.n; i++ {
197
                        text, want, context, err := readParseTest(r)
198
                        if err == io.EOF && tf.n == -1 {
199
                                break
200
                        }
201
                        if err != nil {
202
                                t.Fatal(err)
203
                        }
204
 
205
                        var doc *Node
206
                        if context == "" {
207
                                doc, err = Parse(strings.NewReader(text))
208
                                if err != nil {
209
                                        t.Fatal(err)
210
                                }
211
                        } else {
212
                                contextNode := &Node{
213
                                        Type: ElementNode,
214
                                        Data: context,
215
                                }
216
                                nodes, err := ParseFragment(strings.NewReader(text), contextNode)
217
                                if err != nil {
218
                                        t.Fatal(err)
219
                                }
220
                                doc = &Node{
221
                                        Type: DocumentNode,
222
                                }
223
                                for _, n := range nodes {
224
                                        doc.Add(n)
225
                                }
226
                        }
227
 
228
                        got, err := dump(doc)
229
                        if err != nil {
230
                                t.Fatal(err)
231
                        }
232
                        // Compare the parsed tree to the #document section.
233
                        if got != want {
234
                                t.Errorf("%s test #%d %q, got vs want:\n----\n%s----\n%s----", tf.filename, i, text, got, want)
235
                                continue
236
                        }
237
                        if renderTestBlacklist[text] || context != "" {
238
                                continue
239
                        }
240
                        // Check that rendering and re-parsing results in an identical tree.
241
                        pr, pw := io.Pipe()
242
                        go func() {
243
                                pw.CloseWithError(Render(pw, doc))
244
                        }()
245
                        doc1, err := Parse(pr)
246
                        if err != nil {
247
                                t.Fatal(err)
248
                        }
249
                        got1, err := dump(doc1)
250
                        if err != nil {
251
                                t.Fatal(err)
252
                        }
253
                        if got != got1 {
254
                                t.Errorf("%s test #%d %q, got vs got1:\n----\n%s----\n%s----", tf.filename, i, text, got, got1)
255
                                continue
256
                        }
257
                }
258
        }
259
}
260
 
261
// Some test input result in parse trees are not 'well-formed' despite
262
// following the HTML5 recovery algorithms. Rendering and re-parsing such a
263
// tree will not result in an exact clone of that tree. We blacklist such
264
// inputs from the render test.
265
var renderTestBlacklist = map[string]bool{
266
        // The second  will be reparented to the first 's parent. This
267
        // results in an  whose parent is an , which is not 'well-formed'.
268
        `
XCY`: true,
269
        // More cases of  being reparented:
270
        `ababrx
aoe`: true,
271
        `

272
        `
`: true,
273
        // A  element is reparented, putting it before a table.</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>274</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        // A <plaintext> element can't have anything after it in HTML.</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>275</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        `<table><plaintext><td>`: true,</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>276</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>}</code></pre></td>
      </tr>
   </tbody>
</table>


<script type='text/javascript'>
/* <![CDATA[ */
var rev = new Array();
var a = document.getElementsByTagName('a');
for (var i = 0; i < a.length; i++) {
  if (a[i].className == 'blame-revision') {
    var id = a[i].id;
    addEvent(a[i], 'mouseover', function() { mouseover(this) } );
    addEvent(a[i], 'mouseout', function() { mouseout(this) } );
  }
}

function mouseover(a) {
  // Find the revision by using the link
  var m = /rev=(\d+)/.exec(a.href);
  var r = m[1];

  div = document.createElement('div');
  div.className = 'blame-popup';
  div.innerHTML = rev[r];
  a.parentNode.appendChild(div);
}

function mouseout(a) {
  var div = a.parentNode.parentNode.getElementsByTagName('div');
  for (var i = 0; i < div.length; i++) {
    if (div[i].className = 'blame-popup') {
      div[i].parentNode.removeChild(div[i]);
    }
  }
}

function addEvent(obj, type, func) {
  if (obj.addEventListener) {
    obj.addEventListener(type, func, false);
    return true;
  } else if (obj.attachEvent) {
    return obj.attachEvent('on' + type, func);
  } else {
    return false;
  }
}
rev[747] = '<div class="info"><span class="date">2012-03-02 13:20:09 GMT<\/span><\/div><div class="msg">Initial check-in of GCC, with properties matching the upstream.<\/div>';
/* ]]> */
</script>

</div>
</div>
<div id="websvnfooter">
    <p style="padding:0; margin:0"><small>powered by: <a href="http://www.websvn.info">WebSVN 2.1.0</a></small></p>
</div>
        </div>

                
        <div style="clear: both; margin-left: 200px;">
            <ins
                class="adsbygoogle"
                style="display:inline-block;width:728px;height:90px"
                data-ad-client="ca-pub-8561717607970465"
                data-ad-slot="4128044249"></ins>
            <script type="text/javascript">(adsbygoogle = window.adsbygoogle || []).push({});</script>
        </div>
        
            </div>
    <div class="bot">
        &copy; copyright 1999-2024
OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores&reg;, registered trademark.
    </div>
</div>

<!-- Old browser warning -->
<script type="text/javascript">
  if (!('borderImage' in document.createElement('div').style)) {
    var div = document.getElementById('old-browser-warning')
    div.innerHTML = '<b>Your browser is out-of-date!</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Update your browser to view this website correctly.'
    div.setAttribute('style', 'background-color: red; border-bottom: 2px solid black; margin: 0 -12px 12px -12px; padding: 12px; text-align: center;')
  }
</script>
<!-- /Old browser warning -->
<!-- Google search -->
<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script>
<script type="text/javascript" src="//www.google.com/coop/cse/t13n?form=cse-search-box&amp;t13n_langs=en"></script>
<script type="text/javascript" src="//www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en"></script>
<!-- /Google search -->

</body>
</html>