postcss-nested
Advanced tools
Comparing version
@@ -0,1 +1,6 @@ | ||
## 1.0 | ||
* Use PostCSS 5.0 API. | ||
* Do not add spaces to selector in compressed CSS. | ||
* Move nodes with its comment above. | ||
## 0.3.2 | ||
@@ -2,0 +7,0 @@ * Fix `@supports` at-rule support (by Ben Briggs). |
56
index.js
var postcss = require('postcss'); | ||
var selector = function (parent, node) { | ||
return parent.selectors.map(function (i) { | ||
return node.selectors.map(function (j) { | ||
function selectors(parent, node) { | ||
var result = []; | ||
parent.selectors.forEach(function (i) { | ||
node.selectors.forEach(function (j) { | ||
if ( j.indexOf('&') === -1 ) { | ||
return i + ' ' + j; | ||
result.push(i + ' ' + j); | ||
} else { | ||
return j.replace(/&/g, i); | ||
result.push(j.replace(/&/g, i)); | ||
} | ||
}).join(', '); | ||
}).join(', '); | ||
}; | ||
}); | ||
}); | ||
return result; | ||
} | ||
var atruleChilds = function (rule, atrule) { | ||
var decls = []; | ||
function pickComment(comment, after) { | ||
if ( comment && comment.type === 'comment' ) { | ||
return comment.moveAfter(after); | ||
} else { | ||
return after; | ||
} | ||
} | ||
function atruleChilds(rule, atrule) { | ||
var children = []; | ||
atrule.each(function (child) { | ||
if ( child.type === 'decl' ) { | ||
decls.push( child ); | ||
if ( child.type === 'comment' ) { | ||
children.push( child ); | ||
} if ( child.type === 'decl' ) { | ||
children.push( child ); | ||
} else if ( child.type === 'rule' ) { | ||
child.selector = selector(rule, child); | ||
child.selectors = selectors(rule, child); | ||
} else if ( child.type === 'atrule' ) { | ||
@@ -26,10 +38,10 @@ atruleChilds(rule, child); | ||
}); | ||
if ( decls.length ) { | ||
if ( children.length ) { | ||
var clone = rule.clone({ nodes: [] }); | ||
for ( var i = 0; i < decls.length; i++ ) decls[i].moveTo(clone); | ||
for ( var i = 0; i < children.length; i++ ) children[i].moveTo(clone); | ||
atrule.prepend(clone); | ||
} | ||
}; | ||
} | ||
var processRule = function (rule, bubble) { | ||
function processRule(rule, bubble) { | ||
var unwrapped = false; | ||
@@ -40,3 +52,4 @@ var after = rule; | ||
unwrapped = true; | ||
child.selector = selector(rule, child); | ||
child.selectors = selectors(rule, child); | ||
after = pickComment(child.prev(), after); | ||
after = child.moveAfter(after); | ||
@@ -47,2 +60,3 @@ } else if ( child.type === 'atrule' ) { | ||
atruleChilds(rule, child); | ||
after = pickComment(child.prev(), after); | ||
after = child.moveAfter(after); | ||
@@ -53,6 +67,6 @@ } | ||
if ( unwrapped ) { | ||
rule.semicolon = true; | ||
if ( rule.nodes.length === 0 ) rule.removeSelf(); | ||
rule.raws.semicolon = true; | ||
if ( rule.nodes.length === 0 ) rule.remove(); | ||
} | ||
}; | ||
} | ||
@@ -59,0 +73,0 @@ module.exports = postcss.plugin('postcss-nested', function (opts) { |
{ | ||
"name": "postcss-nested", | ||
"version": "0.3.2", | ||
"description": "PostCSS plugin to unwrap nested rules like how Sass does it.", | ||
"keywords": ["postcss", "css", "postcss-plugin", "sass", "nested"], | ||
"author": "Andrey Sitnik <andrey@sitnik.ru>", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/postcss/postcss-nested.git" | ||
}, | ||
"dependencies": { | ||
"postcss": "^4.1.11" | ||
}, | ||
"devDependencies": { | ||
"gulp-eslint": "0.12.0", | ||
"gulp-mocha": "2.1.0", | ||
"mocha": "2.2.5", | ||
"chai": "2.3.0", | ||
"gulp": "3.8.11" | ||
}, | ||
"scripts": { | ||
"test": "gulp" | ||
} | ||
"name": "postcss-nested", | ||
"version": "1.0.0", | ||
"description": "PostCSS plugin to unwrap nested rules like how Sass does it.", | ||
"keywords": [ | ||
"postcss", | ||
"css", | ||
"postcss-plugin", | ||
"sass", | ||
"nested" | ||
], | ||
"author": "Andrey Sitnik <andrey@sitnik.ru>", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/postcss/postcss-nested.git" | ||
}, | ||
"dependencies": { | ||
"postcss": "^5.0.2" | ||
}, | ||
"devDependencies": { | ||
"gulp-eslint": "1.0.0", | ||
"gulp-mocha": "2.1.3", | ||
"mocha": "2.2.5", | ||
"chai": "3.2.0", | ||
"gulp": "3.9.0" | ||
}, | ||
"scripts": { | ||
"test": "gulp" | ||
} | ||
} |
@@ -45,2 +45,6 @@ # PostCSS Nested [![Build Status][ci-img]][ci] | ||
See also [postcss-nesting] with tabakins proposal syntax with extra `{`. | ||
There is also [postcss-nested-props] for nested properties like `font-size`. | ||
<a href="https://evilmartians.com/?utm_source=postcss-nested"> | ||
@@ -50,5 +54,7 @@ <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"> | ||
[PostCSS]: https://github.com/postcss/postcss | ||
[ci-img]: https://travis-ci.org/postcss/postcss-nested.svg | ||
[ci]: https://travis-ci.org/postcss/postcss-nested | ||
[postcss-nested-props]: https://github.com/jedmao/postcss-nested-props | ||
[postcss-nesting]: https://github.com/jonathantneal/postcss-nesting | ||
[PostCSS]: https://github.com/postcss/postcss | ||
[ci-img]: https://travis-ci.org/postcss/postcss-nested.svg | ||
[ci]: https://travis-ci.org/postcss/postcss-nested | ||
@@ -55,0 +61,0 @@ ## Usage |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11720
77.85%81
19.12%1
-50%77
8.45%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
Updated