Skip to content

Commit a47f034

Browse files
committed
Add support in imagesrcset for URLs with commas and leading commas, per
https://html.spec.whatwg.org/multipage/images.html#parsing-a-srcset-attribute. Addresses #461. PiperOrigin-RevId: 338555233
1 parent 77a56fc commit a47f034

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

transformer/transformer.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ var ampAttrRE = func() *regexp.Regexp {
112112
return r
113113
}()
114114

115-
// firstSrcsetSourceRE captures the first source URL from a srcset.
116-
var firstSrcsetSourceRE = regexp.MustCompile(`[^\s,]+`)
115+
// firstSrcsetSourceRE captures the first source URL from a srcset. Caller must
116+
// remove trailing comma, if present.
117+
var firstSrcsetSourceRE = regexp.MustCompile(`^[\s,]*([^\s]+)`)
117118

118119
// The allowed AMP formats, and their serialization as an html "amp4" attribute.
119120
var ampFormatSuffixes = map[rpb.Request_HtmlFormat]string{
@@ -229,11 +230,11 @@ func extractPreloads(dom *amphtml.DOM) []*rpb.Metadata_Preload {
229230
}
230231
// The href doesn't really matter here. Browsers will ignore it and instead prioritize whichever source is selected from imagesrcset. However, the Link header *must* have it.
231232
// Stub this by just finding the first source URL possible.
232-
firstSource := firstSrcsetSourceRE.FindString(imagesrcset)
233+
firstSource := firstSrcsetSourceRE.FindStringSubmatch(imagesrcset)[1]
233234
if firstSource == "" {
234235
continue
235236
}
236-
href = firstSource
237+
href = strings.TrimSuffix(firstSource, ",")
237238
}
238239
preload := &rpb.Metadata_Preload{Url: href, As: "image"}
239240
for _, attr := range current.Attr {

transformer/transformer_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,26 @@ func TestPreloads(t *testing.T) {
125125
"<html ⚡><head></head><body></body></html>",
126126
[]*rpb.Metadata_Preload{{Url: "foo", As: "image", Attributes: []*rpb.Metadata_Preload_Attribute{{Key: "imagesrcset", Val: "bar"}}}},
127127
},
128+
{
129+
`<html ⚡><link rel=preload as=image imagesrcset="bar,">`,
130+
"<html ⚡><head></head><body></body></html>",
131+
[]*rpb.Metadata_Preload{{Url: "bar", As: "image", Attributes: []*rpb.Metadata_Preload_Attribute{{Key: "imagesrcset", Val: "bar,"}}}},
132+
},
133+
{
134+
`<html ⚡><link rel=preload as=image imagesrcset="bar, baz">`,
135+
"<html ⚡><head></head><body></body></html>",
136+
[]*rpb.Metadata_Preload{{Url: "bar", As: "image", Attributes: []*rpb.Metadata_Preload_Attribute{{Key: "imagesrcset", Val: "bar, baz"}}}},
137+
},
138+
{
139+
`<html ⚡><link rel=preload as=image imagesrcset=",, ,,bar">`,
140+
"<html ⚡><head></head><body></body></html>",
141+
[]*rpb.Metadata_Preload{{Url: "bar", As: "image", Attributes: []*rpb.Metadata_Preload_Attribute{{Key: "imagesrcset", Val: ",, ,,bar"}}}},
142+
},
143+
{
144+
`<html ⚡><link rel=preload as=image imagesrcset="bar,baz quux">`,
145+
"<html ⚡><head></head><body></body></html>",
146+
[]*rpb.Metadata_Preload{{Url: "bar,baz", As: "image", Attributes: []*rpb.Metadata_Preload_Attribute{{Key: "imagesrcset", Val: "bar,baz quux"}}}},
147+
},
128148
{
129149
"<html ⚡><link rel=preload as=image href=foo imagesrcset=bar imagesizes=100vw>",
130150
"<html ⚡><head></head><body></body></html>",

0 commit comments

Comments
 (0)