Skip to content
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

fix: avoid all escaped characters replaced #917

Merged
merged 5 commits into from Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 47 additions & 4 deletions src/utils.js
Expand Up @@ -88,10 +88,7 @@ function getLocalIdent(loaderContext, localIdentName, localName, options) {
options
);

return hash
.replace(/\\@/g, '@')
.replace(new RegExp('[^a-zA-Z0-9@\\-_\u00A0-\uFFFF]', 'g'), '-')
.replace(/^((-?[0-9])|--)/, '_$1');
return normalizeIdentifier(hash);
}

function getFilter(filter, resourcePath, defaultFilter = null) {
Expand All @@ -108,6 +105,52 @@ function getFilter(filter, resourcePath, defaultFilter = null) {
};
}

function normalizeIdentifier(value) {
const escapedSymbols = [
'~',
'!',
'@',
'#',
'$',
'%',
'&',
'^',
'*',
'(',
')',
'{',
'}',
'[',
']',
'`',
'/',
'=',
'?',
'+',
'\\',
'|',
'-',
'_',
':',
';',
"'",
'"',
',',
'<',
'.',
'>',
];

const identifiersRegExp = new RegExp(
`[^a-zA-Z0-9${escapedSymbols.join('\\')}\\-_\u00A0-\uFFFF]`,
'g'
);

return value
.replace(identifiersRegExp, '-')
.replace(/^((-?[0-9])|--)/, '_$1');
}

export {
getImportPrefix,
getLocalIdent,
Expand Down
106 changes: 105 additions & 1 deletion test/__snapshots__/localIdentName-option.test.js.snap
Expand Up @@ -39,6 +39,11 @@ Array [
:local(.m_x_\\\\@) {
margin-left: auto !important;
margin-right: auto !important;
}

:local(.B\\\\&W\\\\?) {
margin-left: auto !important;
margin-right: auto !important;
}",
"",
],
Expand Down Expand Up @@ -86,6 +91,11 @@ Array [
:local(.m_x_\\\\@) {
margin-left: auto !important;
margin-right: auto !important;
}

:local(.B\\\\&W\\\\?) {
margin-left: auto !important;
margin-right: auto !important;
}",
"",
],
Expand Down Expand Up @@ -133,6 +143,11 @@ Array [
:local(.m_x_\\\\@) {
margin-left: auto !important;
margin-right: auto !important;
}

:local(.B\\\\&W\\\\?) {
margin-left: auto !important;
margin-right: auto !important;
}",
"",
],
Expand Down Expand Up @@ -180,6 +195,11 @@ Array [
:local(.m_x_\\\\@) {
margin-left: auto !important;
margin-right: auto !important;
}

:local(.B\\\\&W\\\\?) {
margin-left: auto !important;
margin-right: auto !important;
}",
"",
],
Expand Down Expand Up @@ -227,6 +247,11 @@ Array [
:local(.m_x_\\\\@) {
margin-left: auto !important;
margin-right: auto !important;
}

:local(.B\\\\&W\\\\?) {
margin-left: auto !important;
margin-right: auto !important;
}",
"",
],
Expand Down Expand Up @@ -274,6 +299,11 @@ Array [
:local(.m_x_\\\\@) {
margin-left: auto !important;
margin-right: auto !important;
}

:local(.B\\\\&W\\\\?) {
margin-left: auto !important;
margin-right: auto !important;
}",
"",
],
Expand Down Expand Up @@ -321,6 +351,11 @@ Array [
:local(.m_x_\\\\@) {
margin-left: auto !important;
margin-right: auto !important;
}

:local(.B\\\\&W\\\\?) {
margin-left: auto !important;
margin-right: auto !important;
}",
"",
],
Expand All @@ -329,11 +364,75 @@ Array [

exports[`localIdentName option should use hash prefix: warnings 1`] = `Array []`;

exports[`localIdentName option should сorrectly replace escaped symbols in selector: errors 1`] = `Array []`;

exports[`localIdentName option should сorrectly replace escaped symbols in selector: locals 1`] = `
Object {
"-a0-34a___f": "-a0-34a___f--2nJ5",
"B&W?": "B&W?--1s8i",
"_test": "_test--23te",
"className": "className--1E8H",
"m_x_@": "m_x_@--2G3b",
"someId": "someId--3w7J",
"subClass": "subClass--3lo0",
"test": "test--NW9Y",
}
`;

exports[`localIdentName option should сorrectly replace escaped symbols in selector: module (evaluated) 1`] = `
Array [
Array [
1,
".test--NW9Y {
background: red;
}

._test--23te {
background: blue;
}

.className--1E8H {
background: red;
}

#someId--3w7J {
background: green;
}

.className--1E8H .subClass--3lo0 {
color: green;
}

#someId--3w7J .subClass--3lo0 {
color: blue;
}

.-a0-34a___f--2nJ5 {
color: red;
}

.m_x_\\\\@--2G3b {
margin-left: auto !important;
margin-right: auto !important;
}

.B\\\\&W\\\\?--1s8i {
margin-left: auto !important;
margin-right: auto !important;
}",
"",
],
]
`;

exports[`localIdentName option should сorrectly replace escaped symbols in selector: warnings 1`] = `Array []`;

exports[`localIdentName option should сorrectly replace symbol @ in selector: errors 1`] = `Array []`;

exports[`localIdentName option should сorrectly replace symbol @ in selector: locals 1`] = `
Object {
"-a0-34a___f": "-a0-34a___f--2nJ5",
"B&W?": "B&W?--1s8i",
"_test": "_test--23te",
"className": "className--1E8H",
"m_x_@": "m_x_@--2G3b",
Expand Down Expand Up @@ -375,7 +474,12 @@ Array [
color: red;
}

.m_x_@--2G3b {
.m_x_\\\\@--2G3b {
margin-left: auto !important;
margin-right: auto !important;
}

.B\\\\&W\\\\?--1s8i {
margin-left: auto !important;
margin-right: auto !important;
}",
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/modules/localIdentName.css
Expand Up @@ -29,4 +29,9 @@
:local(.m_x_\@) {
margin-left: auto !important;
margin-right: auto !important;
}

:local(.B\&W\?) {
margin-left: auto !important;
margin-right: auto !important;
}
alreadyExisted marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 1 addition & 3 deletions test/localIdentName-option.test.js
Expand Up @@ -118,7 +118,7 @@ describe('localIdentName option', () => {
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

it('should сorrectly replace symbol @ in selector', async () => {
it('should сorrectly replace escaped symbols in selector', async () => {
const config = {
loader: {
options: {
Expand All @@ -134,8 +134,6 @@ describe('localIdentName option', () => {
const module = modules.find((m) => m.id === testId);
const evaluatedModule = evaluated(module.source, modules);

expect(evaluatedModule.locals['m_x_@']).toContain('m_x_@');

expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
expect(evaluatedModule.locals).toMatchSnapshot('locals');
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
Expand Down