Skip to content

Commit

Permalink
Update: escape double quote in CSSPackager
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinyaigeek committed Mar 6, 2022
1 parent c6c4fac commit ea56294
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
6 changes: 5 additions & 1 deletion packages/core/utils/src/replaceBundleReferences.js
Expand Up @@ -34,12 +34,14 @@ export function replaceURLReferences({
contents,
map,
relative = true,
shouldBeEscaped = false,
}: {|
bundle: NamedBundle,
bundleGraph: BundleGraph<NamedBundle>,
contents: string,
relative?: boolean,
map?: ?SourceMap,
shouldBeEscaped?: boolean,
|}): {|+contents: string, +map: ?SourceMap|} {
let replacements = new Map();
let urlDependencies = [];
Expand Down Expand Up @@ -83,7 +85,7 @@ export function replaceURLReferences({
);
}

return performReplacement(replacements, contents, map);
return performReplacement(replacements, contents, map, shouldBeEscaped);
}

/*
Expand Down Expand Up @@ -201,9 +203,11 @@ function performReplacement(
replacements: ReplacementMap,
contents: string,
map?: ?SourceMap,
shouldBeEscaped = true,
): {|+contents: string, +map: ?SourceMap|} {
let finalContents = contents;
for (let {from, to} of replacements.values()) {
to = shouldBeEscaped ? to.replace(/"/g, '\\"').replace(/'/g, "\\'") : to;
// Perform replacement
finalContents = finalContents.split(from).join(to);
}
Expand Down
1 change: 1 addition & 0 deletions packages/packagers/css/src/CSSPackager.js
Expand Up @@ -129,6 +129,7 @@ export default (new Packager({
bundleGraph,
contents,
map,
shouldBeEscaped: true,
}));

return replaceInlineReferences({
Expand Down
15 changes: 9 additions & 6 deletions packages/transformers/css-experimental/src/CSSTransformer.js
Expand Up @@ -83,13 +83,16 @@ export default (new Transformer({
symbols: new Map([['*', {local: '*', isWeak: true, loc}]]),
});
} else if (dep.type === 'url') {
// if dependency imported with url() includes ", escape " double quotes because this url value will be enclosed with double quote "
asset.addURLDependency(dep.url.replace(/"/g, '\\"'), {
loc,
meta: {
placeholder: dep.placeholder,
// if dependency imported with url() includes \" or \' (escaped quote), this escaped quote should be non-escaped because parcel resolver cannot resolve URL includes escaped quote. URL in url() will be enclosed with double quotes("), so this URL will be re-escaped in the packager.
asset.addURLDependency(
dep.url.replace(/\\"/g, '"').replace(/\\'/g, "'"),
{
loc,
meta: {
placeholder: dep.placeholder,
},
},
});
);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/transformers/css/src/CSSTransformer.js
Expand Up @@ -171,8 +171,8 @@ export default (new Transformer({
) {
let urlNode = node.nodes[0];
let url = asset.addURLDependency(
// if dependency imported with url() includes ", escape " double quotes because this url value will be enclosed with double quote "
urlNode.value.replace(/"/g, '\\"'),
// if dependency imported with url() includes \" or \' (escaped quote), this escaped quote should be non-escaped because parcel resolver cannot resolve URL includes escaped quote. URL in url() will be enclosed with double quotes("), so this URL will be re-escaped in the packager.
urlNode.value.replace(/\\"/g, '"').replace(/\\'/g, "'"),
{
loc:
decl.source &&
Expand Down

0 comments on commit ea56294

Please sign in to comment.