11var postcss = require ( 'postcss' ) ,
2- colorName = Object . keys ( require ( 'color-name' ) ) ;
2+ colorNameList = Object . keys ( require ( 'color-name' ) ) ;
33
44module . exports = postcss . plugin ( 'postcss-extract-value' , function ( opts ) {
55 opts = opts || { } ;
66
77 // Cache RegExp
8- var reColorKeywords = new RegExp ( colorName . join ( '|' ) ) ;
9- var reCheck = new RegExp ( / # \w + | r g b a ? | h s l a ? / . source + '|' + reColorKeywords . source , 'g' ) ;
8+ var reColorKeywords = new RegExp ( colorNameList . join ( '|' ) ) ;
9+ var reCheck = new RegExp ( / # \w + | r g b a ? | h s l a ? / . source +
10+ '|' + reColorKeywords . source , 'g' ) ;
1011 var reCSSVariable = / v a r \( - { 2 } \w { 1 } [ \w + - ] * / ;
1112 var reHex = / # ( \w { 6 } | \w { 3 } ) / ;
1213 var reRgb = / r g b a ? \( [ \d , . \s ] + \) / ;
@@ -17,6 +18,7 @@ module.exports = postcss.plugin('postcss-extract-value', function (opts) {
1718 // Options
1819 var filterByProps = opts . filterByProps ;
1920 var onlyColor = opts . onlyColor ;
21+ var scope = opts . scope || ':root' ;
2022
2123 function checkColor ( value ) {
2224 return reCheck . test ( value ) ;
@@ -40,8 +42,8 @@ module.exports = postcss.plugin('postcss-extract-value', function (opts) {
4042 return '--' + prop + '-' + num ;
4143 }
4244
43- function addCSSVariable ( root , prop , value , num ) {
44- root . append ( makeCSSVariable ( prop , ++ num ) + ': ' + value ) ;
45+ function addCSSVariable ( currentScope , prop , value , num ) {
46+ currentScope . append ( makeCSSVariable ( prop , ++ num ) + ': ' + value ) ;
4547 }
4648
4749 function extractValue ( decl , storePropsLink , valueFiltered ) {
@@ -63,7 +65,7 @@ module.exports = postcss.plugin('postcss-extract-value', function (opts) {
6365
6466 css . walkRules ( function ( rule ) {
6567
66- if ( rule . selector === ':root' ) {
68+ if ( rule . selector === scope ) {
6769 rootSel = rule ;
6870 } else {
6971 rule . walkDecls ( function ( decl ) {
@@ -103,7 +105,7 @@ module.exports = postcss.plugin('postcss-extract-value', function (opts) {
103105 } ) ;
104106
105107 if ( ! rootSel ) {
106- rootSel = postcss . rule ( { selector : ':root' } ) ;
108+ rootSel = postcss . rule ( { selector : scope } ) ;
107109 root . prepend ( rootSel ) ;
108110 }
109111 for ( var prop in storeProps ) {
0 commit comments