Skip to content

Commit 4b2d346

Browse files
Greg GrothausMichaelRybak
authored andcommitted
Parse-srcset regex improvements.
Match the changes in ampproject/amphtml#30252 PiperOrigin-RevId: 334206765
1 parent db23682 commit 4b2d346

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

transformer/internal/amphtml/srcset.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,39 @@ func roundUp(w int) int {
7878
// more spaces + a non empty string containing no space or commas).
7979
// Doesn't capture the initial space.
8080
//
81-
// \s* Match, but don't capture leading spaces
81+
// \s* Match, but don't capture leading spaces.
8282
// (?:,\s*)? Optionally match comma and trailing space,
8383
// but don't capture comma.
8484
// ([^,\s]\S*[^,\s]) Match something like "google.com/favicon.ico"
85-
// but not ",google.com/favicon.ico,"
85+
// but not ",google.com/favicon.ico,".
8686
// \s* Match, but don't capture spaces.
87-
// ([\d]+.?[\d]*[w|x])? e.g. "5w" or "5x" or "10.2x"
87+
// ( Match the width or density descriptor...
88+
// [1-9]\d*[wx] which is a non-zero integer followed by a w
89+
// or an x ...
90+
// | or ...
91+
// [1-9]\d*\.\d+x a decimal with its whole-number part greater
92+
// than zero and followed by an x ...
93+
// | or ...
94+
// 0.\d*[1-9]\d*x a decimal with its fractional part greater
95+
// than zero and followed by an x ...
96+
// )? and make it optional.
8897
// \s* Match, but don't capture space
8998
// (?:(,)\s*)? Optionally match comma and trailing space,
9099
// capturing comma.
91-
var imageCandidateRE = regexp.MustCompile(`\s*(?:,\s*)?([^,\s]\S*[^,\s])\s*([\d]+.?[\d]*[w|x])?\s*(?:(,)\s*)?`)
100+
var imageCandidateRE = regexp.MustCompile(
101+
`\s*` +
102+
`(?:,\s*)?` +
103+
`([^,\s]\S*[^,\s])` +
104+
`\s*` +
105+
`(` +
106+
`[1-9]\d*[wx]` +
107+
`|` +
108+
`[1-9]\d*\.\d+x` +
109+
`|` +
110+
`0.\d*[1-9]\d*` +
111+
`)?` +
112+
`\s*` +
113+
`(?:(,)\s*)?`)
92114

93115
// ParseSrcset parses the given srcset attribute value of its
94116
// image candidates (as defined by

0 commit comments

Comments
 (0)