1 // 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.
21 "golang.org/x/net/html/atom"
24 // readParseTest reads a single test case from r.
25 func readParseTest(r *bufio.Reader) (text, want, context string, err error) {
26 line, err := r.ReadSlice('\n')
28 return "", "", "", err
33 if string(line) != "#data\n" {
34 return "", "", "", fmt.Errorf(`got %q want "#data\n"`, line)
37 line, err = r.ReadSlice('\n')
39 return "", "", "", err
44 b = append(b, line...)
46 text = strings.TrimSuffix(string(b), "\n")
49 // Skip the error list.
50 if string(line) != "#errors\n" {
51 return "", "", "", fmt.Errorf(`got %q want "#errors\n"`, line)
54 line, err = r.ReadSlice('\n')
56 return "", "", "", err
63 if string(line) == "#document-fragment\n" {
64 line, err = r.ReadSlice('\n')
66 return "", "", "", err
68 context = strings.TrimSpace(string(line))
69 line, err = r.ReadSlice('\n')
71 return "", "", "", err
75 // Read the dump of what the parse tree should be.
76 if string(line) != "#document\n" {
77 return "", "", "", fmt.Errorf(`got %q want "#document\n"`, line)
81 line, err = r.ReadSlice('\n')
82 if err != nil && err != io.EOF {
83 return "", "", "", err
85 trimmed := bytes.Trim(line, "| \n")
87 if line[0] == '|' && trimmed[0] == '"' {
90 if trimmed[len(trimmed)-1] == '"' && !(line[0] == '|' && len(trimmed) == 1) {
94 if len(line) == 0 || len(line) == 1 && line[0] == '\n' && !inQuote {
97 b = append(b, line...)
99 return text, string(b), context, nil
102 func dumpIndent(w io.Writer, level int) {
103 io.WriteString(w, "| ")
104 for i := 0; i < level; i++ {
105 io.WriteString(w, " ")
109 type sortedAttributes []Attribute
111 func (a sortedAttributes) Len() int {
115 func (a sortedAttributes) Less(i, j int) bool {
116 if a[i].Namespace != a[j].Namespace {
117 return a[i].Namespace < a[j].Namespace
119 return a[i].Key < a[j].Key
122 func (a sortedAttributes) Swap(i, j int) {
123 a[i], a[j] = a[j], a[i]
126 func dumpLevel(w io.Writer, n *Node, level int) error {
130 return errors.New("unexpected ErrorNode")
132 return errors.New("unexpected DocumentNode")
134 if n.Namespace != "" {
135 fmt.Fprintf(w, "<%s %s>", n.Namespace, n.Data)
137 fmt.Fprintf(w, "<%s>", n.Data)
139 attr := sortedAttributes(n.Attr)
141 for _, a := range attr {
142 io.WriteString(w, "\n")
143 dumpIndent(w, level+1)
144 if a.Namespace != "" {
145 fmt.Fprintf(w, `%s %s="%s"`, a.Namespace, a.Key, a.Val)
147 fmt.Fprintf(w, `%s="%s"`, a.Key, a.Val)
151 fmt.Fprintf(w, `"%s"`, n.Data)
153 fmt.Fprintf(w, "<!-- %s -->", n.Data)
155 fmt.Fprintf(w, "<!DOCTYPE %s", n.Data)
158 for _, a := range n.Attr {
166 if p != "" || s != "" {
167 fmt.Fprintf(w, ` "%s"`, p)
168 fmt.Fprintf(w, ` "%s"`, s)
171 io.WriteString(w, ">")
172 case scopeMarkerNode:
173 return errors.New("unexpected scopeMarkerNode")
175 return errors.New("unknown node type")
177 io.WriteString(w, "\n")
178 for c := n.FirstChild; c != nil; c = c.NextSibling {
179 if err := dumpLevel(w, c, level+1); err != nil {
186 func dump(n *Node) (string, error) {
187 if n == nil || n.FirstChild == nil {
191 for c := n.FirstChild; c != nil; c = c.NextSibling {
192 if err := dumpLevel(&b, c, 0); err != nil {
196 return b.String(), nil
199 const testDataDir = "testdata/webkit/"
201 func TestParser(t *testing.T) {
202 testFiles, err := filepath.Glob(testDataDir + "*.dat")
206 for _, tf := range testFiles {
207 f, err := os.Open(tf)
212 r := bufio.NewReader(f)
215 text, want, context, err := readParseTest(r)
223 err = testParseCase(text, want, context)
226 t.Errorf("%s test #%d %q, %s", tf, i, text, err)
232 // testParseCase tests one test case from the test files. If the test does not
233 // pass, it returns an error that explains the failure.
234 // text is the HTML to be parsed, want is a dump of the correct parse tree,
235 // and context is the name of the context node, if any.
236 func testParseCase(text, want, context string) (err error) {
238 if x := recover(); x != nil {
239 switch e := x.(type) {
243 err = fmt.Errorf("%v", e)
250 doc, err = Parse(strings.NewReader(text))
255 contextNode := &Node{
257 DataAtom: atom.Lookup([]byte(context)),
260 nodes, err := ParseFragment(strings.NewReader(text), contextNode)
267 for _, n := range nodes {
272 if err := checkTreeConsistency(doc); err != nil {
276 got, err := dump(doc)
280 // Compare the parsed tree to the #document section.
282 return fmt.Errorf("got vs want:\n----\n%s----\n%s----", got, want)
285 if renderTestBlacklist[text] || context != "" {
289 // Check that rendering and re-parsing results in an identical tree.
292 pw.CloseWithError(Render(pw, doc))
294 doc1, err := Parse(pr)
298 got1, err := dump(doc1)
303 return fmt.Errorf("got vs got1:\n----\n%s----\n%s----", got, got1)
309 // Some test input result in parse trees are not 'well-formed' despite
310 // following the HTML5 recovery algorithms. Rendering and re-parsing such a
311 // tree will not result in an exact clone of that tree. We blacklist such
312 // inputs from the render test.
313 var renderTestBlacklist = map[string]bool{
314 // The second <a> will be reparented to the first <table>'s parent. This
315 // results in an <a> whose parent is an <a>, which is not 'well-formed'.
316 `<a><table><td><a><table></table><a></tr><a></table><b>X</b>C<a>Y`: true,
317 // The same thing with a <p>:
318 `<p><table></p>`: true,
319 // More cases of <a> being reparented:
320 `<a href="blah">aba<table><a href="foo">br<tr><td></td></tr>x</table>aoe`: true,
321 `<a><table><a></table><p><a><div><a>`: true,
322 `<a><table><td><a><table></table><a></tr><a></table><a>`: true,
323 // A similar reparenting situation involving <nobr>:
324 `<!DOCTYPE html><body><b><nobr>1<table><nobr></b><i><nobr>2<nobr></i>3`: true,
325 // A <plaintext> element is reparented, putting it before a table.
326 // A <plaintext> element can't have anything after it in HTML.
327 `<table><plaintext><td>`: true,
328 `<!doctype html><table><plaintext></plaintext>`: true,
329 `<!doctype html><table><tbody><plaintext></plaintext>`: true,
330 `<!doctype html><table><tbody><tr><plaintext></plaintext>`: true,
331 // A form inside a table inside a form doesn't work either.
332 `<!doctype html><form><table></form><form></table></form>`: true,
333 // A script that ends at EOF may escape its own closing tag when rendered.
334 `<!doctype html><script><!--<script `: true,
335 `<!doctype html><script><!--<script <`: true,
336 `<!doctype html><script><!--<script <a`: true,
337 `<!doctype html><script><!--<script </`: true,
338 `<!doctype html><script><!--<script </s`: true,
339 `<!doctype html><script><!--<script </script`: true,
340 `<!doctype html><script><!--<script </scripta`: true,
341 `<!doctype html><script><!--<script -`: true,
342 `<!doctype html><script><!--<script -a`: true,
343 `<!doctype html><script><!--<script -<`: true,
344 `<!doctype html><script><!--<script --`: true,
345 `<!doctype html><script><!--<script --a`: true,
346 `<!doctype html><script><!--<script --<`: true,
347 `<script><!--<script `: true,
348 `<script><!--<script <a`: true,
349 `<script><!--<script </script`: true,
350 `<script><!--<script </scripta`: true,
351 `<script><!--<script -`: true,
352 `<script><!--<script -a`: true,
353 `<script><!--<script --`: true,
354 `<script><!--<script --a`: true,
355 `<script><!--<script <`: true,
356 `<script><!--<script </`: true,
357 `<script><!--<script </s`: true,
358 // Reconstructing the active formatting elements results in a <plaintext>
359 // element that contains an <a> element.
360 `<!doctype html><p><a><plaintext>b`: true,
363 func TestNodeConsistency(t *testing.T) {
364 // inconsistentNode is a Node whose DataAtom and Data do not agree.
365 inconsistentNode := &Node{
367 DataAtom: atom.Frameset,
370 _, err := ParseFragment(strings.NewReader("<p>hello</p>"), inconsistentNode)
372 t.Errorf("got nil error, want non-nil")
376 func BenchmarkParser(b *testing.B) {
377 buf, err := ioutil.ReadFile("testdata/go1.html")
379 b.Fatalf("could not read testdata/go1.html: %v", err)
381 b.SetBytes(int64(len(buf)))
385 for i := 0; i < b.N; i++ {
386 Parse(bytes.NewBuffer(buf))