This repository has been archived by the owner on Mar 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewport-units-buggyfill.js
220 lines (202 loc) · 5.59 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
/*!
* viewport-units-buggyfill-ie9 v0.0.1
* IE9 only buggyfill for viewport units.
* Based on the excellent work on Rodney Rehm's viewport-units-buggyfill:
* https://github.com/rodneyrehm/viewport-units-buggyfill/
*/
(function(root, factory) {
if (typeof exports === "object") {
module.exports = factory();
} else if (typeof define === "function" && define.amd) {
define([], factory);
} else {
root.ViewportUnitsBuggyfillIE9 = factory(root.Spinner);
}
}(this, function () {
'use strict';
var initialized = false;
var viewportUnitExpression = /([+-]?[0-9.]+)(vh|vw|vmin|vmax)/g;
var forEach = [].forEach;
var styleNode;
var isInternetExplorer9 = navigator.appVersion.indexOf("MSIE 9.") != -1;
/*
* Helper functions
*/
function isViewportUnitExpression(value) {
viewportUnitExpression.lastIndex = 0;
return viewportUnitExpression.test(value);
}
function debounce(func, wait) {
var timeout;
return function() {
var context = this;
var args = arguments;
var callback = function() {
func.apply(context, args);
};
clearTimeout(timeout);
timeout = setTimeout(callback, wait);
};
}
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 insertStyleNode() {
var node = document.createElement('style');
node.id = 'patched-viewport';
document.head.appendChild(node)
return node;
}
function updateStyleNode(content) {
styleNode.textContent = content;
// move to the end in case inline <style>s were added dynamically
styleNode.parentNode.appendChild(styleNode);
}
/*
* Public
*/
function initialize(options) {
if (initialized) {
return;
}
options = options || {};
if (!options.force && !isInternetExplorer9) {
// this buggyfill only applies to old Internet Explorer
return;
}
initialized = true;
styleNode = insertStyleNode();
window.addEventListener('resize', debounce(refresh, options.refreshDebounceWait || 100), true);
refresh();
}
function refresh() {
if (!initialized) {
return;
}
updateStyleNode(
getReplacedViewportUnits(
findDeclarations()));
}
/*
* Find declarations
*/
function findDeclarations() {
var declarations = [];
forEach.call(document.styleSheets, function(sheet) {
if (sheet.ownerNode.id === 'patched-viewport' || !sheet.cssRules) {
// skip entire sheet because no rules are present, it's supposed to be ignored or it's the target-element of the buggyfill
return;
}
if (sheet.media && sheet.media.mediaText && window.matchMedia && !window.matchMedia(sheet.media.mediaText).matches) {
// skip entire sheet because media attribute doesn't match
return;
}
forEach.call(sheet.cssRules, function(rule) {
findDeclarationsInRule(declarations, rule);
});
});
return declarations;
}
function findDeclarationsInRule(declarations, rule) {
if (!rule) {
return;
}
if (!rule.style) {
if (!rule.cssRules) {
return;
}
forEach.call(rule.cssRules, function(_rule) {
findDeclarationsInRule(_rule);
});
return;
}
forEach.call(rule.style, function(name) {
var value = rule.style.getPropertyValue(name);
if (isViewportUnitExpression(value)) {
declarations.push([rule, name, value]);
}
});
}
/*
* Replace CSS
*/
function getReplacedViewportUnits(declarations) {
var dimensions = getViewport();
var css = [];
var buffer = [];
var open;
var close;
var overwriteDeclaration = _overwriteDeclaration(dimensions);
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(dimensions) {
var replaceValues = _replaceValues(dimensions);
return function(rule, name, value) {
var _value;
var _selectors = [];
_value = value.replace(viewportUnitExpression, replaceValues);
if (name) {
// skipping KeyframesRule
_selectors.push(rule.selectorText);
_value = name + ': ' + _value + ';';
}
var _rule = rule.parentRule;
while (_rule) {
_selectors.unshift('@media ' + _rule.media.mediaText);
_rule = _rule.parentRule;
}
return {
selector: _selectors,
content: _value
};
}
}
function _replaceValues(dimensions) {
return function(match, number, unit) {
var _base = dimensions[unit];
var _number = parseFloat(number) / 100;
return (_number * _base) + 'px';
}
}
return {
version: '0.0.1',
init: initialize,
refresh: refresh
};
}));