( node, context, options: Required<AssetURLOptions> = defaultAssetUrlOptions, )
| 38 | } |
| 39 | |
| 40 | export const transformSrcset: NodeTransform = ( |
| 41 | node, |
| 42 | context, |
| 43 | options: Required<AssetURLOptions> = defaultAssetUrlOptions, |
| 44 | ) => { |
| 45 | if (node.type === NodeTypes.ELEMENT) { |
| 46 | if (srcsetTags.includes(node.tag) && node.props.length) { |
| 47 | node.props.forEach((attr, index) => { |
| 48 | if (attr.name === 'srcset' && attr.type === NodeTypes.ATTRIBUTE) { |
| 49 | if (!attr.value) return |
| 50 | const value = attr.value.content |
| 51 | if (!value) return |
| 52 | const imageCandidates: ImageCandidate[] = value.split(',').map(s => { |
| 53 | // The attribute value arrives here with all whitespace, except |
| 54 | // normal spaces, represented by escape sequences |
| 55 | const [url, descriptor] = s |
| 56 | .replace(escapedSpaceCharacters, ' ') |
| 57 | .trim() |
| 58 | .split(' ', 2) |
| 59 | return { url, descriptor } |
| 60 | }) |
| 61 | |
| 62 | // data urls contains comma after the encoding so we need to re-merge |
| 63 | // them |
| 64 | for (let i = 0; i < imageCandidates.length; i++) { |
| 65 | const { url } = imageCandidates[i] |
| 66 | if (isDataUrl(url)) { |
| 67 | imageCandidates[i + 1].url = |
| 68 | url + ',' + imageCandidates[i + 1].url |
| 69 | imageCandidates.splice(i, 1) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | const shouldProcessUrl = (url: string) => { |
| 74 | return ( |
| 75 | url && |
| 76 | !isExternalUrl(url) && |
| 77 | !isDataUrl(url) && |
| 78 | (options.includeAbsolute || isRelativeUrl(url)) |
| 79 | ) |
| 80 | } |
| 81 | // When srcset does not contain any qualified URLs, skip transforming |
| 82 | if (!imageCandidates.some(({ url }) => shouldProcessUrl(url))) { |
| 83 | return |
| 84 | } |
| 85 | |
| 86 | if (options.base) { |
| 87 | const base = options.base |
| 88 | const set: string[] = [] |
| 89 | let needImportTransform = false |
| 90 | |
| 91 | imageCandidates.forEach(candidate => { |
| 92 | let { url, descriptor } = candidate |
| 93 | descriptor = descriptor ? ` ${descriptor}` : `` |
| 94 | if (url[0] === '.') { |
| 95 | candidate.url = (path.posix || path).join(base, url) |
| 96 | set.push(candidate.url + descriptor) |
| 97 | } else if (shouldProcessUrl(url)) { |
nothing calls this directly
no test coverage detected