| 55 | * @return {Srcset<SrcsetSourceDef>} |
| 56 | */ |
| 57 | export function parseSrcset(s) { |
| 58 | const sources = []; |
| 59 | let match; |
| 60 | while ((match = srcsetRegex.exec(s))) { |
| 61 | const url = match[1]; |
| 62 | let width, dpr; |
| 63 | if (match[2]) { |
| 64 | const type = match[3].toLowerCase(); |
| 65 | if (type == 'w') { |
| 66 | width = parseInt(match[2], 10); |
| 67 | } else if (type == 'x') { |
| 68 | dpr = parseFloat(match[2]); |
| 69 | } else { |
| 70 | continue; |
| 71 | } |
| 72 | } else { |
| 73 | // If no "w" or "x" specified, we assume it's "1x". |
| 74 | dpr = 1; |
| 75 | } |
| 76 | sources.push(/** @type {SrcsetSourceDef} */ ({url, width, dpr})); |
| 77 | } |
| 78 | return new Srcset(sources); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * A srcset object contains one or more sources. |