Skip to content

Commit 8f7fa26

Browse files
namusyakanigeltao
authored andcommitted
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 <namusyaka@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Nigel Tao <nigeltao@golang.org>
1 parent b954d4e commit 8f7fa26

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed

html/parse_test.go

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,66 +21,92 @@ import (
2121
"golang.org/x/net/html/atom"
2222
)
2323

24+
type testAttrs struct {
25+
text, want, context string
26+
scripting bool
27+
}
28+
2429
// readParseTest reads a single test case from r.
25-
func readParseTest(r *bufio.Reader) (text, want, context string, err error) {
30+
func readParseTest(r *bufio.Reader) (*testAttrs, error) {
31+
ta := &testAttrs{scripting: true}
2632
line, err := r.ReadSlice('\n')
2733
if err != nil {
28-
return "", "", "", err
34+
return nil, err
2935
}
3036
var b []byte
3137

3238
// Read the HTML.
3339
if string(line) != "#data\n" {
34-
return "", "", "", fmt.Errorf(`got %q want "#data\n"`, line)
40+
return nil, fmt.Errorf(`got %q want "#data\n"`, line)
3541
}
3642
for {
3743
line, err = r.ReadSlice('\n')
3844
if err != nil {
39-
return "", "", "", err
45+
return nil, err
4046
}
4147
if line[0] == '#' {
4248
break
4349
}
4450
b = append(b, line...)
4551
}
46-
text = strings.TrimSuffix(string(b), "\n")
52+
ta.text = strings.TrimSuffix(string(b), "\n")
4753
b = b[:0]
4854

4955
// Skip the error list.
5056
if string(line) != "#errors\n" {
51-
return "", "", "", fmt.Errorf(`got %q want "#errors\n"`, line)
57+
return nil, fmt.Errorf(`got %q want "#errors\n"`, line)
5258
}
5359
for {
5460
line, err = r.ReadSlice('\n')
5561
if err != nil {
56-
return "", "", "", err
62+
return nil, err
5763
}
5864
if line[0] == '#' {
5965
break
6066
}
6167
}
6268

69+
if ls := string(line); strings.HasPrefix(ls, "#script-") {
70+
switch {
71+
case strings.HasSuffix(ls, "-on\n"):
72+
ta.scripting = true
73+
case strings.HasSuffix(ls, "-off\n"):
74+
ta.scripting = false
75+
default:
76+
return nil, fmt.Errorf(`got %q, want "#script-on" or "#script-off"`, line)
77+
}
78+
for {
79+
line, err = r.ReadSlice('\n')
80+
if err != nil {
81+
return nil, err
82+
}
83+
if line[0] == '#' {
84+
break
85+
}
86+
}
87+
}
88+
6389
if string(line) == "#document-fragment\n" {
6490
line, err = r.ReadSlice('\n')
6591
if err != nil {
66-
return "", "", "", err
92+
return nil, err
6793
}
68-
context = strings.TrimSpace(string(line))
94+
ta.context = strings.TrimSpace(string(line))
6995
line, err = r.ReadSlice('\n')
7096
if err != nil {
71-
return "", "", "", err
97+
return nil, err
7298
}
7399
}
74100

75101
// Read the dump of what the parse tree should be.
76102
if string(line) != "#document\n" {
77-
return "", "", "", fmt.Errorf(`got %q want "#document\n"`, line)
103+
return nil, fmt.Errorf(`got %q want "#document\n"`, line)
78104
}
79105
inQuote := false
80106
for {
81107
line, err = r.ReadSlice('\n')
82108
if err != nil && err != io.EOF {
83-
return "", "", "", err
109+
return nil, err
84110
}
85111
trimmed := bytes.Trim(line, "| \n")
86112
if len(trimmed) > 0 {
@@ -96,7 +122,8 @@ func readParseTest(r *bufio.Reader) (text, want, context string, err error) {
96122
}
97123
b = append(b, line...)
98124
}
99-
return text, string(b), context, nil
125+
ta.want = string(b)
126+
return ta, nil
100127
}
101128

102129
func dumpIndent(w io.Writer, level int) {
@@ -220,18 +247,18 @@ func TestParser(t *testing.T) {
220247
r := bufio.NewReader(f)
221248

222249
for i := 0; ; i++ {
223-
text, want, context, err := readParseTest(r)
250+
ta, err := readParseTest(r)
224251
if err == io.EOF {
225252
break
226253
}
227254
if err != nil {
228255
t.Fatal(err)
229256
}
230257

231-
err = testParseCase(text, want, context)
258+
err = testParseCase(ta.text, ta.want, ta.context, ParseOptionEnableScripting(ta.scripting))
232259

233260
if err != nil {
234-
t.Errorf("%s test #%d %q, %s", tf, i, text, err)
261+
t.Errorf("%s test #%d %q, %s", tf, i, ta.text, err)
235262
}
236263
}
237264
}
@@ -319,7 +346,7 @@ func testParseCase(text, want, context string, opts ...ParseOption) (err error)
319346
go func() {
320347
pw.CloseWithError(Render(pw, doc))
321348
}()
322-
doc1, err := Parse(pr)
349+
doc1, err := ParseWithOptions(pr, opts...)
323350
if err != nil {
324351
return err
325352
}

0 commit comments

Comments
 (0)