postcss-nested-ancestors
Advanced tools
Comparing version 0.0.0 to 0.0.1
49
index.js
var postcss = require('postcss'), | ||
assign = require('object-assign'), | ||
escpRegex = require('escape-string-regexp'); | ||
escRgx = require('escape-string-regexp'); | ||
@@ -12,4 +12,4 @@ module.exports = postcss.plugin('postcss-nested-ancestors', function (opts) { | ||
opts = assign({ | ||
levelSymbol: opts.placeholder.charAt(0), /* default "^" */ | ||
parentSymbol: opts.placeholder.charAt(1) /* default "&" */ | ||
levelSymbol: opts.levelSymbol || opts.placeholder.charAt(0), | ||
parentSymbol: opts.parentSymbol || opts.placeholder.charAt(1) | ||
}, opts); | ||
@@ -19,22 +19,17 @@ | ||
placeholderRegex = new RegExp( | ||
/* | ||
/** | ||
* Get all ancestors placeholder recurrencies: | ||
* ^&, ^^&, ^^^&, [...] | ||
*/ | ||
escpRegex(opts.levelSymbol) + '+' + escpRegex(opts.parentSymbol), | ||
// eslint-disable-next-line max-len | ||
'(' + escRgx(opts.levelSymbol) + ')+(' + escRgx(opts.parentSymbol) + ')', | ||
'g' | ||
); | ||
/* | ||
* getParentSelectorAtLevel() | ||
/** | ||
* Walk current ancestor stack and | ||
* assembly ancestor selector at the given depth. | ||
* | ||
* Assembly current ancestor selector. | ||
* | ||
* nestingLevel param allows to select | ||
* an higher element up into ancestors hierarchy | ||
* | ||
* eg. | ||
* nestingLevel = 0 is direct parent (same of "&") | ||
* nestingLevel = 1 is grandparent | ||
* ... | ||
* @param {Number} ancestor nesting depth ( 0 = &, 1 = grandparent, ...) | ||
* @return {String} ancestor selector | ||
*/ | ||
@@ -53,5 +48,13 @@ function getParentSelectorAtLevel(nestingLevel) { | ||
/** | ||
* Given an ancestor placeholder, | ||
* returns the corresponding selector fragment. | ||
* Used as replacer in .replace method | ||
* | ||
* @param {String} placeholder eg.^^& | ||
* @return {String} string ancestor selector fragment | ||
*/ | ||
function placeholderReplacer(placeholder) { | ||
return getParentSelectorAtLevel( | ||
// Get how many levels ("^") has current placeholder | ||
// Get how many level symbols ("^") has current placeholder | ||
placeholder.split(opts.levelSymbol).length - 1 | ||
@@ -61,2 +64,8 @@ ); | ||
/** | ||
* Replace any ancestor placeholder into a given selector. | ||
* | ||
* @param {String} selector | ||
* @return {String} selector | ||
*/ | ||
function replacePlaceholders(selector) { | ||
@@ -80,4 +89,6 @@ // Find placeholders and replace them with matching parent selector | ||
// Replace parents placeholders in rule selctor | ||
// Replace parents placeholders in rule selector | ||
rule.selectors = rule.selectors.map(replacePlaceholders); | ||
// Process child rules | ||
process(rule); | ||
@@ -92,3 +103,1 @@ } | ||
}); | ||
{ | ||
"name": "postcss-nested-ancestors", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "PostCSS plugin to reference any ancestor selector in nested CSS", | ||
@@ -32,3 +32,6 @@ "keywords": [ | ||
"scripts": { | ||
"test": "ava && eslint *.js" | ||
"test": "ava && eslint *.js", | ||
"preversion": "npm test", | ||
"version": "git add package.json", | ||
"postversion": "git push && git push --tags" | ||
}, | ||
@@ -35,0 +38,0 @@ "eslintConfig": { |
@@ -5,13 +5,10 @@ # PostCSS Nested ancestors [![Build Status][ci-img]][ci] | ||
[PostCSS]: https://github.com/postcss/postcss | ||
[ci-img]: https://travis-ci.org/toomuchdesign/postcss-nested-ancestors.svg | ||
[ci]: https://travis-ci.org/toomuchdesign/postcss-nested-ancestors | ||
[PostCSS]: https://github.com/postcss/postcss | ||
[ci-img]: https://travis-ci.org/toomuchdesign/postcss-nested-ancestors.svg | ||
[ci]: https://travis-ci.org/toomuchdesign/postcss-nested-ancestors | ||
[postcss-current-selector]: https://github.com/komlev/postcss-current-selector | ||
[postcss-nested]: https://github.com/postcss/postcss-nested | ||
[postcss-simple-vars]: https://github.com/postcss/postcss-simple-vars | ||
## Installation | ||
```console | ||
$ npm install postcss-nested-ancestors | ||
``` | ||
## Usage | ||
## Getting ancestor selectors | ||
When writing modular nested CSS, `&` current parent selector is often not enough. | ||
@@ -21,7 +18,7 @@ | ||
This plugin should be used **before** a POSTCSS rules unwrapper like [postcss-nested](https://github.com/postcss/postcss-nested). | ||
This plugin should be used **before** a POSTCSS rules unwrapper like [postcss-nested]. | ||
See [PostCSS] docs for examples for your environment. | ||
### Ancestor selector schema | ||
### Ancestor selectors schema | ||
@@ -84,2 +81,14 @@ ``` | ||
## Installation | ||
```console | ||
$ npm install postcss-nested-ancestors | ||
``` | ||
## Usage | ||
```js | ||
postcss([ require('postcss-nested-ancestors') ]); | ||
``` | ||
## Options | ||
@@ -107,1 +116,4 @@ | ||
Ancestor selector base symbol | ||
## Todo's | ||
- Add warning when nestingLevel >= parentsStack.length |
8837
83
116