-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcssprefix.esm.js
180 lines (153 loc) · 4.87 KB
/
cssprefix.esm.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
/* ================================================
DON'T MANUALLY EDIT THIS FILE
================================================ */
/*
* CSSPrefix
* https://github.com/anseki/cssprefix
*
* Copyright (c) 2021 anseki
* Licensed under the MIT license.
*/
function ucf(text) {
return text.substr(0, 1).toUpperCase() + text.substr(1);
}
var PREFIXES = ['webkit', 'moz', 'ms', 'o'],
NAME_PREFIXES = PREFIXES.reduce(function (prefixes, prefix) {
prefixes.push(prefix);
prefixes.push(ucf(prefix));
return prefixes;
}, []),
VALUE_PREFIXES = PREFIXES.map(function (prefix) {
return "-".concat(prefix, "-");
}),
/**
* Get sample CSSStyleDeclaration.
* @returns {CSSStyleDeclaration}
*/
getDeclaration = function () {
var declaration;
return function () {
return declaration = declaration || document.createElement('div').style;
};
}(),
/**
* Normalize name.
* @param {} propName - A name that is normalized.
* @returns {string} A normalized name.
*/
normalizeName = function () {
var rePrefixedName = new RegExp('^(?:' + PREFIXES.join('|') + ')(.)', 'i'),
reUc = /[A-Z]/;
return function (propName) {
return (propName = (propName + '').replace(/\s/g, '').replace(/-([\da-z])/gi, function (str, p1) {
return p1.toUpperCase();
}) // camelCase
// 'ms' and 'Ms' are found by rePrefixedName 'i' option
.replace(rePrefixedName, function (str, p1) {
return reUc.test(p1) ? p1.toLowerCase() : str;
}) // Remove prefix
).toLowerCase() === 'float' ? 'cssFloat' : propName;
}; // For old CSSOM
}(),
/**
* Normalize value.
* @param {} propValue - A value that is normalized.
* @returns {string} A normalized value.
*/
normalizeValue = function () {
var rePrefixedValue = new RegExp('^(?:' + VALUE_PREFIXES.join('|') + ')', 'i');
return function (propValue) {
return (propValue != null ? propValue + '' : '').replace(/\s/g, '').replace(rePrefixedValue, '');
};
}(),
/**
* Polyfill for `CSS.supports`.
* @param {string} propName - A name.
* @param {string} propValue - A value.
* Since `CSSStyleDeclaration.setProperty` might return unexpected result,
* the `propValue` should be checked before the `cssSupports` is called.
* @returns {boolean} `true` if given pair is accepted.
*/
cssSupports = function () {
return (// return window.CSS && window.CSS.supports || ((propName, propValue) => {
// `CSS.supports` doesn't find prefixed property.
function (propName, propValue) {
var declaration = getDeclaration(); // In some browsers, `declaration[prop] = value` updates any property.
propName = propName.replace(/[A-Z]/g, function (str) {
return "-".concat(str.toLowerCase());
}); // kebab-case
declaration.setProperty(propName, propValue);
return declaration[propName] != null && // Because getPropertyValue returns '' if it is unsupported
declaration.getPropertyValue(propName) === propValue;
}
);
}(),
// Cache
propNames = {},
propValues = {};
function getName(propName) {
propName = normalizeName(propName);
if (propName && propNames[propName] == null) {
var declaration = getDeclaration();
if (declaration[propName] != null) {
// Original
propNames[propName] = propName;
} else {
// Try with prefixes
var ucfName = ucf(propName);
if (!NAME_PREFIXES.some(function (prefix) {
var prefixed = prefix + ucfName;
if (declaration[prefixed] != null) {
propNames[propName] = prefixed;
return true;
}
return false;
})) {
propNames[propName] = false;
}
}
}
return propNames[propName] || void 0;
}
function getValue(propName, propValue) {
var res;
if (!(propName = getName(propName))) {
return res;
} // Invalid property
propValues[propName] = propValues[propName] || {};
(Array.isArray(propValue) ? propValue : [propValue]).some(function (propValue) {
propValue = normalizeValue(propValue);
if (propValues[propName][propValue] != null) {
// Cache
if (propValues[propName][propValue] !== false) {
res = propValues[propName][propValue];
return true;
}
return false; // Continue to next value
}
if (cssSupports(propName, propValue)) {
// Original
res = propValues[propName][propValue] = propValue;
return true;
}
if (VALUE_PREFIXES.some(function (prefix) {
// Try with prefixes
var prefixed = prefix + propValue;
if (cssSupports(propName, prefixed)) {
res = propValues[propName][propValue] = prefixed;
return true;
}
return false;
})) {
return true;
}
propValues[propName][propValue] = false;
return false; // Continue to next value
});
return typeof res === 'string' ? res : void 0; // It might be empty string.
}
var CSSPrefix = {
getName: getName,
getValue: getValue
};
export default CSSPrefix;