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

Commit 9bb2748

Browse files
author
crossjs
committed
update tests
1 parent e07f942 commit 9bb2748

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var postcss = require('postcss')
44

55
var valueRegExp = /(dpr|rem|url)\((.+?)(px)?\)/
66
var dprRegExp = /dpr\((\d+(?:\.\d+)?)px\)/
7-
var urlRegExp = /url\(['"]?\S+?@[1-3]x\S+?['"]?\)/
87

98
module.exports = postcss.plugin('postcss-flexible', function (options) {
109
if (!options) {
@@ -23,6 +22,7 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
2322
return prefix + ' ' + selector
2423
}
2524
var dprList = (options.dprList || [3, 2, 1]).sort().reverse()
25+
var urlRegExp = new RegExp('url\\([\'"]?\\S+?@(' + dprList.join('|') + ')x\\S+?[\'"]?\\)')
2626

2727
// get calculated value of px or rem
2828
function getCalcValue (value, dpr) {
@@ -35,7 +35,7 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
3535
return value.replace(valueGlobalRegExp, function ($0, $1, $2) {
3636
if ($1 === 'url') {
3737
if (dpr) {
38-
return 'url(' + $2.replace(/@[1-3]x/g, '@' + dpr + 'x') + ')'
38+
return 'url(' + $2.replace(new RegExp('@(' + dprList.join('|') + ')x', 'g'), '@' + dpr + 'x') + ')'
3939
}
4040
} else if ($1 === 'dpr') {
4141
if (dpr) {
@@ -55,7 +55,7 @@ module.exports = postcss.plugin('postcss-flexible', function (options) {
5555
decl.value = '0'
5656
} else {
5757
if (dprRegExp.test(decl.value) || urlRegExp.test(decl.value)) {
58-
decl.value = getCalcValue(decl.value, 2)
58+
decl.value = getCalcValue(decl.value, baseDpr)
5959
} else {
6060
// only has rem()
6161
decl.value = getCalcValue(decl.value)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "flexible transformer for flexible",
55
"main": "index.js",
66
"scripts": {
7-
"test": "istanbul cover _mocha"
7+
"test": "istanbul cover _mocha",
8+
"test-win": "istanbul cover node_modules/mocha/bin/_mocha"
89
},
910
"repository": {
1011
"type": "git",

test/flexible.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,16 @@ describe('postcss-flexible', function() {
4646
assert.equal(outputText.trim(), expectedText.trim())
4747
})
4848

49+
it('should output right css text with dprList', function() {
50+
var srcPath = path.join(__dirname, 'source.css')
51+
var srcText = fs.readFileSync(srcPath, {
52+
encoding: 'utf8'
53+
})
54+
var outputText = postcss().use(flexible({ dprList: [4, 2] })).process(srcText).css
55+
var expectedText = fs.readFileSync(path.join(__dirname, 'output-custom-2.css'), {
56+
encoding: 'utf8'
57+
})
58+
assert.equal(outputText.trim(), expectedText.trim())
59+
})
60+
4961
})

test/output-custom-2.css

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
.selector,
2+
.selector2 {
3+
width: 1rem;
4+
line-height: 3;
5+
}
6+
7+
[data-dpr="2"] .selector, [data-dpr="2"] .selector2 {
8+
font-size: 32px;
9+
background-image: url(/images/qr@2x.png);
10+
}
11+
12+
[data-dpr="4"] .selector, [data-dpr="4"] .selector2 {
13+
font-size: 64px;
14+
background-image: url(/images/qr@4x.png);
15+
}
16+
17+
[data-dpr="2"] .selector3 {
18+
padding: 20px;
19+
}
20+
21+
[data-dpr="4"] .selector3 {
22+
padding: 40px;
23+
}
24+
25+
[data-dpr="2"] .selector4 {
26+
background: url(/images/qr@2x.png) 20px 0.266667rem;
27+
}
28+
29+
[data-dpr="4"] .selector4 {
30+
background: url(/images/qr@4x.png) 40px 0.266667rem;
31+
}
32+
33+
@media screen and (min-width: 480px) {
34+
[data-dpr="2"] body {
35+
margin: 20px;
36+
}
37+
[data-dpr="4"] body {
38+
margin: 40px;
39+
}
40+
}
41+
42+
@keyframes c-spinner-snake {
43+
0% {
44+
transform: rotate(0deg);
45+
}
46+
47+
to {
48+
transform: rotate(1turn);
49+
}
50+
}
51+
52+
html[data-dpr="2"][dir="rtl"] body {
53+
padding: 8px;
54+
}
55+
56+
html[data-dpr="4"][dir="rtl"] body {
57+
padding: 16px;
58+
}

0 commit comments

Comments
 (0)