Skip to content

Commit 643fd16

Browse files
maciekmmgopherbot
authored andcommitted
html: fix SOLIDUS '/' handling in attribute parsing
Calling the Tokenizer with HTML elements containing SOLIDUS (/) character in the attribute name results in incorrect tokenization. This is due to violation of the following rule transitions in the WHATWG spec: - https://html.spec.whatwg.org/multipage/parsing.html#attribute-name-state, where we are not reconsuming the character if '/' is encountered - https://html.spec.whatwg.org/multipage/parsing.html#after-attribute-name-state, where we are not switching to self closing state Fixes golang/go#63402 Change-Id: I90d998dd8decde877bd63aa664f3657aa6161024 GitHub-Last-Rev: 3546db8 GitHub-Pull-Request: #195 Reviewed-on: https://go-review.googlesource.com/c/net/+/533518 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Michael Pratt <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent 73e4b50 commit 643fd16

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

html/token.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -910,17 +910,16 @@ func (z *Tokenizer) readTagAttrKey() {
910910
return
911911
}
912912
switch c {
913-
case ' ', '\n', '\r', '\t', '\f', '/':
914-
z.pendingAttr[0].end = z.raw.end - 1
915-
return
916913
case '=':
917914
if z.pendingAttr[0].start+1 == z.raw.end {
918915
// WHATWG 13.2.5.32, if we see an equals sign before the attribute name
919916
// begins, we treat it as a character in the attribute name and continue.
920917
continue
921918
}
922919
fallthrough
923-
case '>':
920+
case ' ', '\n', '\r', '\t', '\f', '/', '>':
921+
// WHATWG 13.2.5.33 Attribute name state
922+
// We need to reconsume the char in the after attribute name state to support the / character
924923
z.raw.end--
925924
z.pendingAttr[0].end = z.raw.end
926925
return
@@ -939,6 +938,11 @@ func (z *Tokenizer) readTagAttrVal() {
939938
if z.err != nil {
940939
return
941940
}
941+
if c == '/' {
942+
// WHATWG 13.2.5.34 After attribute name state
943+
// U+002F SOLIDUS (/) - Switch to the self-closing start tag state.
944+
return
945+
}
942946
if c != '=' {
943947
z.raw.end--
944948
return

html/token_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,21 @@ var tokenTests = []tokenTest{
601601
`<p =asd>`,
602602
`<p =asd="">`,
603603
},
604+
{
605+
"forward slash before attribute name",
606+
`<p/=">`,
607+
`<p ="="">`,
608+
},
609+
{
610+
"forward slash before attribute name with spaces around",
611+
`<p / =">`,
612+
`<p ="="">`,
613+
},
614+
{
615+
"forward slash after attribute name followed by a character",
616+
`<p a/ ="">`,
617+
`<p a="" =""="">`,
618+
},
604619
}
605620

606621
func TestTokenizer(t *testing.T) {

0 commit comments

Comments
 (0)