postcss-composition
Advanced tools
Comparing version 0.1.0 to 0.1.1
64
index.js
var postcss = require('postcss'); | ||
const isCompose = n => n.type === 'atrule' && n.name === 'compose'; | ||
const notCompose = n => n.type !== 'atrule' || !isCompose(n); | ||
const trim = _ => _.trim(); | ||
const nonEmpty = _ => _ && _ !== ''; | ||
const isCompose = _ => _.type === 'atrule' && _.name === 'compose'; | ||
const notCompose = _ => _.type !== 'atrule' || !isCompose(_); | ||
const isPseudo = _ => _ && _.pseudo; | ||
const distinctCombo = (rules, newRules) => rules | ||
.filter(rule => { | ||
const r = newRules.filter( | ||
newRule => newRule.prop && newRule.prop == rule.prop | ||
); | ||
return !r || r.length === 0; | ||
}) | ||
.concat(newRules); | ||
function fmtSelector(selector) { | ||
return { selector }; | ||
} | ||
function pseudo(ref) { | ||
const p = ref.selector | ||
.split(/:(.+)/); | ||
return Object.assign({}, ref, { | ||
selector: p[0], | ||
pseudo: p[1] && p[1] ? ':' + p[1] : null | ||
}); | ||
} | ||
function getSelectors(selectorString) { | ||
return selectorString | ||
.split(',') | ||
.map(trim) | ||
.map(fmtSelector) | ||
.map(pseudo); | ||
} | ||
function eqSelector(selectors, c) { | ||
let i = selectors.map(_ => _.selector).indexOf(c); | ||
return i > -1 ? selectors[i] : null; | ||
} | ||
function distinctCombo(rules, newRules) { | ||
return rules | ||
.filter(rule => { | ||
const r = newRules.filter( | ||
newRule => newRule.prop && newRule.prop == rule.prop | ||
); | ||
return !r || r.length === 0; | ||
}) | ||
.concat(newRules); | ||
} | ||
module.exports = postcss.plugin('postcss-composition', function (opts) { | ||
@@ -29,4 +61,4 @@ opts = opts || {}; | ||
.split(/,|\r?\n|\r/) | ||
.map(_ => _.trim()) | ||
.filter(_ => _ && _ !== ''); | ||
.map(trim) | ||
.filter(nonEmpty); | ||
@@ -37,3 +69,9 @@ let newNodes = []; | ||
root.walkRules(r => { | ||
if ( r.selector === c ) { | ||
const selectors = getSelectors(r.selector); | ||
let s = eqSelector(selectors, c); | ||
if ( isPseudo(s) ) { | ||
const clone = r.clone(); | ||
clone.selector = rule.selector + s.pseudo; | ||
root.insertAfter(rule, clone); | ||
} else if ( s ) { | ||
newNodes = newNodes.concat(r.nodes); | ||
@@ -40,0 +78,0 @@ } |
{ | ||
"name": "postcss-composition", | ||
"version": "0.1.0", | ||
"description": "PostCSS plugin Compose multiple CSS selectors together", | ||
"version": "0.1.1", | ||
"description": "PostCSS plugin Compose multiple CSS selectors together.", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "postcss", |
# PostCSS Composition [![Build Status][ci-img]][ci] | ||
[PostCSS] plugin Compose multiple CSS selectors together. | ||
[PostCSS] PostCSS plugin to compose multiple CSS selectors together like CSS modules. | ||
Primarily meant to be used with Tachyons or other atomic CSS. Currently useable, but | ||
still in heavy development. | ||
@@ -5,0 +7,0 @@ [PostCSS]: https://github.com/postcss/postcss |
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
90181
71
30