-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewport-units-buggyfill.js
executable file
·262 lines (227 loc) · 6.74 KB
/
viewport-units-buggyfill.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
/*!
* viewport-units-buggyfill v0.3.1
* @web: https://github.com/rodneyrehm/viewport-units-buggyfill/
* @author: Rodney Rehm - http://rodneyrehm.de/en/
*/
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.viewportUnitsBuggyfill = factory();
}
}(this, function () {
'use strict';
/*global document, window, location, XMLHttpRequest, XDomainRequest*/
var initialized = false;
var viewportUnitExpression = /([0-9.-]+)(vh|vw|vmin|vmax)/g;
var forEach = [].forEach;
var join = [].join;
var dimensions;
var declarations;
var styleNode;
var is_safari_or_uiwebview = /(iPhone|iPod|iPad).+AppleWebKit/i.test(window.navigator.userAgent);
function initialize(force) {
if (initialized || (!force && !is_safari_or_uiwebview)) {
// this buggyfill only applies to mobile safari
return;
}
initialized = true;
styleNode = document.createElement('style');
styleNode.id = 'patched-viewport';
document.head.appendChild(styleNode);
// Issue #6: Cross Origin Stylesheets are not accessible through CSSOM,
// therefore download and inject them as <style> to circumvent SOP.
importCrossOriginLinks(function() {
//window.addEventListener('orientationchange', updateStyles, true);
// doing a full refresh rather than updateStyles because an orientationchange
// could activate different stylesheets
window.addEventListener('orientationchange', refresh, true);
refresh();
});
}
function updateStyles() {
styleNode.textContent = getReplacedViewportUnits();
}
function refresh() {
if (!initialized) {
return;
}
findProperties();
updateStyles();
}
function findProperties() {
declarations = [];
forEach.call(document.styleSheets, function(sheet) {
if (sheet.ownerNode.id === 'patched-viewport' || !sheet.cssRules) {
// skip entire sheet because no rules ara present or it's the target-element of the buggyfill
return;
}
if (sheet.media.mediaText && !window.matchMedia(sheet.media.mediaText).matches) {
// skip entire sheet because media attribute doesn't match
return;
}
forEach.call(sheet.cssRules, findDeclarations);
});
return declarations;
}
function findDeclarations(rule) {
if (rule.type === 7) {
var value = rule.cssText;
viewportUnitExpression.lastIndex = 0;
if (viewportUnitExpression.test(value)) {
declarations.push([rule, null, value]);
}
return;
}
if (!rule.style) {
if (!rule.cssRules) {
return;
}
forEach.call(rule.cssRules, function(_rule) {
findDeclarations(_rule);
});
return;
}
forEach.call(rule.style, function(name) {
var value = rule.style.getPropertyValue(name);
viewportUnitExpression.lastIndex = 0;
if (viewportUnitExpression.test(value)) {
declarations.push([rule, name, value]);
}
});
}
function getReplacedViewportUnits() {
dimensions = getViewport();
var css = [];
var buffer = [];
var open;
var close;
declarations.forEach(function(item) {
var _item = overwriteDeclaration.apply(null, item);
var _open = _item.selector.length ? (_item.selector.join(' {\n') + ' {\n') : '';
var _close = new Array(_item.selector.length + 1).join('\n}');
if (!_open || _open !== open) {
if (buffer.length) {
css.push(open + buffer.join('\n') + close);
buffer.length = 0;
}
if (_open) {
open = _open;
close = _close;
buffer.push(_item.content);
} else {
css.push(_item.content);
open = null;
close = null;
}
return;
}
if (_open && !open) {
open = _open;
close = _close;
}
buffer.push(_item.content);
});
if (buffer.length) {
css.push(open + buffer.join('\n') + close);
}
return css.join('\n\n');
}
function overwriteDeclaration(rule, name, value) {
var _value = value.replace(viewportUnitExpression, replaceValues);
var _selectors = [];
if (name) {
_selectors.push(rule.selectorText);
_value = name + ': ' + _value + ';';
}
var _rule = rule.parentRule;
while (_rule) {
_selectors.unshift('@media ' + join.call(_rule.media, ', '));
_rule = _rule.parentRule;
}
return {
selector: _selectors,
content: _value
};
}
function replaceValues(match, number, unit) {
var _base = dimensions[unit];
var _number = parseFloat(number) / 100;
return (_number * _base) + 'px';
}
function getViewport() {
var vh = window.innerHeight;
var vw = window.innerWidth;
return {
vh: vh,
vw: vw,
vmax: Math.max(vw, vh),
vmin: Math.min(vw, vh)
};
}
function importCrossOriginLinks(next) {
var _waiting = 0;
var decrease = function() {
_waiting--;
if (!_waiting) {
next();
}
};
forEach.call(document.styleSheets, function(sheet) {
if (!sheet.href || origin(sheet.href) === origin(location.href) ) {
// skip <style> and <link> from same origin
return;
}
_waiting++;
convertLinkToStyle(sheet.ownerNode, decrease);
});
if (!_waiting) {
next();
}
}
function origin(url) {
return url.slice(0, url.indexOf('/', url.indexOf('://') + 3));
}
function convertLinkToStyle(link, next) {
getCors(link.href, function() {
var style = document.createElement('style');
style.media = link.media;
style.setAttribute('data-href', link.href);
style.textContent = this.responseText;
link.parentNode.replaceChild(style, link);
next();
}, next);
}
function getCors(url, success, error) {
var xhr = new XMLHttpRequest();
if ('withCredentials' in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open('GET', url, true);
} else if (typeof XDomainRequest !== 'undefined') {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open('GET', url);
} else {
throw new Error('cross-domain XHR not supported');
}
xhr.onload = success;
xhr.onerror = error;
xhr.send();
return xhr;
}
return {
version: '0.3.1',
findProperties: findProperties,
getCss: getReplacedViewportUnits,
init: initialize,
refresh: refresh
};
}));