-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSSPropertyTranslator.js
80 lines (78 loc) · 2.75 KB
/
CSSPropertyTranslator.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
if (typeof __dirname != "undefined") this.parse = CSSPropertyTranslator;
function CSSPropertyTranslator(property, value, re, parse, bind) {
// @license WTFPL - http://en.wikipedia.org/wiki/WTFPL
bind = CSSPropertyTranslator.bind || function (o) {
var self = this;
return function () {
return self.apply(o, arguments);
};
};
value = "" + value;
switch(property) {
case 'border-radius':
re = /^.+$/;
parse = {parse:function (prefix, match) {
return prefix + ":" + match;
}};
return "/* webkit */\n" + [
value.replace(re, bind.call(function (match) {
return this.parse("-webkit-border-radius", match);
}, parse)),
value.replace(re, bind.call(function (match) {
return this.parse("border-radius", match);
}, parse))
].join(";\n") + ";";
case 'min-device-pixel-ratio':
re = /\d*(?:\.\d*)/;
parse = {parse:function (vendor, value) {
if (vendor == "o") {
// this case, opera only
// a stupid way to transform
// 1.5 into 3/2
var
splitted = value.split("."),
multiply = Math.pow(10, (splitted[1] || "").length),
decimal = splitted[0] * multiply,
floating = splitted[1],
divisor = Math.round(decimal / floating)
;
return multiply == 1 ? value : (divisor * value) + "/" + divisor;
}
}};
return "/* webkit, moz, o */\n" + [
value.replace(re, bind.call(function (wholeMatch) {
return "-webkit-min-device-pixel-ratio:" +
wholeMatch
;
}, parse)),
value.replace(re, bind.call(function (wholeMatch) {
// the infamous prefix
return "min--moz-device-pixel-ratio:" +
wholeMatch
;
}, parse)),
value.replace(re, bind.call(function (wholeMatch) {
return "-o-min-device-pixel-ratio:" +
// the infamous parser
this.parse("o", wholeMatch)
// this object can be tested in isolation
// and compiled, considering self.parse is the only
// available method (so far)
;
}, parse)),
value.replace(re, bind.call(function (
wholeMatch, // what RegExp found
matchN, // optional arg
// if no parentheses used
// N as one or more match[N]
index, // optional arg if you don't need it
input // optional arg if you don't need it
) {
return "min-device-pixel-ratio:" +
wholeMatch
;
}, parse))
].join(";\n") + ";";
}
return property + ":" + value + ";";
}