Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove unnecessary things
  • Loading branch information
MaximusFT committed Mar 10, 2021
commit a30d719b0d889152dd68436c9fe2f4448bd3db69
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ module: {
- `transform` - In cases where you may want to use the prefix differently for different selectors, it is possible to pass in a custom transform method.
- `ignoreFiles` - List of ignored files for processing.
- `includeFiles` - List of included files for processing.
- `bethSymbol` - Abbr from between, in case you want glue prefix with selector or use non-space sybmol.

## Maintainer

Expand Down
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
module.exports = function postcssPrefixSelector(options) {
const { prefix, bethSymbol = ' ' } = options;
const prefixWithSymbol = prefix.trim() + bethSymbol;
const prefix = options.prefix;
const prefixWithSpace = /\s+$/.test(prefix) ? prefix : `${prefix} `;
const ignoreFiles = options.ignoreFiles ? [].concat(options.ignoreFiles) : [];
const includeFiles = options.includeFiles
? [].concat(options.includeFiles)
: [];

const searchSelector = (selector, searchArray) => {
return searchArray.some((rule) => {
if (rule instanceof RegExp) {
return rule.test(selector);
}

return selector === rule;
});
};

return function (root) {
if (
root.source.input.file &&
Expand Down Expand Up @@ -43,20 +33,30 @@ module.exports = function postcssPrefixSelector(options) {
}

rule.selectors = rule.selectors.map((selector) => {
if (options.exclude && searchSelector(selector, options.exclude)) {
if (options.exclude && excludeSelector(selector, options.exclude)) {
return selector;
}

if (options.transform) {
return options.transform(
prefix,
selector,
prefixWithSymbol + selector
prefixWithSpace + selector
);
}

return prefixWithSymbol + selector;
return prefixWithSpace + selector;
});
});
};
};

function excludeSelector(selector, excludeArr) {
return excludeArr.some((excludeRule) => {
if (excludeRule instanceof RegExp) {
return excludeRule.test(selector);
}

return selector === excludeRule;
});
}
4 changes: 2 additions & 2 deletions test/fixtures/between-symbol-plus-selector.expected.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.hello+.a {
.hello+ .a {
color: coral;
}

.hello+.b {
.hello+ .b {
color: deepskyblue;
}
9 changes: 5 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,10 @@ it('should use custom symbol between prefix and selector. Use empty to glue', ()
const out = postcss()
.use(
prefixer({
prefix: '.hello ',
bethSymbol: '',
prefix: '.hello',
transform(prefix, selector) {
return prefix + selector;
},
})
)
.process(getFixtureContents('between-symbol-selector.css')).css;
Expand All @@ -168,8 +170,7 @@ it('should use custom symbol between prefix and selector. Use "+"', () => {
const out = postcss()
.use(
prefixer({
prefix: '.hello ',
bethSymbol: '+',
prefix: '.hello+',
})
)
.process(getFixtureContents('between-symbol-plus-selector.css')).css;
Expand Down