postcss-nested
Advanced tools
Comparing version 0.2.2 to 0.3.0
82
index.js
@@ -1,28 +0,23 @@ | ||
var list = require('postcss/lib/list'); | ||
var postcss = require('postcss'); | ||
var selector = function (parent, node) { | ||
return list.comma(parent.selector) | ||
.map(function (i) { | ||
return list.comma(node.selector) | ||
.map(function (j) { | ||
if ( j.indexOf('&') == -1 ) { | ||
return i + ' ' + j; | ||
} else { | ||
return j.replace(/&/g, i); | ||
} | ||
}) | ||
.join(', '); | ||
}) | ||
.join(', '); | ||
return parent.selectors.map(function (i) { | ||
return node.selectors.map(function (j) { | ||
if ( j.indexOf('&') === -1 ) { | ||
return i + ' ' + j; | ||
} else { | ||
return j.replace(/&/g, i); | ||
} | ||
}).join(', '); | ||
}).join(', '); | ||
}; | ||
var atruleChilds = function (rule, atrule) { | ||
var clone; | ||
var decls = []; | ||
atrule.each(function (child) { | ||
if ( child.type == 'decl' ) { | ||
if ( child.type === 'decl' ) { | ||
decls.push( child ); | ||
} else if ( child.type == 'rule' ) { | ||
} else if ( child.type === 'rule' ) { | ||
child.selector = selector(rule, child); | ||
} else if ( child.type == 'atrule' ) { | ||
} else if ( child.type === 'atrule' ) { | ||
atruleChilds(rule, child); | ||
@@ -32,3 +27,3 @@ } | ||
if ( decls.length ) { | ||
clone = rule.clone({ nodes: [] }); | ||
var clone = rule.clone({ nodes: [] }); | ||
for ( var i = 0; i < decls.length; i++ ) decls[i].moveTo(clone); | ||
@@ -39,33 +34,42 @@ atrule.prepend(clone); | ||
var rule = function (rule) { | ||
var processRule = function (rule, bubble) { | ||
var unwrapped = false; | ||
var after = rule; | ||
rule.each(function (child) { | ||
if ( child.type == 'rule' ) { | ||
if ( child.type === 'rule' ) { | ||
unwrapped = true; | ||
child.selector = selector(rule, child); | ||
after = child.moveAfter(after); | ||
} else if ( child.type == 'atrule' ) { | ||
unwrapped = true; | ||
atruleChilds(rule, child); | ||
after = child.moveAfter(after); | ||
} else if ( child.type === 'atrule' ) { | ||
if ( bubble.indexOf(child.name) !== -1 ) { | ||
unwrapped = true; | ||
atruleChilds(rule, child); | ||
after = child.moveAfter(after); | ||
} | ||
} | ||
}); | ||
if ( unwrapped && rule.nodes.length === 0 ) rule.removeSelf(); | ||
if ( unwrapped ) { | ||
rule.semicolon = true; | ||
if ( rule.nodes.length === 0 ) rule.removeSelf(); | ||
} | ||
}; | ||
var process = function (node) { | ||
node.each(function (child) { | ||
if ( child.type == 'rule' ) { | ||
rule(child); | ||
} else if ( child.type == 'atrule' ) { | ||
process(child); | ||
} | ||
}); | ||
}; | ||
module.exports = postcss.plugin('postcss-nested', function (opts) { | ||
var bubble = ['media', 'support', 'document']; | ||
if ( opts && opts.bubble ) { | ||
bubble = bubble.concat(opts.bubble.map(function (i) { | ||
return i.replace(/^@/, ''); | ||
})); | ||
} | ||
module.exports = function () { | ||
var process = function (node) { | ||
node.each(function (child) { | ||
if ( child.type === 'rule' ) { | ||
processRule(child, bubble); | ||
} else if ( child.type === 'atrule' ) { | ||
process(child); | ||
} | ||
}); | ||
}; | ||
return process; | ||
}; | ||
module.exports.postcss = process; | ||
}); |
{ | ||
"name": "postcss-nested", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "PostCSS plugin to unwrap nested rules like how Sass does it.", | ||
@@ -12,13 +12,11 @@ "keywords": ["postcss", "sass", "css", "nested"], | ||
}, | ||
"peerDependencies": { | ||
"postcss": "^4.0.0" | ||
"dependencies": { | ||
"postcss": "^4.1.9" | ||
}, | ||
"devDependencies": { | ||
"jshint-stylish": "1.0.0", | ||
"gulp-jshint": "1.9.2", | ||
"gulp-mocha": "2.0.0", | ||
"postcss": "4.0.3", | ||
"mocha": "2.1.0", | ||
"chai": "1.10.0", | ||
"gulp": "3.8.10" | ||
"gulp-eslint": "0.11.1", | ||
"gulp-mocha": "2.0.1", | ||
"mocha": "2.2.4", | ||
"chai": "2.3.0", | ||
"gulp": "3.8.11" | ||
}, | ||
@@ -25,0 +23,0 @@ "scripts": { |
@@ -1,4 +0,6 @@ | ||
# PostCSS Nested [![Build Status](https://travis-ci.org/postcss/postcss-nested.svg)](https://travis-ci.org/postcss/postcss-nested) | ||
# PostCSS Nested [![Build Status][ci-img]][ci] | ||
<img align="right" width="135" height="95" src="http://postcss.github.io/postcss/logo-leftp.png" title="Philosopher’s stone, logo of PostCSS"> | ||
<img align="right" width="135" height="95" | ||
title="Philosopher’s stone, logo of PostCSS" | ||
src="http://postcss.github.io/postcss/logo-leftp.png"> | ||
@@ -48,2 +50,4 @@ [PostCSS] plugin to unwrap nested rules like how Sass does it. | ||
[PostCSS]: https://github.com/postcss/postcss | ||
[ci-img]: https://travis-ci.org/postcss/postcss-nested.svg | ||
[ci]: https://travis-ci.org/postcss/postcss-nested | ||
@@ -57,1 +61,12 @@ ## Usage | ||
See [PostCSS] docs for examples for your environment. | ||
## Options | ||
### `bubble` | ||
By default, plugin will unwrap only `@media`, `@support` and `@document` | ||
at-rules. You can add your custom at-rules to this list by `bubble` option: | ||
```js | ||
postcss([ require('postcss-nested')({ bubble: ['phone'] }) ] | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6464
5
7
68
71
+ Addedpostcss@^4.1.9