Skip to content

Commit

Permalink
html: support #script-(on|off) directives for tests
Browse files Browse the repository at this point in the history
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 <namusyaka@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
  • Loading branch information
namusyaka authored and nigeltao committed Nov 24, 2019
1 parent b954d4e commit 8f7fa26
Showing 1 changed file with 44 additions and 17 deletions.
61 changes: 44 additions & 17 deletions html/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -220,18 +247,18 @@ 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
}
if err != nil {
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)
}
}
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 8f7fa26

Please sign in to comment.