forked from rodneyrehm/viewport-units-buggyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewport-units-buggyfill.hacks.js
executable file
·125 lines (103 loc) · 3.47 KB
/
viewport-units-buggyfill.hacks.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
/*!
* viewport-units-buggyfill.hacks v0.5.0
* @web: https://github.com/rodneyrehm/viewport-units-buggyfill/
* @author: Zoltan Hawryluk - http://www.useragentman.com/
*/
(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.viewportUnitsBuggyfillHacks = factory();
}
}(this, function () {
'use strict';
var options;
var calcExpression = /calc\(/g;
var quoteExpression = /[\"\']/g;
var urlExpression = /url\([^\)]*\)/g;
var isOldInternetExplorer = false;
var supportsVminmax = true;
var supportsVminmaxCalc = true;
// WARNING!
// Do not remove the following conditional comment.
// It is required to identify the current version of IE
/*@cc_on
@if (@_jscript_version <= 10)
isOldInternetExplorer = true;
supportsVminmaxCalc = false;
supportsVminmax = false;
@end
@*/
// iOS SAFARI, IE9, or Stock Android: abuse "content" if "viewport-units-buggyfill" specified
function checkHacks(declarations, rule, name, value) {
var needsHack = name === 'content' && value.indexOf('viewport-units-buggyfill') > -1;
if (!needsHack) {
return;
}
var fakeRules = value.replace(quoteExpression, '');
fakeRules.split(';').forEach(function(fakeRuleElement) {
var fakeRule = fakeRuleElement.split(':');
if (fakeRule.length !== 2) {
return;
}
var name = fakeRule[0].trim();
if (name === 'viewport-units-buggyfill') {
return;
}
var value = fakeRule[1].trim();
declarations.push([rule, name, value]);
if (calcExpression.test(value)) {
var webkitValue = value.replace(calcExpression, '-webkit-calc(');
declarations.push([rule, name, webkitValue]);
}
});
}
return {
required: function(options) {
return options.isMobileSafari || isOldInternetExplorer;
},
initialize: function(initOptions) {
options = initOptions;
// Test viewport units support in calc() expressions
var div = document.createElement('div');
div.style.width = '1vmax';
supportsVminmax = div.style.width !== '';
// there is no accurate way to detect this programmatically.
if (options.isMobileSafari || options.isBadStockAndroid) {
supportsVminmaxCalc = false;
}
},
initializeEvents: function(options, refresh, _refresh) {
if (options.force) {
return;
}
if (isOldInternetExplorer && !options._listeningToResize) {
window.addEventListener('resize', _refresh, true);
options._listeningToResize = true;
}
},
findDeclarations: function(declarations, rule, name, value) {
if (name === null) {
// KeyframesRule does not have a CSS-PropertyName
return;
}
checkHacks(declarations, rule, name, value);
},
overwriteDeclaration: function(rule, name, _value) {
if (isOldInternetExplorer && name === 'filter') {
// remove unit "px" from complex value, e.g.:
// filter: progid:DXImageTransform.Microsoft.DropShadow(OffX=5.4px, OffY=3.9px, Color=#000000);
_value = _value.replace(/px/g, '');
}
return _value;
}
};
}));