Skip to content
This repository has been archived by the owner on Jul 31, 2019. It is now read-only.

Commit

Permalink
Merge pull request #5 from ygtzz/master
Browse files Browse the repository at this point in the history
add aDpr config option
  • Loading branch information
crossjs committed Mar 31, 2017
2 parents 40769ee + 73654bb commit 9c94aee
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions index.js
Expand Up @@ -22,6 +22,10 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
}
return prefix + ' ' + selector
}
var aDpr = options.aDpr || [3, 2, 1]
aDpr.sort(function(a, b){
return b - a
})

// get calculated value of px or rem
function getCalcValue (value, dpr) {
Expand All @@ -31,7 +35,6 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
val = parseFloat(val.toFixed(remPrecision)) // control decimal precision of the calculated value
return val == 0 ? val : val + type
}

return value.replace(valueGlobalRegExp, function ($0, $1, $2) {
if ($1 === 'url') {
if (dpr) {
Expand Down Expand Up @@ -72,11 +75,11 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {

var newRules = []
var hasDecls = false
for (var dpr = 3; dpr >= 1; dpr--) {

for (var i = 0;i < aDpr.length; i++) {
var newRule = postcss.rule({
selectors: rule.selectors.map(function (sel) {
return addPrefixToSelector(sel, '[data-dpr="' + dpr + '"]')
return addPrefixToSelector(sel, '[data-dpr="' + aDpr[i] + '"]')
}),
type: rule.type
})
Expand All @@ -93,7 +96,7 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
newRules.forEach(function (newRule, index) {
var newDecl = postcss.decl({
prop: decl.prop,
value: getCalcValue(decl.value, 3 - index)
value: getCalcValue(decl.value, aDpr[index])
})
newRule.append(newDecl)
})
Expand Down

0 comments on commit 9c94aee

Please sign in to comment.