Skip to content

Add pseudoClasses option #3

New issue

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

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

Already on GitHub? # to your account

Merged
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
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -111,5 +111,49 @@ Default: `&`

Ancestor selector base symbol

### replaceValues

Type `boolean`
Default: `false`

If this is true then this plugin will look through your declaration values for the placeholder symbol and replace them with the desired selector.

i.e.

```css
.foo {
background-color: red;

&:hover {
background-color: blue;

&::before {
content: '^&';
}
}
}
```

```
.foo {
background-color: red;
}

.foo:hover {
background-color: blue;
}

.foo:hover::before {
content: '.foo'
}
```

### pseudoClasses

Type `boolean`
Default: `false`

If this is true then pseudo classes (`&:hover`, `&:focus`) are included in the parent chain

## Todo's
- Add warning when nestingLevel >= parentsStack.length
18 changes: 17 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,9 @@ var postcss = require('postcss'),

module.exports = postcss.plugin('postcss-nested-ancestors', function (opts) {
opts = assign({
placeholder: '^&'
placeholder: '^&',
replaceValues: false,
pseudoClasses: false
}, opts);

// Advanced options
@@ -34,6 +36,8 @@ module.exports = postcss.plugin('postcss-nested-ancestors', function (opts) {
function getParentSelectorAtLevel(nestingLevel) {
nestingLevel = nestingLevel || 1;

if (opts.pseudoClasses) nestingLevel -= 1;

// @TODO add warning when nestingLevel >= parentsStack.length

return parentsStack.filter( function (rule, index) {
@@ -85,6 +89,18 @@ module.exports = postcss.plugin('postcss-nested-ancestors', function (opts) {
// Replace parents placeholders in rule selector
rule.selectors = rule.selectors.map(replacePlaceholders);

if (opts.replaceValues) {
rule.nodes.forEach(function (ruleNode) {
if (ruleNode.type === 'decl') {
if (ruleNode.value.indexOf(opts.placeholder) >= 0) {
ruleNode.value = replacePlaceholders(
ruleNode.value
);
}
}
});
}

// Process child rules
process(rule);
}
18 changes: 18 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -43,6 +43,24 @@ test('Prepend 2 ancestors in double selector', t => {
);
});

test('Replace declaration values if replaceValues is true', t => {
return run( t,
'.a{ background: red; &:hover {&::before { content: "^&"; }}}',
'.a{ background: red; &:hover {&::before { content: ".a"; }}}',
{ replaceValues: true }
);
});

test('Take into account pseudo classes if pseudoClasses is true', t => {
return run( t,
'.a{ background: red; &:hover {&::before ' +
'{ content: "^&"; }}}',
'.a{ background: red; &:hover {&::before ' +
'{ content: ".a:hover"; }}}',
{ replaceValues: true, pseudoClasses: true }
);
});

test('Replace ancestors at different nesting levels', t => {
return run( t,
'.a{ &:hover{ ^&-b{} } .c{ .d{ ^&-e{} } } .z{} }',