forked from LeaVerou/css3test
-
Notifications
You must be signed in to change notification settings - Fork 3
/
supports.js
158 lines (118 loc) · 3.56 KB
/
supports.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
/*! matchMedia() polyfill - Test a CSS media type/query in JS.
Authors & copyright (c) 2012: Scott Jehl, Paul Irish, Nicholas Zakas. Dual MIT/BSD license */
window.matchMedia = window.matchMedia || (function(doc, undefined){
var bool,
docElem = doc.documentElement,
refNode = docElem.firstElementChild || docElem.firstChild,
// fakeBody required for <FF4 when executed in <head>
fakeBody = doc.createElement('body'),
div = doc.createElement('div');
div.id = 'mq-test-1';
div.style.cssText = "position:absolute;top:-100em";
fakeBody.appendChild(div);
return function(q){
div.innerHTML = '­<style media="'+q+'"> #mq-test-1 { width: 42px; }</style>';
docElem.insertBefore(fakeBody, refNode);
bool = div.offsetWidth == 42;
docElem.removeChild(fakeBody);
return { matches: bool, media: q };
};
})(document);
(function(){
/**
* Setup dummy elements
*/
var dummy = document.createElement('_'),
inline = dummy.style,
style = document.createElement('style');
document.documentElement.appendChild(style);
dummy.setAttribute('data-foo', 'bar');
dummy.setAttribute('data-px', '1px');
document.documentElement.appendChild(dummy);
var _ = window.Supports = {
prefixes: ['', '-moz-', '-webkit-', '-o-', '-ms-', 'ms-', '-khtml-'],
property: function(property) {
if(property.charAt(0) === '-') {
return camelCase(property) in inline? property : false;
}
if(!_.property.cached) {
_.property.cached = {};
}
else if(_.property.cached[property]) {
return _.property.cached[property];
}
for(var i=0; i<_.prefixes.length; i++) {
var prefixed = _.prefixes[i] + property;
if(camelCase(prefixed) in inline) {
return _.property.cached[property] = prefixed;
}
}
return _.property.cached[property] = false;
},
value: function(property, value) {
property = _.property(property);
if(!property) { return false; }
property = camelCase(property);
inline.cssText = '';
inline[property] = '';
for(var i=0; i<_.prefixes.length; i++) {
var prefixed = _.prefixes[i] + value;
try {
inline[property] = prefixed;
} catch(e) {}
if(inline.length > 0) {
return prefixed;
}
}
return false;
},
selector: function(selector) {
if(!_.selector.cached) {
_.selector.cached = {};
}
else if(_.selector.cached[selector]) {
return _.selector.cached[selector];
}
for(var i=0; i<_.prefixes.length; i++) {
var prefixed = selector.replace(/^(:+)/, '$1' + _.prefixes[i]);
try {
document.querySelector(prefixed);
return _.selector.cached[selector] = prefixed;
}
catch (e) {}
}
return _.selector.cached[selector] = false;
},
atrule: function(atrule) {
if(!_.atrule.cached) {
_.atrule.cached = {};
}
else if(_.selector.cached[atrule]) {
return _.atrule.cached[atrule];
}
for(var i=0; i<_.prefixes.length; i++) {
var prefixed = atrule.replace(/^@/, '@' + _.prefixes[i]);
style.textContent = prefixed + '{}'; // Safari 4 has issues with style.innerHTML
if(style.sheet.cssRules.length > 0) {
return _.atrule.cached[atrule] = prefixed;
}
}
return _.atrule.cached[atrule] = false;
},
mq: function(mq) {
if(window.matchMedia) {
return matchMedia(mq).media !== 'invalid';
}
else {
style.textContent = '@media ' + mq + '{ foo {} }';
return style.sheet.cssRules.length > 0? mq : false;
}
}
};
/**
* Private
*/
function camelCase (str) {
return str.replace(/-([a-z])/g, function($0, $1) { return $1.toUpperCase(); }).replace('-','');
}
})();