Skip to content
This repository was archived by the owner on Feb 21, 2024. It is now read-only.

Commit 8639816

Browse files
committed
fix: escape regexp identifiers in string options
1 parent c6afbc9 commit 8639816

File tree

6 files changed

+40
-7
lines changed

6 files changed

+40
-7
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules/
22
coverage/
3-
tests/fixtures/*-processed.css
3+
develop-output/
4+
*-processed.css
45
*.log

develop/index.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,28 @@ const postcss = require('postcss');
66
const plugin = require('../lib');
77

88
const fixture = 'develop';
9-
const fixturePath = path.join(process.cwd(), 'tests', 'fixtures');
10-
const input = fs.readFileSync(path.join(fixturePath, `${fixture}.css`), 'utf-8');
9+
const inputPath = path.join(process.cwd(), 'tests/fixtures/develop');
10+
const outputPath = path.join(process.cwd(), 'tests/fixtures/develop-output');
11+
const input = fs.readFileSync(path.join(inputPath, `${fixture}.css`), 'utf-8');
1112

1213
const write = (result) => {
1314
console.log(`\n⚡️ ${fixture}-processed.css updated\n`);
1415

15-
fs.writeFileSync(path.join(fixturePath, `${fixture}-processed.css`), result.css);
16+
fs.writeFileSync(path.join(outputPath, `${fixture}-processed.css`), result.css);
1617
};
1718

1819
postcss()
19-
.use(plugin)
20+
.use(
21+
plugin({
22+
exclude: [
23+
'.excluded',
24+
'[data-icon]',
25+
':not(.something)',
26+
'#id',
27+
'strong'
28+
]
29+
})
30+
)
2031
.process(input, { from: undefined })
2132
.then(write)
2233
.catch(console.error);

lib/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const postcss = require('postcss');
4+
const escapeIdentifiers = require('escape-regex-string');
45

56
module.exports = postcss.plugin('postcss-selector-scope', (options = {}) => {
67
const settings = Object.assign(
@@ -24,7 +25,7 @@ module.exports = postcss.plugin('postcss-selector-scope', (options = {}) => {
2425
const allExclusions = settings.exclude.concat(defaultExclusions);
2526
const excludedSelectors = allExclusions.map((value) => {
2627
if (typeof value === 'string') {
27-
return new RegExp(value, 'i');
28+
return new RegExp(`${escapeIdentifiers(value)}$`, 'i');
2829
}
2930

3031
if (value instanceof RegExp) {

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
},
4343
"main": "lib/index.js",
4444
"dependencies": {
45+
"escape-regex-string": "^1.0.6",
4546
"postcss": "^7.0.14"
4647
},
4748
"devDependencies": {

tests/fixtures/develop.css renamed to tests/fixtures/develop/develop.css

+15-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,21 @@ body .class,
2424

2525
.class-with-numbers-234,
2626
.class-with-from,
27-
.class-with-to {
27+
.class-with-to,
28+
.fromsomething,
29+
.tosomething,
30+
.class-with-excluded,
31+
.class-withexcluded,
32+
.excluded,
33+
.parent .excluded,
34+
.item.excluded,
35+
.excluded-any,
36+
.excludedany,
37+
#id,
38+
#idnotme,
39+
[data-icon],
40+
[data-icon="icon"].any,
41+
.any:not(.something) {
2842
color: red;
2943
}
3044

0 commit comments

Comments
 (0)