postcss-scopify
Advanced tools
Comparing version 0.1.7 to 0.1.8
81
index.js
'use strict'; | ||
var postcss = require('postcss'); | ||
var conditionalGroupRules = ['media','supports','document']; | ||
module.exports = postcss.plugin('postcss-scopify', scopify); | ||
function scopify(scope, options) { | ||
options = options || {}; | ||
return function(root) { | ||
options = options || {}; | ||
// guard statment- allow only valid scopes | ||
if(!isValidScope(scope)){ | ||
throw root.error('invalid scope', { plugin: 'postcss-scopify' }); | ||
} | ||
root.walkRules(function (rule) { | ||
if (!rule.selectors){ | ||
return rule; | ||
} | ||
return function(root) { | ||
if(rule.parent.type === 'atrule' && (rule.parent.name === '-webkit-keyframes' || rule.parent.name === 'keyframes')){ | ||
return rule; | ||
}; | ||
rule.selectors = rule.selectors.map(function(selector) { | ||
if (isScopeApplied(selector,scope)) { | ||
return selector; | ||
// guard statment- allow only valid scopes | ||
if(!isValidScope(scope)){ | ||
throw root.error('invalid scope', { plugin: 'postcss-scopify' }); | ||
} | ||
return scope + ' ' + selector; | ||
root.walkRules(function (rule) { | ||
}); | ||
}); | ||
}; | ||
// skip scoping of special rules (certain At-rules, nested, etc') | ||
if(!isRuleScopable(rule)){ | ||
return rule; | ||
} | ||
rule.selectors = rule.selectors.map(function(selector) { | ||
if (isScopeApplied(selector,scope)) { | ||
return selector; | ||
} | ||
// special case for a top level '&' selector, resolves to scope | ||
if (selector === '&') { | ||
return scope; | ||
} | ||
return scope + ' ' + selector; | ||
}); | ||
}); | ||
}; | ||
} | ||
@@ -45,4 +50,4 @@ | ||
function isScopeApplied(selector,scope) { | ||
var selectorTopScope = selector.split(" ",1)[0]; | ||
return selectorTopScope === scope; | ||
var selectorTopScope = selector.split(" ",1)[0]; | ||
return selectorTopScope === scope; | ||
} | ||
@@ -64,1 +69,27 @@ | ||
} | ||
/** | ||
* Determine if rule should be scoped | ||
* | ||
* @param {rule} rule | ||
*/ | ||
function isRuleScopable(rule){ | ||
if (!rule.selectors){ | ||
return false; | ||
} | ||
else if(rule.parent.type !== 'root') { | ||
if (rule.parent.type === 'atrule' && conditionalGroupRules.indexOf(rule.parent.name) > -1){ | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
else { | ||
return true; | ||
} | ||
} |
{ | ||
"name": "postcss-scopify", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "PostCSS plugin that adds a user input scope to each selector", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -27,2 +27,6 @@ # PostCSS Scopify [![Build Status][ci-img]][ci] | ||
} | ||
& { | ||
/* declarations */ | ||
} | ||
``` | ||
@@ -35,2 +39,6 @@ __Example output__ | ||
} | ||
#scope { | ||
/* declarations */ | ||
} | ||
``` | ||
@@ -64,2 +72,5 @@ | ||
## Change Log | ||
### v0.1.8 | ||
closes [#10](https://github.com/pazams/postcss-scopify/issues/10) | ||
### v0.1.7 | ||
@@ -66,0 +77,0 @@ fixes [#7](https://github.com/pazams/postcss-scopify/issues/7) |
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
6662
8
73
81