-
Notifications
You must be signed in to change notification settings - Fork 29
/
r2.js
214 lines (181 loc) · 5.58 KB
/
r2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*!
* R2 - a CSS LTR ∞ RTL converter
* Copyright Dustin Diaz 2012
* https://github.com/ded/r2
* License MIT
*/
var fs = require('fs')
, css = require('css');
function quad(v, m) {
// 1px 2px 3px 4px => 1px 4px 3px 2px
if ((m = v.trim().split(/\s+/)) && m.length == 4) {
return [m[0], m[3], m[2], m[1]].join(' ')
}
return v
}
function quad_radius(v) {
var m = v.trim().split(/\s+/)
// 1px 2px 3px 4px => 1px 2px 4px 3px
// since border-radius: top-left top-right bottom-right bottom-left
// will be border-radius: top-right top-left bottom-left bottom-right
if (m && m.length == 4) {
return [m[1], m[0], m[3], m[2]].join(' ')
} else if (m && m.length == 3) {
// super odd how this works
// 5px 10px 20px => 10px 5px 10px 20px
// yeah it's pretty dumb
return [m[1], m[0], m[1], m[2]].join(' ')
}
return v
}
function direction(v) {
return v.match(/ltr/) ? 'rtl' : v.match(/rtl/) ? 'ltr' : v
}
function rtltr(v) {
if (v.match(/left/)) return 'right'
if (v.match(/right/)) return 'left'
return v
}
function bgPosition(values) {
return values.split(/\s*,\s*/g).map(function (v) {
if (v.match(/\bleft\b/)) {
v = v.replace(/\bleft\b/, 'right')
} else if (v.match(/\bright\b/)) {
v = v.replace(/\bright\b/, 'left')
}
var m = v.trim().split(/\s+/)
if (m && (m.length == 1) && v.match(/(\d+)([a-z]{2}|%)/)) {
v = 'right ' + v
}
if (m && m.length == 2 && m[0].match(/\d+%/)) {
// 30% => 70% (100 - x)
v = (100 - parseInt(m[0], 10)) + '% ' + m[1]
}
return v
}).join(', ')
}
var propertyMap = {
'margin-left': 'margin-right'
, 'margin-right': 'margin-left'
, 'padding-left': 'padding-right'
, 'padding-right': 'padding-left'
, 'border-left': 'border-right'
, 'border-right': 'border-left'
, 'border-left-color': 'border-right-color'
, 'border-right-color': 'border-left-color'
, 'border-left-width': 'border-right-width'
, 'border-right-width': 'border-left-width'
, 'border-radius-bottomleft': 'border-radius-bottomright'
, 'border-radius-bottomright': 'border-radius-bottomleft'
, 'border-bottom-right-radius': 'border-bottom-left-radius'
, 'border-bottom-left-radius': 'border-bottom-right-radius'
, '-webkit-border-bottom-right-radius': '-webkit-border-bottom-left-radius'
, '-webkit-border-bottom-left-radius': '-webkit-border-bottom-right-radius'
, '-moz-border-radius-bottomright': '-moz-border-radius-bottomleft'
, '-moz-border-radius-bottomleft': '-moz-border-radius-bottomright'
, 'border-radius-topleft': 'border-radius-topright'
, 'border-radius-topright': 'border-radius-topleft'
, 'border-top-right-radius': 'border-top-left-radius'
, 'border-top-left-radius': 'border-top-right-radius'
, '-webkit-border-top-right-radius': '-webkit-border-top-left-radius'
, '-webkit-border-top-left-radius': '-webkit-border-top-right-radius'
, '-moz-border-radius-topright': '-moz-border-radius-topleft'
, '-moz-border-radius-topleft': '-moz-border-radius-topright'
, 'left': 'right'
, 'right': 'left'
}
var valueMap = {
'padding': quad,
'margin': quad,
'text-align': rtltr,
'float': rtltr,
'clear': rtltr,
'direction': direction,
'-webkit-border-radius': quad_radius,
'-moz-border-radius': quad_radius,
'border-radius': quad_radius,
'border-color': quad,
'border-width': quad,
'border-style': quad,
'background-position': bgPosition
}
function processRule(rule, idx, list) {
var prev = list[idx-1]
if (prev && prev.type === 'comment' && prev.comment.trim() === '@noflip')
return;
if (rule.declarations)
rule.declarations.forEach(processDeclaration)
else if (rule.rules)
rule.rules.forEach(processRule)
}
function processDeclaration(declaration) {
// Ignore comments in declarations
if (declaration.type !== 'declaration')
return
var prop = declaration.property
, val = declaration.value
, important = /!important/
, isImportant = val.match(important)
, asterisk = prop.match(/^(\*+)(.+)/, '')
if (asterisk) {
prop = asterisk[2]
asterisk = asterisk[1]
} else {
asterisk = ''
}
prop = propertyMap[prop] || prop
val = valueMap[prop] ? valueMap[prop](val) : val
if (!val.match(important) && isImportant) val += '!important'
declaration.property = asterisk + prop;
declaration.value = val;
}
function r2(cssString, options) {
var ast
if (!options)
options = { compress: true }
ast = css.parse(cssString)
ast.stylesheet.rules.forEach(processRule)
return css.stringify(ast, options)
}
module.exports.exec = function (args) {
var out
, read = args[0]
, out = args[1]
, options = { compress: args[2] !== '--no-compress' }
, data
/*
/ If no read arg then read from stdin
/ allows for standard piping and reading in
/ ex: lessc file.less | r2 > file-rtl.css
/ ex: r2 < file.css
*/
if (!read) {
var buffer = ''
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', function(chunk) {
buffer += chunk
});
process.stdin.on('end', function() {
if (buffer) {
console.log(r2(buffer, options))
}
});
} else {
/*
/ If reading from a file then print to stdout or out arg
/ To stdout: r2 styles.css
/ To file: r2 styles.cc styles-rtl.css
*/
data = fs.readFileSync(read, 'utf8')
if (out) {
console.log('Swapping ' + read + ' to ' + out + '...')
fs.writeFileSync(out, r2(data, options), 'utf8')
} else {
console.log(r2(data, options))
}
}
}
module.exports.swap = function (cssString, options) {
return r2(cssString, options)
}