( rawUrl: string, matched: string, replacer: CssUrlReplacer, funcName: string = 'url', )
| 2147 | ) |
| 2148 | } |
| 2149 | async function doUrlReplace( |
| 2150 | rawUrl: string, |
| 2151 | matched: string, |
| 2152 | replacer: CssUrlReplacer, |
| 2153 | funcName: string = 'url', |
| 2154 | ) { |
| 2155 | let wrap = '' |
| 2156 | const first = rawUrl[0] |
| 2157 | let unquotedUrl = rawUrl |
| 2158 | if (first === `"` || first === `'`) { |
| 2159 | wrap = first |
| 2160 | unquotedUrl = rawUrl.slice(1, -1) |
| 2161 | } |
| 2162 | if (skipUrlReplacer(unquotedUrl)) { |
| 2163 | return matched |
| 2164 | } |
| 2165 | // Remove escape sequences to get the actual file name before resolving. |
| 2166 | unquotedUrl = unquotedUrl.replace(/\\(\W)/g, '$1') |
| 2167 | |
| 2168 | let newUrl = await replacer(unquotedUrl, rawUrl) |
| 2169 | if (newUrl === false) { |
| 2170 | return matched |
| 2171 | } |
| 2172 | |
| 2173 | // The new url might need wrapping even if the original did not have it, e.g. |
| 2174 | // if a space was added during replacement or the URL contains ")" |
| 2175 | if (wrap === '' && (newUrl !== encodeURI(newUrl) || newUrl.includes(')'))) { |
| 2176 | wrap = '"' |
| 2177 | } |
| 2178 | // If wrapping in single quotes and newUrl also contains single quotes, switch to double quotes. |
| 2179 | // Give preference to double quotes since SVG inlining converts double quotes to single quotes. |
| 2180 | if (wrap === "'" && newUrl.includes("'")) { |
| 2181 | wrap = '"' |
| 2182 | } |
| 2183 | // Escape double quotes if they exist (they also tend to be rarer than single quotes) |
| 2184 | if (wrap === '"' && newUrl.includes('"')) { |
| 2185 | newUrl = newUrl.replace(nonEscapedDoubleQuoteRe, '\\"') |
| 2186 | } |
| 2187 | return `${funcName}(${wrap}${newUrl}${wrap})` |
| 2188 | } |
| 2189 | |
| 2190 | async function doImportCSSReplace( |
| 2191 | rawUrl: string, |
no test coverage detected