Skip to content

Commit

Permalink
fix: handling escaped urls
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 11, 2019
1 parent e091d27 commit afb048f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
15 changes: 7 additions & 8 deletions src/plugins/postcss-url-parser.js
Expand Up @@ -2,6 +2,8 @@ import postcss from 'postcss';
import valueParser from 'postcss-value-parser';
import { urlToRequest } from 'loader-utils';

import { unescape } from '../utils';

const pluginName = 'postcss-url-parser';

const isUrlFunc = /url/i;
Expand Down Expand Up @@ -78,23 +80,20 @@ function getUrlsFromValue(value, result, filter, decl) {
}

const splittedUrl = url.split(/(\?)?#/);
let normalizedUrl = urlToRequest(decodeURIComponent(splittedUrl[0]));
let [normalizedUrl] = splittedUrl;
const [, singleQuery, hashValue] = splittedUrl;
const hash =
singleQuery || hashValue
? `${singleQuery ? '?' : ''}${hashValue ? `#${hashValue}` : ''}`
: '';

// Remove extra escaping requirements for `require`
// See https://drafts.csswg.org/css-values-3/#urls
if (!isStringNode && /\\["'() \t\n]/.test(normalizedUrl)) {
normalizedUrl = normalizedUrl.replace(/\\(["'() \t\n])/g, '$1');
}
// https://drafts.csswg.org/css-values-4/#strings
else if (isStringNode && /\\[\n]/.test(normalizedUrl)) {
// See https://drafts.csswg.org/css-values-4/#strings
if (isStringNode && /\\[\n]/.test(normalizedUrl)) {
normalizedUrl = normalizedUrl.replace(/\\[\n]/g, '');
}

normalizedUrl = urlToRequest(decodeURIComponent(unescape(normalizedUrl)));

urls.push({ node, url: normalizedUrl, hash, needQuotes });
});

Expand Down
1 change: 1 addition & 0 deletions src/utils.js
Expand Up @@ -395,6 +395,7 @@ function getExportCode(
}

export {
unescape,
getFilter,
getModulesPlugins,
normalizeSourceMap,
Expand Down

0 comments on commit afb048f

Please sign in to comment.