From 8f7fa2680c82de6c160eb8da9de59f8df5016a0c Mon Sep 17 00:00:00 2001 From: Kunpei Sakai Date: Sat, 9 Nov 2019 01:31:01 +0900 Subject: [PATCH] html: support #script-(on|off) directives for tests Those directives are now supported by html5lib-tests. See: https://github.com/html5lib/html5lib-tests/blob/e52ff68cc7113a6ef3687747fa82691079bf9cc5/tree-construction/README.md Also, this fixes missing opts on parsing for identical check Change-Id: I92f2398ebda0477fd7f6bb438c54f3948063c08d Reviewed-on: https://go-review.googlesource.com/c/net/+/206118 Run-TryBot: Kunpei Sakai TryBot-Result: Gobot Gobot Reviewed-by: Nigel Tao --- html/parse_test.go | 61 +++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/html/parse_test.go b/html/parse_test.go index b16d69ac..0d70c9c4 100644 --- a/html/parse_test.go +++ b/html/parse_test.go @@ -21,66 +21,92 @@ import ( "golang.org/x/net/html/atom" ) +type testAttrs struct { + text, want, context string + scripting bool +} + // readParseTest reads a single test case from r. -func readParseTest(r *bufio.Reader) (text, want, context string, err error) { +func readParseTest(r *bufio.Reader) (*testAttrs, error) { + ta := &testAttrs{scripting: true} line, err := r.ReadSlice('\n') if err != nil { - return "", "", "", err + return nil, err } var b []byte // Read the HTML. if string(line) != "#data\n" { - return "", "", "", fmt.Errorf(`got %q want "#data\n"`, line) + return nil, fmt.Errorf(`got %q want "#data\n"`, line) } for { line, err = r.ReadSlice('\n') if err != nil { - return "", "", "", err + return nil, err } if line[0] == '#' { break } b = append(b, line...) } - text = strings.TrimSuffix(string(b), "\n") + ta.text = strings.TrimSuffix(string(b), "\n") b = b[:0] // Skip the error list. if string(line) != "#errors\n" { - return "", "", "", fmt.Errorf(`got %q want "#errors\n"`, line) + return nil, fmt.Errorf(`got %q want "#errors\n"`, line) } for { line, err = r.ReadSlice('\n') if err != nil { - return "", "", "", err + return nil, err } if line[0] == '#' { break } } + if ls := string(line); strings.HasPrefix(ls, "#script-") { + switch { + case strings.HasSuffix(ls, "-on\n"): + ta.scripting = true + case strings.HasSuffix(ls, "-off\n"): + ta.scripting = false + default: + return nil, fmt.Errorf(`got %q, want "#script-on" or "#script-off"`, line) + } + for { + line, err = r.ReadSlice('\n') + if err != nil { + return nil, err + } + if line[0] == '#' { + break + } + } + } + if string(line) == "#document-fragment\n" { line, err = r.ReadSlice('\n') if err != nil { - return "", "", "", err + return nil, err } - context = strings.TrimSpace(string(line)) + ta.context = strings.TrimSpace(string(line)) line, err = r.ReadSlice('\n') if err != nil { - return "", "", "", err + return nil, err } } // Read the dump of what the parse tree should be. if string(line) != "#document\n" { - return "", "", "", fmt.Errorf(`got %q want "#document\n"`, line) + return nil, fmt.Errorf(`got %q want "#document\n"`, line) } inQuote := false for { line, err = r.ReadSlice('\n') if err != nil && err != io.EOF { - return "", "", "", err + return nil, err } trimmed := bytes.Trim(line, "| \n") if len(trimmed) > 0 { @@ -96,7 +122,8 @@ func readParseTest(r *bufio.Reader) (text, want, context string, err error) { } b = append(b, line...) } - return text, string(b), context, nil + ta.want = string(b) + return ta, nil } func dumpIndent(w io.Writer, level int) { @@ -220,7 +247,7 @@ func TestParser(t *testing.T) { r := bufio.NewReader(f) for i := 0; ; i++ { - text, want, context, err := readParseTest(r) + ta, err := readParseTest(r) if err == io.EOF { break } @@ -228,10 +255,10 @@ func TestParser(t *testing.T) { t.Fatal(err) } - err = testParseCase(text, want, context) + err = testParseCase(ta.text, ta.want, ta.context, ParseOptionEnableScripting(ta.scripting)) if err != nil { - t.Errorf("%s test #%d %q, %s", tf, i, text, err) + t.Errorf("%s test #%d %q, %s", tf, i, ta.text, err) } } } @@ -319,7 +346,7 @@ func testParseCase(text, want, context string, opts ...ParseOption) (err error) go func() { pw.CloseWithError(Render(pw, doc)) }() - doc1, err := Parse(pr) + doc1, err := ParseWithOptions(pr, opts...) if err != nil { return err }