( rawUrl: string, matched: string, replacer: CssUrlReplacer, funcName: string = 'url', )
| 117 | } |
| 118 | |
| 119 | async function doUrlReplace( |
| 120 | rawUrl: string, |
| 121 | matched: string, |
| 122 | replacer: CssUrlReplacer, |
| 123 | funcName: string = 'url', |
| 124 | ) { |
| 125 | let wrap = '' |
| 126 | const first = rawUrl[0] |
| 127 | if (first === `"` || first === `'`) { |
| 128 | wrap = first |
| 129 | rawUrl = rawUrl.slice(1, -1) |
| 130 | } |
| 131 | |
| 132 | if (skipUrlReplacer(rawUrl)) { |
| 133 | return matched |
| 134 | } |
| 135 | |
| 136 | let newUrl = await replacer(rawUrl) |
| 137 | // The new url might need wrapping even if the original did not have it, e.g. if a space was added during replacement |
| 138 | if (wrap === '' && newUrl !== encodeURI(newUrl)) { |
| 139 | wrap = '"' |
| 140 | } |
| 141 | // If wrapping in single quotes and newUrl also contains single quotes, switch to double quotes. |
| 142 | // Give preference to double quotes since SVG inlining converts double quotes to single quotes. |
| 143 | if (wrap === "'" && newUrl.includes("'")) { |
| 144 | wrap = '"' |
| 145 | } |
| 146 | // Escape double quotes if they exist (they also tend to be rarer than single quotes) |
| 147 | if (wrap === '"' && newUrl.includes('"')) { |
| 148 | newUrl = newUrl.replace(nonEscapedDoubleQuoteRE, '\\"') |
| 149 | } |
| 150 | return `${funcName}(${wrap}${newUrl}${wrap})` |
| 151 | } |
| 152 | |
| 153 | function skipUrlReplacer(rawUrl: string, aliases?: string[]) { |
| 154 | return ( |
no test coverage detected