-
Notifications
You must be signed in to change notification settings - Fork 46
/
compatible.js
126 lines (105 loc) · 3.54 KB
/
compatible.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
/*!
* compatible.js
* @author ydr.me
* 2014-09-16 16:20
*/
define(function (require, exports, module) {
/**
* @module core/navigator/compatible
* @requires utils/dato
* @requires utils/string
*/
'use strict';
var dato = require('../../utils/dato.js');
var string = require('../../utils/string.js');
var html5Prefixs = ['', 'webkit', 'moz', 'ms', 'MS'];
var css3Prefixs = ['', '-webkit', '-moz', '-ms'];
var regCss3 = /^-(webkit|moz|ms|o)-/i;
var p = document.createElement('p');
var fixCss = {
'float': 'cssFloat'
};
var regOn = /^on/;
/**
* 获取有浏览器前缀的方法名称
* @param {String} standard 标准属性、方法名称
* @param {Object} parent 标准方法父级
* @param {Boolean} [isEventType=false] 是否为事件类型
* @returns {String} 私有属性、方法名称
*
* @example
* compatible.html5('audioContext', window);
* // => "webkitAudioContext"
*/
exports.html5 = function (standard, parent, isEventType) {
var html5Key = null;
var find = false;
if (isEventType) {
standard = standard.replace(regOn, '');
}
dato.each(html5Prefixs, function (index, prefix) {
html5Key = isEventType ?
(prefix + standard ) :
(prefix ? prefix + _toUpperCaseFirstLetter(standard) : standard);
if ((isEventType ? 'on' : '') + html5Key in parent) {
find = true;
return false;
}
});
html5Key = find ? html5Key : undefined;
return html5Key;
};
/**
* 获取有浏览器前缀的CSS3名称
* @param {String} standardKey 标准的CSS3属性
* @param {String} [standardVal] 标准的CSS3属性值
* @returns {String|null} 私有CSS3属性
*
* @example
* compatible.css3('border-start');
* // => "-webkit-border-start"
*/
exports.css3 = function (standardKey, standardVal) {
var cssKey = null;
var cssVal = null;
var findKey = false;
var findVal = null;
standardKey = string.separatorize(standardKey.trim().replace(regCss3, ''));
dato.each(css3Prefixs, function (index, prefix) {
cssKey = prefix ? prefix + '-' + standardKey : standardKey;
var testCssKey = fixCss[cssKey] ? fixCss[cssKey] : cssKey;
testCssKey = string.humprize(testCssKey);
if (testCssKey in p.style) {
findKey = true;
if (standardVal) {
dato.each(css3Prefixs, function (index, prefix) {
cssVal = prefix ? prefix + '-' + standardVal : standardVal;
p.style[testCssKey] = cssVal;
if (p.style[testCssKey].indexOf(standardVal) > -1) {
findVal = cssVal;
return false;
}
});
if (findVal) {
return false;
}
} else {
return false;
}
}
});
if (standardVal) {
return findVal ? [cssKey, cssVal] : null;
}
return findKey ? cssKey : null;
};
/**
* 大写单词中的第一个字母
* @param {String} word
* @returns {String}
* @private
*/
function _toUpperCaseFirstLetter(word) {
return word[0].toUpperCase() + word.substr(1);
}
});