This repository was archived by the owner on Nov 2, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +70
-0
lines changed Expand file tree Collapse file tree 3 files changed +70
-0
lines changed Original file line number Diff line number Diff line change
1
+ const postcss = require ( 'postcss' )
2
+ const crypto = require ( 'crypto' )
3
+
4
+ const declPatterns = [
5
+ / ( m a r g i n | p a d d i n g ) - ( l e f t | r i g h t | t o p | b o t t o m ) / ,
6
+ / f o n t - s i z e / ,
7
+ ]
8
+
9
+ function updateDecl ( decl ) {
10
+ const hash = parseInt ( crypto . createHash ( 'sha1' ) . update ( JSON . stringify ( decl ) ) . digest ( 'hex' ) , 16 )
11
+ const matches = / ^ ( \d + ) ( [ a - z ] + ) $ / i. exec ( decl . value )
12
+
13
+ if ( ! matches ) return
14
+
15
+ const diff = hash % 5
16
+
17
+ decl . replaceWith ( decl . clone ( {
18
+ value : `${ Number ( matches [ 1 ] ) + diff } ${ matches [ 2 ] } `
19
+ } ) )
20
+ }
21
+
22
+ module . exports = postcss . plugin ( 'postcss-pixel-imperfect' , ( opts ) => ( css ) => {
23
+ declPatterns . forEach ( ( declPattern ) => {
24
+ css . walkDecls ( declPattern , updateDecl )
25
+ } )
26
+ } )
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " postcss-pixel-imperfect" ,
3
+ "version" : " 1.0.0" ,
4
+ "description" : " " ,
5
+ "main" : " index.js" ,
6
+ "scripts" : {
7
+ "test" : " node test.js"
8
+ },
9
+ "author" : " Viacheslav Slinko <vslinko@yahoo.com>" ,
10
+ "license" : " MIT" ,
11
+ "devDependencies" : {
12
+ "postcss" : " ^5.0.21" ,
13
+ "tape" : " ^4.6.0"
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ const test = require ( 'tape' )
2
+ const postcss = require ( 'postcss' )
3
+ const plugin = require ( './index' )
4
+
5
+ function run ( t , input , output , opts = { } ) {
6
+ return postcss ( [ plugin ( opts ) ] )
7
+ . process ( input )
8
+ . then ( result => {
9
+ t . deepEqual ( result . css , output )
10
+ t . deepEqual ( result . warnings ( ) . length , 0 )
11
+ t . end ( )
12
+ } )
13
+ }
14
+
15
+ test ( 'some' , ( t ) => {
16
+ run ( t , `
17
+ .header {
18
+ font-size: 1pt;
19
+ margin-top: 10px;
20
+ margin-bottom: 10px;
21
+ }
22
+ ` , `
23
+ .header {
24
+ font-size: 4pt;
25
+ margin-top: 10px;
26
+ margin-bottom: 13px;
27
+ }
28
+ ` )
29
+ } )
You can’t perform that action at this time.
0 commit comments