forked from imsky/cssFx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcssfx.js
411 lines (365 loc) · 12.7 KB
/
cssfx.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* cssFx.js - Vendor prefix polyfill for CSS3 properties - v0.9.7
* http://github.com/imsky/cssFx
* (C) 2011-2012 Ivan Malopinsky - http://imsky.co
*
* Provided under BSD License.
*/
var cssFx = cssFx || {};
/*
* cssFx process:
*
* fx.fetchCSS - external files
* fx.processCSS
* fx.fetchCSS - @import
* fx.processCSS
* fx.insertCSS
* fx.processDec - keyframes
* fx.processDec - main CSS
* fx.insertCSS
*
*/
(function (fx) {
function ajax(url, callback) {
//adapted from microAjax.js
//140medley.js xhr object
var xhr = function (a) {
for (a = 0; a < 4; a++) try {
return a ? new ActiveXObject([, "Msxml2", "Msxml3", "Microsoft"][a] + ".XMLHTTP") : new XMLHttpRequest
} catch (b) {}
}
if (r = xhr()) {
r.onreadystatechange = function () {
r.readyState == 4 && callback(r.responseText);
};
r.open("GET", url, true);
r.send(null);
}
}
function contentLoaded(e){var t=window,n="addEventListener",r="complete",i="readystatechange",s=!1,o=s,u=!0,a=t.document,f=a.documentElement,l=a[n]?n:"attachEvent",c=a[n]?"removeEventListener":"detachEvent",h=a[n]?"":"on",p=function(n){if(n.type==i&&a.readyState!=r)return;(n.type=="load"?t:a)[c](h+n.type,p,s),!o&&(o=!0)&&e.call(t,n.type||n)},d=function(){try{f.doScroll("left")}catch(e){setTimeout(d,50);return}p("poll")};if(a.readyState==r)e.call(t,"lazy");else{if(a.createEventObject&&f.doScroll){try{u=!t.frameElement}catch(v){}u&&d()}a[l](h+"DOMContentLoaded",p,s),a[l](h+i,p,s),t[l](h+"load",p,s)}}
function str_combo(text, mode) {
//If mode is defined, the function works as strip_css_comments + str_trim, otherwise as str_trim
return text.replace(mode != null ? /\/\*([\s\S]*?)\*\//gim : "", "").replace(/\n/gm, "").replace(/^\s*|\s*$/g,"").replace(/\s{2,}|\t/gm, " ");
}
function rgb2hex(a, b, c) {
//140bytes
return ((256 + a << 8 | b) << 8 | c).toString(16).slice(1)
}
function inArray(a, b) {
for (var d = 0, c = b.length; d < c; d++) if (b[d] == a) return !0;
return !1
}
function forEach(b, c) {
for (var d = b.length, a = 0; a < d; a++) c.call(this, b[a])
};
function props() {
var animation = "animation",
border = "border",
background = "background",
box = "box",
column = "column",
transition = "transition",
transform = "transform",
properties = {
moz_and_webkit: [
background + "-origin",
background + "-size",
border + "-image",
border + "-image-outset",
border + "-image-repeat",
border + "-image-source",
border + "-image-width",
border + "-radius",
box + "-shadow",
column + "-count",
column + "-gap",
column + "-rule",
column + "-rule-color",
column + "-rule-style",
column + "-rule-width",
column + "-width"],
moz_and_webkit_and_ms: [
box + "-flex",
box + "-orient",
box + "-align",
box + "-ordinal-group",
box + "-flex-group",
box + "-pack",
box + "-direction",
box + "-lines",
box + "-sizing",
animation + "-duration",
animation + "-name",
animation + "-delay",
animation + "-direction",
animation + "-iteration-count",
animation + "-play-state",
animation + "-timing-function",
animation + "-fill-mode"],
moz_and_webkit_and_ms_and_opera: [
transform,
transform + "-origin",
transition,
transition + "-property",
transition + "-duration",
transition + "-timing-function",
transition + "-delay",
"user-select"],
misc: [
background + "-clip",
border + "-bottom-left-radius",
border + "-bottom-right-radius",
border + "-top-left-radius",
border + "-top-right-radius"
]
}
return properties;
}
//cssFx-specific data
var prefix = ["-moz-", "-webkit-", "-o-", "-ms-"];
var properties = props();
var _moz = prefix[0],
_webkit = prefix[1],
_opera = prefix[2],
_ms = prefix[3];
var prefixes01 = properties.moz_and_webkit;
var prefixes013 = properties.moz_and_webkit_and_ms;
var prefixes0123 = properties.moz_and_webkit_and_ms_and_opera;
var prefixesMisc = properties.misc;
var prefixed_rules = prefixesMisc.concat(prefixes0123, prefixes01, prefixes013);
var supported_rules = ["display", "opacity", "text-overflow", "background-image", "background"].concat(prefixed_rules);
var ms_gradient = "filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='{1}', endColorstr='{2}',GradientType=0)";
fx.processCSS = function (cssFiles,url) {
var css_fx_output = [];
var css_regex = /([\s\S]*?)\{([\s\S]*?)\}/gim;
var import_regex = /\@import\s+(?:url\([\'\"]?(.*)[\'\"]?\))\s*\;?/gim;
var keyframes_regex = /@keyframes\s*([^\{]*)\{([^@]*)\}/g;
for (var x = 0; x < cssFiles.length; x++) {
var css = str_combo(cssFiles[x], 1);
var rules = [];
var imports = import_regex.test(css) && css.match(import_regex);
var keyframes = keyframes_regex.test(css) && css.match(keyframes_regex);
import_regex.lastIndex = 0;
keyframes_regex.lastIndex = 0;
//Processing imports, removing them from the main CSS, then processing and inserting them
for (var y = 0; y < imports.length; y++) {
css = css.replace(imports[y],"")
var file = import_regex.exec(imports[y])[1];
var import_url = file[0] == "/" ? file : url.replace(/[^\/]*?$/,'')+file;
fx.fetchCSS(import_url, function(f){
//Fetch @import, relative to its parent stylesheet
fx.insertCSS(fx.processCSS([f],url));
})
import_regex.lastIndex = 0;
}
//Processing keyframes, removing them from the main CSS, then processing each declaration individually
for (var y = 0, g = keyframes.length; y < g; y++) {
css = css.replace(keyframes[y], "");
var keyframe = keyframes_regex.exec(keyframes[y])
if (keyframe) {
var kfs = keyframe[2].match(css_regex),
kfs_p = [];
for (var _k = 0; _k < kfs.length; _k++) {
//k[1] = selector, k[2] = rule; just like in standard CSS
var k = css_regex.exec(kfs[_k])
if (k) {
kfs_p.push(str_combo(k[1]) + "{" + fx.processDec(k[2], true) + "}");
}
css_regex.lastIndex = 0;
}
forEach([0, 1, 3], function (i) {
rules.push("@" + prefix[i] + "keyframes " + str_combo(keyframe[1]) + "{" + kfs_p.join("\n") + "}");
})
}
keyframes_regex.lastIndex = 0;
}
var matches = css_regex.test(css) && css.match(css_regex);
css_regex.lastIndex = 0;
for (var _x = 0, l = matches.length; _x < l; _x++) {
var nextMatch = css_regex.exec(matches[_x]);
if (nextMatch !== null) {
var selector = str_combo(nextMatch[1], 1);
var rule = str_combo(nextMatch[2], 1);
for (var _y = 0, _l = supported_rules.length; _y < _l; _y++) {
if (rule.indexOf(supported_rules[_y])>=0) {
var new_dec = fx.processDec(rule);
if (new_dec) {
rules.push(selector + "{" + new_dec + "}");
}
break;
}
}
}
css_regex.lastIndex = 0;
}
if (rules.length) {
css_fx_output.push(rules.join("\n"));
}
}
return css_fx_output;
}
fx.insertCSS = function (output) {
for (var x = 0; x < output.length; x++) {
var css_fx_output = document.createElement('style');
css_fx_output.setAttribute('type', 'text/css');
if (css_fx_output.styleSheet) {
//Internet Explorer
css_fx_output.styleSheet.cssText = output[x];
} else {
//Everyone else
css_fx_output.textContent = output[x];
}
document.getElementsByTagName("head")[0].appendChild(css_fx_output);
}
}
fx.processDec = function (originalRule, includeAllProperties) {
var css_array = originalRule.split(";"),
rules = [], __background = "background";
for (var r = 0; r < css_array.length; r++) {
if (css_array[r].indexOf(":")<0) continue;
var rule = css_array[r].split(":");
if (rule.length != 2) continue;
var property = str_combo(rule[0]);
var value = str_combo(rule[1]);
var clean_rule = [property, value].join(":");
var new_rules = [];
if (inArray(property, prefixes01)) {
//-moz, -webkit
new_rules.push(_moz + clean_rule, _webkit + clean_rule);
} else if (inArray(property, prefixes013)) {
//-moz, -webkit, -ms
new_rules.push( _moz + clean_rule,
_webkit + clean_rule,
(property == "box-align" ? _ms + property + ":middle" : _ms + clean_rule));
} else if (inArray(property, prefixes0123)) {
//-moz, -webkit, -o, -ms
//This includes all transition rules
forEach([0, 1, 2, 3], function (i) {
var current_prefix = prefix[i];
if (property == "transition") {
var trans_prop = value.split(" ")[0];
if (inArray(trans_prop, prefixed_rules)) {
new_rules.push(current_prefix + clean_rule.replace(trans_prop, current_prefix + trans_prop));
} else {
new_rules.push(current_prefix + clean_rule);
}
} else if (property == "transition-property") {
if (current_prefix == _moz) {
//Only Firefox supports this at the moment
var trans_props = value.split(",");
var replaced_props = [];
forEach(trans_props, function (p) {
var prop = str_combo(p);
if (inArray(prop, prefixed_rules)) {
replaced_props.push(current_prefix + prop);
}
});
new_rules.push(current_prefix + property + ":" + replaced_props.join(","))
}
} else {
new_rules.push(current_prefix + clean_rule);
}
});
} else if (inArray(property, prefixesMisc)) {
if (property == __background + "-clip") {
if (value === "padding-box") {
new_rules.push( _webkit + clean_rule,
_moz + property + ":padding");
}
} else {
//Border-radius properties here ONLY
var v = property.split("-");
new_rules.push( _moz + "border-radius-" + v[1] + v[2] + ":" + value,
_webkit + clean_rule);
}
} else {
switch (property) {
case "display":
if (value == "box") {
//display:box
forEach([0, 1, 3], function (i) {
new_rules.push("display:" + prefix[i] + value)
});
} else if (value == "inline-block") {
//display:inline-block
new_rules.push( "display:" + _moz + "inline-stack",
"zoom:1;*display:inline");
}
break;
case "text-overflow":
if (value == "ellipsis") {
new_rules.push(_opera + clean_rule);
}
break;
case "opacity":
var opacity = Math.round(value*100);
new_rules.push( _ms + "filter:progid:DXImageTransform.Microsoft.Alpha(Opacity=" + opacity + ")",
"filter: alpha(opacity=" + opacity + ")",
_moz + clean_rule,
_webkit + clean_rule);
break;
case __background + "-image":
case __background + "-color":
case __background:
var lg = "linear-gradient";
if (value.indexOf(lg)>=0) {
var attributes = new RegExp(lg+"\\s?\\((.*)\\)","ig").exec(value);
if(attributes[1]!=null){
attributes = attributes[1];
var prop = lg + "("+attributes+")";
forEach([0, 1, 2, 3], function (i) {
new_rules.push(property + ":" + prefix[i] + prop);
});
var attributes_colors = attributes.match(/\#([a-z0-9]{3,})/g);
if(attributes_colors && attributes_colors.length>1 && attributes_colors[attributes_colors.length-1]!=null){
new_rules.push(ms_gradient.replace("{1}",attributes_colors[0]).replace("{2}",attributes_colors[attributes_colors.length-1]));
}
}
} else if (value.indexOf("rgba")>=0) {
//Color array
var cA = value.match(/rgba\((.*?)\)/)[1].split(",");
var hex = Math.floor(+(str_combo(cA[3])) * 255).toString(16) + rgb2hex(+str_combo(cA[0]), +str_combo(cA[1]), +str_combo(cA[2]));
new_rules.push(ms_gradient.replace("{1}","#"+hex).replace("{2}","#"+hex)+";zoom:1");
}
break;
default:
if (!!includeAllProperties) {
new_rules.push(clean_rule);
}
break;
}
}
if (new_rules.length) {
rules.push(new_rules.join(";"));
}
}
return rules.length && rules.join(";");
}
fx.fetchCSS = function (file, callback) {
ajax(file, (callback == null ?
function (f) {
fx.insertCSS(fx.processCSS([f],file))
} : callback));
}
var fxinit = function () {
var style_els = document.getElementsByTagName("style");
var link_els = document.getElementsByTagName("link");
//Processing external stylesheets
for (var x in link_els) {
if (typeof (link_els[x]) === "object" && link_els[x].className === "cssfx") {
fx.fetchCSS(link_els[x].href);
}
}
var css_files = [];
//Processing in-page stylesheets
for (var x in style_els) {
if (typeof (style_els[x]) === "object") {
css_files.push(style_els[x].innerHTML);
}
}
if(css_files.length){
fx.insertCSS(fx.processCSS(css_files))
}
}
contentLoaded(fxinit);
})(cssFx);