postcss-modules-local-by-default
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -9,2 +9,6 @@ # Change Log | ||
## [0.0.7] - 2015-05-30 | ||
### Changed | ||
- Migrated to `css-selector-tokenizer`. | ||
## [0.0.6] - 2015-05-28 | ||
@@ -38,3 +42,3 @@ ### Changed | ||
[unreleased]: https://github.com/postcss-modules-local-by-default/compare/v0.0.6...HEAD | ||
[unreleased]: https://github.com/postcss-modules-local-by-default/compare/v0.0.7...HEAD | ||
[0.0.2]: https://github.com/postcss-modules-local-by-default/compare/v0.0.1...v0.0.2 | ||
@@ -45,1 +49,2 @@ [0.0.3]: https://github.com/postcss-modules-local-by-default/compare/v0.0.2...v0.0.3 | ||
[0.0.6]: https://github.com/postcss-modules-local-by-default/compare/v0.0.5...v0.0.6 | ||
[0.0.7]: https://github.com/postcss-modules-local-by-default/compare/v0.0.6...v0.0.7 |
74
index.js
var postcss = require('postcss'); | ||
var Tokenizer = require('css-selector-tokenizer'); | ||
var ESCAPED_DOT = ' ___LOCAL_SCOPE__ESCAPED_DOT___ '; | ||
function localizeNodes(nodes) { | ||
var isGlobalContext = false; | ||
module.exports = postcss.plugin('postcss-modules-local-by-default', function (config) { | ||
var options = config || {}; | ||
return function(css, result) { | ||
css.eachRule(function(rule) { | ||
rule.selector = rule.selector | ||
.split(',') | ||
.map(transformSelector.bind(null, options, rule)) | ||
.join(','); | ||
}); | ||
}; | ||
}); | ||
return nodes | ||
.map(function(node, i) { | ||
var newNode = node; | ||
function transformSelector(options, rule, selector) { | ||
var trimmedSelector = selector.trim(); | ||
if (isGlobal(newNode)) { | ||
isGlobalContext = true; | ||
return null; | ||
} | ||
if (options.lint) { | ||
if (!/^\./.test(trimmedSelector) && !/^\:local/.test(trimmedSelector)) { | ||
if (!/^\:global/.test(trimmedSelector)) { | ||
throw rule.error('Global selector detected in local context. Does this selector really need to be global? If so, you need to explicitly export it into the global scope with ":global", e.g. ":global '+trimmedSelector+'"', { plugin: 'postcss-modules-local-by-default' }); | ||
if (newNode.type === 'spacing' && isGlobal(nodes[i-1])) { | ||
return null; | ||
} | ||
} | ||
} | ||
return selector | ||
.replace(/\:global\((.*?)\)/g, escapeDots) | ||
.replace(/\:global (.*)/g, escapeDots) | ||
.replace(/(\:extends\((.*?)\))/g, escapeDots) | ||
.replace(/\.local\[(-?[_a-zA-Z]+[_a-zA-Z0-9-]*)\]/g, '.$1') // source: http://stackoverflow.com/questions/448981/what-characters-are-valid-in-css-class-names-selectors | ||
.replace(/\:local\(\.(-?[_a-zA-Z]+[_a-zA-Z0-9-]*)\)/g, '.$1') | ||
.replace(/\.(-?[_a-zA-Z]+[_a-zA-Z0-9-]*)/g, ':local(.$1)') | ||
.replace(new RegExp(ESCAPED_DOT, 'g'), '.'); | ||
if (!isGlobalContext && node.type === 'class') { | ||
newNode = { type: 'nested-pseudo-class', name: 'local', nodes: [node] } | ||
} else if (isNestedGlobal(newNode)) { | ||
newNode = node.nodes[0]; | ||
} else if (!isNestedLocal(newNode) && newNode.nodes) { | ||
newNode.nodes = localizeNodes(newNode.nodes); | ||
} | ||
return newNode; | ||
}).filter(function(node) { | ||
return node !== null | ||
}); | ||
} | ||
function escapeDots(match, p1) { | ||
return p1.replace(/\./g, ESCAPED_DOT); | ||
function isGlobal(node) { | ||
return node.type === 'pseudo-class' && node.name === 'global'; | ||
} | ||
function isNestedGlobal(node) { | ||
return node.type === 'nested-pseudo-class' && node.name === 'global'; | ||
} | ||
function isNestedLocal(node) { | ||
return node.type === 'nested-pseudo-class' && node.name === 'local'; | ||
} | ||
module.exports = postcss.plugin('postcss-modules-local-by-default', function () { | ||
return function(css, result) { | ||
css.eachRule(function(rule) { | ||
var selector = Tokenizer.parse(rule.selector); | ||
selector.nodes = localizeNodes(selector.nodes); | ||
rule.selector = Tokenizer.stringify(selector).trim(); | ||
}); | ||
}; | ||
}); |
{ | ||
"name": "postcss-modules-local-by-default", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "A CSS Modules transform to make local scope the default", | ||
@@ -18,2 +18,3 @@ "keywords": [ | ||
"dependencies": { | ||
"css-selector-tokenizer": "^0.3.1", | ||
"postcss": "^4.1.5" | ||
@@ -20,0 +21,0 @@ }, |
@@ -5,3 +5,3 @@ [![Build Status][ci-img]][ci] [![npm][npm-img]][npm] | ||
Transforms: | ||
Transformation examples: | ||
@@ -28,2 +28,3 @@ ```css | ||
.foo:global(.bar) .baz { ... } /* => */ :local(.foo).bar :local(.baz) { ... } | ||
``` | ||
@@ -30,0 +31,0 @@ ## Development |
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
6508
44
49
2
+ Addedcss-selector-tokenizer@0.3.1(transitive)
+ Addedfastparse@1.1.2(transitive)