Skip to content

Commit

Permalink
修复 padding, margin, 四个参数转换bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chengpan168 committed Dec 18, 2017
1 parent 7a2488d commit 5fcc40b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const postcss = require('postcss')

module.exports = postcss.plugin('wx-px2rpx', (opts = {}) => {
const { proportion = 1 } = opts

const pxRegExp = /\b(\d+(\.\d+)?)px\b/

const {proportion = 1} = opts
const pxRegExp = /\b(\d+(\.\d+)?)px\b/g
return (root) => {
root.replaceValues(pxRegExp, { fast: 'px' }, string => `${(proportion * Number(string.replace(/px$/, '')))}rpx`);
root.replaceValues(pxRegExp, {fast: 'px'}, string => {
return `${proportion * parseInt(string)}rpx`

This comment has been minimized.

Copy link
@jinker

jinker Dec 26, 2017

Contributor

使用parseInt, 觉得要考虑下浮点数的情况

This comment has been minimized.

Copy link
@chengpan168

chengpan168 via email Dec 27, 2017

Author Owner
})
}
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wx-px2rpx",
"version": "1.2.0",
"version": "1.2.2",
"description": "PostCSS mini program px to rpx",
"main": "index.js",
"keywords": [
Expand Down
7 changes: 7 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ const test = (input, output, opts, done) => {
}

describe('wx-px2rpx', () => {
it('replaces pixel values', (done) => {
test('a{padding: 200px 10px;}', 'a{padding: 200rpx 10rpx;}', {}, done)
})
it('replaces pixel values', (done) => {
test('a{padding: 200px 10px 10px 10px;}', 'a{padding: 200rpx 10rpx 10rpx 10rpx;}', {}, done)
})

it('replaces pixel values', (done) => {
test('a{width: 200px;}', 'a{width: 200rpx;}', {}, done)
})
Expand Down

0 comments on commit 5fcc40b

Please sign in to comment.