forked from eqcss/eqcss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEQCSS-polyfills.js
executable file
·234 lines (196 loc) · 7.76 KB
/
EQCSS-polyfills.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
/*
# EQCSS IE8 Polyfills
## version 1.9.2
This file contains optional polyfills to provide:
IE8 support to the EQCSS.js plugin
- github.com/eqcss/eqcss
- elementqueries.com
Authors: Tommy Hodgins, Maxime Euzière
License: MIT
*/
/*
* addEventListener polyfill 1.0 / Eirik Backer / MIT Licence
* Forked from http://css-tricks.com/snippets/javascript/addeventlistner-polyfill/
* Adds the native DOM2 function addEventListener on IE6 - 8.
*/
(function (win, doc) {
// If the function already exists, no need to polyfill
if (win.addEventListener) return;
function docHijack(p) {
var old = doc[p];
doc[p] = function (v) {
return addListen(old(v))
}
}
function addEvent(on, fn, self) {
return (self = this).attachEvent('on' + on, function(e) {
e = e || win.event;
e.preventDefault = e.preventDefault || function(){e.returnValue = false}
e.stopPropagation = e.stopPropagation || function(){e.cancelBubble = true}
try {
fn.call(self, e);
}
catch (e) {}
});
}
function addListen(obj, i) {
if ((i = obj.length)) while (i--) obj[i].addEventListener = addEvent;
else obj.addEventListener = addEvent;
return obj;
}
addListen([doc, win]);
if ('Element' in win) win.Element.prototype.addEventListener = addEvent; // IE8
else{ // IE < 8
doc.attachEvent('onreadystatechange', function(){addListen(doc.all)}); // Make sure we also init at domReady
docHijack('getElementsByTagName');
docHijack('getElementById');
docHijack('createElement');
addListen(doc.all);
}
})(window, document);
/*
* getComputedStyle and getPropertyValue polyfill / Jonathan Neal / License CC0
* Forked from: https://github.com/Financial-Times/polyfill-service/tree/master/polyfills/getComputedStyle
* Allows to measure a CSS property of any element on IE6-8.
* Dimensions (width, height...) are converted and returned in pixels, like modern browsers do.
*/
(function(win) {
// If the function already exists, no need to polyfill
if (win.getComputedStyle) return;
function getComputedStylePixel(element, property, fontSize) {
// Internet Explorer sometimes struggles to read currentStyle until the element's document is accessed.
var value = element.document && element.currentStyle[property].match(/([\d.]+)(%|cm|em|in|mm|pc|pt|)/) || [0, 0, ''],
size = value[1],
suffix = value[2],
rootSize;
fontSize = !fontSize ? fontSize : /%|em/.test(suffix) && element.parentElement ? getComputedStylePixel(element.parentElement, 'fontSize', null) : 16;
rootSize = property == 'fontSize' ? fontSize : /width/i.test(property) ? element.clientWidth : element.clientHeight;
return suffix == '%' ? size / 100 * rootSize :
suffix == 'cm' ? size * 0.3937 * 96 :
suffix == 'em' ? size * fontSize :
suffix == 'in' ? size * 96 :
suffix == 'mm' ? size * 0.3937 * 96 / 10 :
suffix == 'pc' ? size * 12 * 96 / 72 :
suffix == 'pt' ? size * 96 / 72 :
size;
}
function setShortStyleProperty(style, property) {
var borderSuffix = property == 'border' ? 'Width' : '',
t = property + 'Top' + borderSuffix,
r = property + 'Right' + borderSuffix,
b = property + 'Bottom' + borderSuffix,
l = property + 'Left' + borderSuffix;
style[property] = (style[t] == style[r] && style[t] == style[b] && style[t] == style[l] ? [ style[t] ] :
style[t] == style[b] && style[l] == style[r] ? [ style[t], style[r] ] :
style[l] == style[r] ? [ style[t], style[r], style[b] ] :
[ style[t], style[r], style[b], style[l] ]).join(' ');
}
// <CSSStyleDeclaration>
function CSSStyleDeclaration(element) {
var style = this,
currentStyle = element.currentStyle,
fontSize = getComputedStylePixel(element, 'fontSize'),
unCamelCase = function (match) {
return '-' + match.toLowerCase();
},
property;
for (property in currentStyle) {
Array.prototype.push.call(style, property == 'styleFloat' ? 'float' : property.replace(/[A-Z]/, unCamelCase));
if (property == 'width') {
style[property] = element.offsetWidth + 'px';
} else if (property == 'height') {
style[property] = element.offsetHeight + 'px';
} else if (property == 'styleFloat') {
style.float = currentStyle[property];
} else if (/margin.|padding.|border.+W/.test(property) && style[property] != 'auto') {
style[property] = Math.round(getComputedStylePixel(element, property, fontSize)) + 'px';
} else if (/^outline/.test(property)) {
// errors on checking outline
try {
style[property] = currentStyle[property];
} catch (error) {
style.outlineColor = currentStyle.color;
style.outlineStyle = style.outlineStyle || 'none';
style.outlineWidth = style.outlineWidth || '0px';
style.outline = [style.outlineColor, style.outlineWidth, style.outlineStyle].join(' ');
}
} else {
style[property] = currentStyle[property];
}
}
setShortStyleProperty(style, 'margin');
setShortStyleProperty(style, 'padding');
setShortStyleProperty(style, 'border');
style.fontSize = Math.round(fontSize) + 'px';
}
CSSStyleDeclaration.prototype = {
constructor: CSSStyleDeclaration,
// <CSSStyleDeclaration>.getPropertyPriority
getPropertyPriority: function () {
throw new Error('NotSupportedError: DOM Exception 9');
},
// <CSSStyleDeclaration>.getPropertyValue
getPropertyValue: function (property) {
return this[property.replace(/-\w/g, function (match) {
return match[1].toUpperCase();
})];
},
// <CSSStyleDeclaration>.item
item: function (index) {
return this[index];
},
// <CSSStyleDeclaration>.removeProperty
removeProperty: function () {
throw new Error('NoModificationAllowedError: DOM Exception 7');
},
// <CSSStyleDeclaration>.setProperty
setProperty: function () {
throw new Error('NoModificationAllowedError: DOM Exception 7');
},
// <CSSStyleDeclaration>.getPropertyCSSValue
getPropertyCSSValue: function () {
throw new Error('NotSupportedError: DOM Exception 9');
}
};
// <win>.getComputedStyle
win.getComputedStyle = function getComputedStyle(element) {
return new CSSStyleDeclaration(element);
};
})(window);
/*
* document.querySelector and querySelectorAll polyfill / Maxime Euzière / public domain
* Forked from: http://xem.github.io/Lazy/
* Adds basic DOM selection on IE6-8 (selection by tag, class or id only)
*/
(function(doc) {
if (doc.querySelectorAll) return;
doc.querySelectorAll = function(a) {
if ("#" == a.charAt(0)) return [doc.getElementById(a.substr(1))];
if ("." == a.charAt(0)) return doc.getElementsByClassName(a.substr(1));
return doc.getElementsByTagName(a);
}
doc.querySelector = function(a) {
return querySelectorAll(a)[0];
}
})(document);
/*
* textContent polyfill / Eli Grey / cc-by-nc 3.0 license
* Forked from http://eligrey.com/blog/post/textcontent-in-ie8/
* Adds textContent property to DOM elements in IE8
*/
(function() {
if (Object.defineProperty
&& Object.getOwnPropertyDescriptor
&& Object.getOwnPropertyDescriptor(Element.prototype, "textContent")
&& !Object.getOwnPropertyDescriptor(Element.prototype, "textContent").get) {
var innerText = Object.getOwnPropertyDescriptor(Element.prototype, "innerText");
Object.defineProperty(Element.prototype, "textContent", {
get: function() {
return innerText.get.call(this)
},
set: function(x) {
return innerText.set.call(this, x)
}
});
}
})();