Skip to content

fix: handle escaping selectors #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ function getSingleLocalNamesForComposes(root) {
});
}

const whitespace = '[\\x20\\t\\r\\n\\f]';
const unescapeRegExp = new RegExp(
'\\\\([\\da-f]{1,6}' + whitespace + '?|(' + whitespace + ')|.)',
'ig'
);

function unescape(str) {
return str.replace(unescapeRegExp, (_, escaped, escapedWhitespace) => {
const high = '0x' + escaped - 0x10000;

// NaN means non-codepoint
// Workaround erroneous numeric interpretation of +"0x"
return high !== high || escapedWhitespace
? escaped
: high < 0
? // BMP codepoint
String.fromCharCode(high + 0x10000)
: // Supplemental Plane codepoint (surrogate pair)
String.fromCharCode((high >> 10) | 0xd800, (high & 0x3ff) | 0xdc00);
});
}

const processor = postcss.plugin('postcss-modules-scope', function(options) {
return css => {
const generateScopedName =
Expand All @@ -64,10 +86,15 @@ const processor = postcss.plugin('postcss-modules-scope', function(options) {
css.source.input.from,
css.source.input.css
);

exports[name] = exports[name] || [];
if (exports[name].indexOf(scopedName) < 0) {
exports[name].push(scopedName);

const unescapedScopedName = unescape(scopedName);

if (exports[name].indexOf(unescapedScopedName) < 0) {
exports[name].push(unescapedScopedName);
}

return scopedName;
}

Expand Down
17 changes: 16 additions & 1 deletion test/test-cases/export-class/expected.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
color: rebeccapurple;
}

._input__\31 a2b3c {
color: gainsboro;
}

._input__\32 {
color: aqua;
}

._input__𝌆 {
color: fuchsia;
}

@media (max-width: 520px) {
/* selector doubled to increase specificity */
._input__exportName._input__exportName {
Expand All @@ -22,7 +34,10 @@
}

:export {
2: _input__2;
exportName: _input__exportName;
::::: _input__\:\:\:\:;
::::: _input__::::;
1a2b3c: _input__1a2b3c;
𝌆: _input__𝌆;
newExport: _input__newExport;
}
12 changes: 12 additions & 0 deletions test/test-cases/export-class/source.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
color: rebeccapurple;
}

:local(.\31 a2b3c) {
color: gainsboro;
}

:local(.\32) {
color: aqua;
}

:local(.𝌆) {
color: fuchsia;
}

@media (max-width: 520px) {
/* selector doubled to increase specificity */
:local(.exportName):local(.exportName) {
Expand Down