|
| 1 | +import type {SrcsetCandidate} from "./types"; |
| 2 | + |
| 3 | +export type InternalDescriptor = |
| 4 | + | {kind: "density"; value: number; raw: string} |
| 5 | + | {kind: "width"; value: number; raw: string} |
| 6 | + | {kind: "invalid"; raw: string}; |
| 7 | + |
| 8 | +export type InternalCandidate = { |
| 9 | + candidate: SrcsetCandidate; |
| 10 | + descriptors: InternalDescriptor[]; |
| 11 | + index: number; |
| 12 | + raw: string; |
| 13 | + url: string; |
| 14 | +}; |
| 15 | + |
| 16 | +const ASCII_WHITESPACE = /[\t\n\f\r ]/u; |
| 17 | +const DENSITY_DESCRIPTOR = /^(\d+(?:\.\d+)?|\.\d+)x$/u; |
| 18 | +const WIDTH_DESCRIPTOR = /^(\d+)w$/u; |
| 19 | + |
| 20 | +export function parseInternal(input: string): InternalCandidate[] { |
| 21 | + return splitCandidates(input).map((segment, index) => parseCandidateSegment(segment, index)); |
| 22 | +} |
| 23 | + |
| 24 | +function splitCandidates(input: string): string[] { |
| 25 | + const segments: string[] = []; |
| 26 | + const length = input.length; |
| 27 | + let index = 0; |
| 28 | + |
| 29 | + while (index < length) { |
| 30 | + while (index < length && isCandidateLeadingIgnored(input[index])) { |
| 31 | + index += 1; |
| 32 | + } |
| 33 | + |
| 34 | + if (index >= length) { |
| 35 | + break; |
| 36 | + } |
| 37 | + |
| 38 | + const start = index; |
| 39 | + let seenUrlWhitespace = false; |
| 40 | + |
| 41 | + while (index < length) { |
| 42 | + const char = input[index]; |
| 43 | + |
| 44 | + if (isAsciiWhitespace(char)) { |
| 45 | + seenUrlWhitespace = true; |
| 46 | + index += 1; |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + if (char === "," && (seenUrlWhitespace || isFallbackSeparator(input, index))) { |
| 51 | + break; |
| 52 | + } |
| 53 | + |
| 54 | + index += 1; |
| 55 | + } |
| 56 | + |
| 57 | + const segment = input.slice(start, index).trim(); |
| 58 | + |
| 59 | + if (segment.length > 0) { |
| 60 | + segments.push(segment); |
| 61 | + } |
| 62 | + |
| 63 | + if (input[index] === ",") { |
| 64 | + index += 1; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return segments; |
| 69 | +} |
| 70 | + |
| 71 | +function parseCandidateSegment(segment: string, index: number): InternalCandidate { |
| 72 | + const [url = "", ...descriptorTokens] = segment.split(ASCII_WHITESPACE).filter(Boolean); |
| 73 | + const descriptors = descriptorTokens.map(parseDescriptor); |
| 74 | + const descriptor = descriptors[0]; |
| 75 | + |
| 76 | + return { |
| 77 | + candidate: buildCandidate(url, descriptors.length === 1 ? descriptor : undefined), |
| 78 | + descriptors, |
| 79 | + index, |
| 80 | + raw: segment, |
| 81 | + url, |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +function parseDescriptor(token: string): InternalDescriptor { |
| 86 | + const width = WIDTH_DESCRIPTOR.exec(token); |
| 87 | + |
| 88 | + if (width) { |
| 89 | + return { |
| 90 | + kind: "width", |
| 91 | + raw: token, |
| 92 | + value: Number(width[1]), |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + const density = DENSITY_DESCRIPTOR.exec(token); |
| 97 | + |
| 98 | + if (density) { |
| 99 | + return { |
| 100 | + kind: "density", |
| 101 | + raw: token, |
| 102 | + value: Number(density[1]), |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + return { |
| 107 | + kind: "invalid", |
| 108 | + raw: token, |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +function buildCandidate(url: string, descriptor?: InternalDescriptor): SrcsetCandidate { |
| 113 | + if (descriptor?.kind === "density") { |
| 114 | + return { |
| 115 | + url, |
| 116 | + density: descriptor.value, |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + if (descriptor?.kind === "width") { |
| 121 | + return { |
| 122 | + url, |
| 123 | + width: descriptor.value, |
| 124 | + }; |
| 125 | + } |
| 126 | + |
| 127 | + return {url}; |
| 128 | +} |
| 129 | + |
| 130 | +function isAsciiWhitespace(char: string | undefined): boolean { |
| 131 | + return char !== undefined && ASCII_WHITESPACE.test(char); |
| 132 | +} |
| 133 | + |
| 134 | +function isCandidateLeadingIgnored(char: string | undefined): boolean { |
| 135 | + return char === "," || isAsciiWhitespace(char); |
| 136 | +} |
| 137 | + |
| 138 | +function isFallbackSeparator(input: string, commaIndex: number): boolean { |
| 139 | + let index = commaIndex + 1; |
| 140 | + |
| 141 | + while (index < input.length && isAsciiWhitespace(input[index])) { |
| 142 | + index += 1; |
| 143 | + } |
| 144 | + |
| 145 | + return index === input.length || index > commaIndex + 1; |
| 146 | +} |
0 commit comments