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

Feat/add comment exclusions #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 26 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,33 @@ module.exports = postcss.plugin('postcss-increase-specificity', function(options
var opts = objectAssign({}, defaults, options);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay @mihaisavezi 🙇

This looks like a great v1 we can merge of this feature 👍

My comments are mainly framing everything in terms of disable/enable instead of exclude


return function(css) {
css.walkRules(function(rule) {
// Avoid adding additional selectors (stackableRoot) to descendant rules of @keyframe {}
// i.e. `from`, `to`, or `{number}%`
var isInsideKeyframes = rule.parent.type === 'atrule' && rule.parent.name === 'keyframes';
let isExcluded = false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let isExcluded = false;
let isDisabled = false;

css.walk(function(node) {

if(!isInsideKeyframes) {
increaseSpecifityOfRule(rule, opts);
if (node.type === 'comment') {

const commentStart = 'no important-start';
const commentEnd = 'no important-end';
Comment on lines +63 to +64
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to

Suggested change
const commentStart = 'no important-start';
const commentEnd = 'no important-end';
const enableComment = 'postcss-increase-specificity enable';
const disableComment = 'postcss-increase-specificity disable';


if(node.text === commentStart) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to worry about comparing whitespace here? Perhaps we need to node.text.trim()?

isExcluded = true;
}

if(node.text === commentEnd) {
isExcluded = false;
}
}

if (node.type === 'rule' && !isExcluded) {
const rule = node;
// Avoid adding additional selectors (stackableRoot) to descendant rules of @keyframe {}
// i.e. `from`, `to`, or `{number}%`
var isInsideKeyframes = rule.parent.type === 'atrule' && rule.parent.name === 'keyframes';

if (!isInsideKeyframes) {
increaseSpecifityOfRule(rule, opts);
}
}
});
})
};
});
13 changes: 13 additions & 0 deletions test/fixtures/comments-to-exclude.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.foo {
background: #f00;
}

/* no important-start */
.boo {
background: green;
}

/* no important-end */
.coo {
background:goldenrod;
}
13 changes: 13 additions & 0 deletions test/fixtures/comments-to-exclude.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
:not(#\9):not(#\9):not(#\9) .foo {
background: #f00;
}

/* no important-start */
.boo {
background: green;
}
/* no important-end */

:not(#\9):not(#\9):not(#\9) .coo {
background:goldenrod;
}
4 changes: 4 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ describe('postcss-increase-specificity', function() {
return testPlugin('./test/fixtures/classes.css', './test/fixtures/classes.expected.css');
});

it('should handle exclusion commments', function () {
return testPlugin('./test/fixtures/comments-to-exclude.css', './test/fixtures/comments-to-exclude.expected.css');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disable-enable-with-comments.css

});

it('should work with multiple classes `.foo, .bar`', function() {
return testPlugin('./test/fixtures/multiple-classes.css', './test/fixtures/multiple-classes.expected.css');
});
Expand Down