-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr2.js
175 lines (145 loc) · 4.62 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
/*!
* R2 - a CSS LTR ∞ RTL converter
* Copyright Dustin Diaz 2012
* https://github.com/ded/r2
* License MIT
*/
var fs = require('fs')
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, m) {
// 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 = v.trim().split(/\s+/)) && m.length == 4) {
return [m[1], m[0], m[3], 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
}
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-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
}
function r2(css) {
css = css.trim() // give it a solid trimming to start
// comments
.replace(/\/\*[\s\S]+?\*\//g, '')
// line breaks and carriage returns
.replace(/[\n\r]/g, '')
// space between selectors, declarations, properties and values
.replace(/\s*([:;,{}])\s*/g, '$1')
// replace multiple spaces with single spaces
.replace(/\s+/g, ' ')
var result = css.match(/([^{]+\{[^}]+\})+?/g).map(function (rule) {
// break rule into selector|declaration parts
var parts = rule.match(/([^{]+)\{([^}]+)/)
, selector = parts[1]
, declarations = parts[2]
return selector + '{' + declarations.split(/;(?!base64)/).map(function (decl) {
if (!decl) return ''
var m = decl.match(/([^:]+):(.+)$/)
if (!m) return ''
var prop = m[1]
, val = m[2]
, important = /!important/
, isImportant = val.match(important)
prop = propertyMap[prop] || prop
val = valueMap[prop] ? valueMap[prop](val) : val
if (!val.match(important) && isImportant) val += '!important'
return prop + ':' + val + ';'
}).join('') + '}'
});
return result.join('')
}
module.exports.exec = function (args) {
var out
, read = args[0]
, out = args[1]
, 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))
}
});
} 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), 'utf8')
} else {
console.log(r2(data))
}
}
}
module.exports.swap = function (css) {
return r2(css)
}