febs-browser 1.2.13 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/common/string.js CHANGED
@@ -88,14 +88,21 @@ exports.getByteSize = function (s) {
88
88
  var charCode;
89
89
  for (i = 0; i < s.length; i++) {
90
90
  charCode = s.charCodeAt(i);
91
- if (charCode < 0x007f) {
91
+ if (charCode < 0x0080) {
92
92
  totalLength = totalLength + 1;
93
- } else if ((0x0080 <= charCode) && (charCode <= 0x07ff)) {
93
+ } else if (charCode < 0x0800) {
94
94
  totalLength += 2;
95
- } else if ((0x0800 <= charCode) && (charCode <= 0xffff)) {
95
+ } else if (charCode >= 0xD800 && charCode <= 0xDBFF) {
96
+ // utf-16 代理对: 组合成一个码点, 编码为 4 字节.
97
+ var next = s.charCodeAt(i + 1);
98
+ if (next >= 0xDC00 && next <= 0xDFFF) {
99
+ totalLength += 4;
100
+ i++;
101
+ } else {
102
+ totalLength += 3;
103
+ }
104
+ } else {
96
105
  totalLength += 3;
97
- } else if ((0x10000 <= charCode)) {
98
- totalLength += 4;
99
106
  }
100
107
  }
101
108
  //alert(totalLength);
@@ -141,24 +148,33 @@ exports.utf8ToBytes = function (str) {
141
148
  return new Array();
142
149
  }
143
150
  var bytes = new Array();
144
- var len, c;
145
- len = str.length;
146
- for (var i = 0; i < len; i++) {
151
+ var c;
152
+ for (var i = 0; i < str.length; i++) {
147
153
  c = str.charCodeAt(i);
148
- if (c >= 0x010000 && c <= 0x10FFFF) {
149
- bytes.push(((c >> 18) & 0x07) | 0xF0);
150
- bytes.push(((c >> 12) & 0x3F) | 0x80);
151
- bytes.push(((c >> 6) & 0x3F) | 0x80);
154
+
155
+ // utf-16 代理对: 组合成一个码点后再编码.
156
+ if (c >= 0xD800 && c <= 0xDBFF) {
157
+ var next = str.charCodeAt(i + 1);
158
+ if (next >= 0xDC00 && next <= 0xDFFF) {
159
+ c = 0x10000 + ((c - 0xD800) << 10) + (next - 0xDC00);
160
+ i++;
161
+ }
162
+ }
163
+
164
+ if (c <= 0x007F) {
165
+ bytes.push(c & 0xFF);
166
+ } else if (c <= 0x07FF) {
167
+ bytes.push(((c >> 6) & 0x1F) | 0xC0);
152
168
  bytes.push((c & 0x3F) | 0x80);
153
- } else if (c >= 0x000800 && c <= 0x00FFFF) {
169
+ } else if (c <= 0xFFFF) {
154
170
  bytes.push(((c >> 12) & 0x0F) | 0xE0);
155
171
  bytes.push(((c >> 6) & 0x3F) | 0x80);
156
172
  bytes.push((c & 0x3F) | 0x80);
157
- } else if (c >= 0x000080 && c <= 0x0007FF) {
158
- bytes.push(((c >> 6) & 0x1F) | 0xC0);
159
- bytes.push((c & 0x3F) | 0x80);
160
173
  } else {
161
- bytes.push(c & 0xFF);
174
+ bytes.push(((c >> 18) & 0x07) | 0xF0);
175
+ bytes.push(((c >> 12) & 0x3F) | 0x80);
176
+ bytes.push(((c >> 6) & 0x3F) | 0x80);
177
+ bytes.push((c & 0x3F) | 0x80);
162
178
  }
163
179
  }
164
180
  return bytes;
@@ -197,7 +213,13 @@ exports.bytesToUtf8 = function (utf8Bytes) {
197
213
  unicode |= (utf8Bytes[pos + 1] & 0x3F) << 12;
198
214
  unicode |= (utf8Bytes[pos + 2] & 0x3F) << 6;
199
215
  unicode |= (utf8Bytes[pos + 3] & 0x3F);
200
- unicodeStr += String.fromCharCode(unicode);
216
+ if (unicode > 0xFFFF) {
217
+ // 4 字节序列需要还原为 utf-16 代理对.
218
+ unicode -= 0x10000;
219
+ unicodeStr += String.fromCharCode(0xD800 + (unicode >> 10), 0xDC00 + (unicode & 0x03FF));
220
+ } else {
221
+ unicodeStr += String.fromCharCode(unicode);
222
+ }
201
223
  pos += 4;
202
224
 
203
225
  } else if ((flag & 0xE0) === 0xE0) {
@@ -9,12 +9,25 @@
9
9
 
10
10
  var Window = "undefined" != typeof window ? window : ("undefined" != typeof global ? global : ("undefined" != typeof self ? self : undefined));
11
11
 
12
+ /**
13
+ * @desc: 获取 userAgent. 无 window/无 navigator 时返回空串, 避免在 node 下崩溃.
14
+ * @return: string.
15
+ */
16
+ function getUserAgent(userAgent) {
17
+ if (userAgent) return userAgent;
18
+ if (Window && Window.navigator && Window.navigator.userAgent) {
19
+ return Window.navigator.userAgent;
20
+ }
21
+ return '';
22
+ }
12
23
 
13
24
  /**
14
25
  * @desc: 判断是否是ie.
26
+ * @deprecated 构建已不再支持 IE, 保留该接口仅为兼容.
15
27
  */
16
28
  exports.browserIsIE =
17
29
  function () {
30
+ if (!Window) return false;
18
31
  if (!!Window.ActiveXObject || "ActiveXObject" in Window)
19
32
  return true;
20
33
  else
@@ -25,12 +38,13 @@ exports.browserIsIE =
25
38
  * @desc: 判断ie版本号.
26
39
  * @return number. 非ie返回Number.MAX_SAFE_INTEGER.
27
40
  * 如果是 edge 返回 'edge'
41
+ * @deprecated 构建已不再支持 IE, 保留该接口仅为兼容.
28
42
  */
29
43
  exports.browserIEVer =
30
44
  function () {
31
45
  if (!exports.browserIsIE()) return Number.MAX_SAFE_INTEGER;
32
46
 
33
- var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
47
+ var userAgent = getUserAgent(); //取得浏览器的userAgent字符串
34
48
  var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
35
49
  var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
36
50
  if (isIE) {
@@ -61,7 +75,7 @@ exports.browserIEVer =
61
75
  */
62
76
  exports.browserIsSupportHtml5 =
63
77
  function () {
64
- if (typeof (Worker) !== "undefined") {
78
+ if (Window && typeof (Window.Worker) !== "undefined") {
65
79
  return true;
66
80
  } else {
67
81
  return false;
@@ -75,13 +89,7 @@ exports.browserIsSupportHtml5 =
75
89
  */
76
90
  exports.browserIsMobile = function(userAgent) {
77
91
 
78
- if (!userAgent) {
79
- if (typeof Window !== undefined) {
80
- userAgent = Window.navigator.userAgent
81
- }
82
- }
83
-
84
- var agent = userAgent;
92
+ var agent = getUserAgent(userAgent);
85
93
  var platforms = [
86
94
  'Android', 'webOS', 'iPhone', 'iPad',
87
95
  'iPod', 'Blackberry', 'Windows Phone'
@@ -96,13 +104,7 @@ exports.browserIsMobile = function(userAgent) {
96
104
  * @param userAgent: the browser user agent string.
97
105
  */
98
106
  exports.browserIsIOS = function(userAgent) {
99
- if (!userAgent) {
100
- if (typeof Window !== undefined) {
101
- userAgent = Window.navigator.userAgent
102
- }
103
- }
104
-
105
- var agent = userAgent;
107
+ var agent = getUserAgent(userAgent);
106
108
  var platforms = [
107
109
  'iPhone', 'iPad',
108
110
  'iPod'
@@ -118,13 +120,7 @@ exports.browserIsIOS = function(userAgent) {
118
120
  * @param userAgent: the browser user agent string.
119
121
  */
120
122
  exports.browserIsPhone = function(userAgent) {
121
- if (!userAgent) {
122
- if (typeof Window !== undefined) {
123
- userAgent = Window.navigator.userAgent
124
- }
125
- }
126
-
127
- var agent = userAgent;
123
+ var agent = getUserAgent(userAgent);
128
124
  var platforms = [
129
125
  'Android', 'iPhone',
130
126
  'iPod', 'Blackberry', 'Windows Phone'
@@ -139,13 +135,7 @@ exports.browserIsPhone = function(userAgent) {
139
135
  * @param userAgent: the browser user agent string.
140
136
  */
141
137
  exports.browserIsSafari = function(userAgent) {
142
- if (!userAgent) {
143
- if (typeof Window !== undefined) {
144
- userAgent = Window.navigator.userAgent
145
- }
146
- }
147
-
148
- var agent = userAgent.toLowerCase();
138
+ var agent = getUserAgent(userAgent).toLowerCase();
149
139
  return agent.match(/version\/([\d.]+).*safari/);
150
140
  }
151
141
 
@@ -154,13 +144,7 @@ exports.browserIsSafari = function(userAgent) {
154
144
  * @param userAgent: the browser user agent string.
155
145
  */
156
146
  exports.browserIsOpera = function(userAgent) {
157
- if (!userAgent) {
158
- if (typeof Window !== undefined) {
159
- userAgent = Window.navigator.userAgent
160
- }
161
- }
162
-
163
- var agent = userAgent.toLowerCase();
147
+ var agent = getUserAgent(userAgent).toLowerCase();
164
148
  return agent.match(/opera.([\d.]+)/);
165
149
  }
166
150
 
@@ -169,13 +153,7 @@ exports.browserIsOpera = function(userAgent) {
169
153
  * @param userAgent: the browser user agent string.
170
154
  */
171
155
  exports.browserIsFirefox = function(userAgent) {
172
- if (!userAgent) {
173
- if (typeof Window !== undefined) {
174
- userAgent = Window.navigator.userAgent
175
- }
176
- }
177
-
178
- var agent = userAgent.toLowerCase();
156
+ var agent = getUserAgent(userAgent).toLowerCase();
179
157
  return agent.match(/firefox\/([\d.]+)/);
180
158
  }
181
159
 
@@ -184,13 +162,7 @@ exports.browserIsFirefox = function(userAgent) {
184
162
  * @param userAgent: the browser user agent string.
185
163
  */
186
164
  exports.browserIsChrome = function(userAgent) {
187
- if (!userAgent) {
188
- if (typeof Window !== undefined) {
189
- userAgent = Window.navigator.userAgent
190
- }
191
- }
192
-
193
- var agent = userAgent.toLowerCase();
165
+ var agent = getUserAgent(userAgent).toLowerCase();
194
166
  return !!agent.match(/chrome\/([\d.]+)/) && !(!!agent.match(/edg\/([\d.]+)/));
195
167
  }
196
168
 
@@ -199,26 +171,14 @@ exports.browserIsChrome = function(userAgent) {
199
171
  * @param userAgent: the browser user agent string.
200
172
  */
201
173
  exports.browserIsEdge = function(userAgent) {
202
- if (!userAgent) {
203
- if (typeof Window !== undefined) {
204
- userAgent = Window.navigator.userAgent
205
- }
206
- }
207
-
208
- var agent = userAgent.toLowerCase();
174
+ var agent = getUserAgent(userAgent).toLowerCase();
209
175
  return agent.match(/edg\/([\d.]+)/);
210
176
  }
211
177
  /**
212
178
  * @desc: the browser is weixin.
213
179
  */
214
180
  exports.browserIsWeixin = function(userAgent) {
215
- if (!userAgent) {
216
- if (typeof Window !== undefined) {
217
- userAgent = Window.navigator.userAgent
218
- }
219
- }
220
-
221
- var agent = userAgent;
181
+ var agent = getUserAgent(userAgent);
222
182
  if(agent.match(/MicroMessenger/i)=="MicroMessenger") {
223
183
  return true;
224
184
  } else {
@@ -231,19 +191,16 @@ exports.browserIsWeixin = function(userAgent) {
231
191
  * @desc: the platform is Windows.
232
192
  */
233
193
  exports.platformIsWin = function(userAgent) {
234
- if (!userAgent) {
235
- if (typeof Window !== undefined) {
236
- userAgent = Window.navigator.userAgent
237
- }
238
- }
239
-
240
- var agent = userAgent;
194
+ var agent = getUserAgent(userAgent).toLowerCase();
241
195
  if (agent.indexOf("win32") >= 0 || agent.indexOf("wow32") >= 0) {
242
196
  return true;
243
197
  }
244
198
  if (agent.indexOf("win64") >= 0 || agent.indexOf("wow64") >= 0) {
245
199
  return true;
246
200
  }
201
+ if (agent.indexOf("windows nt") >= 0) {
202
+ return true;
203
+ }
247
204
 
248
205
  return false;
249
206
  }
@@ -253,13 +210,7 @@ exports.platformIsWin = function(userAgent) {
253
210
  * @desc: the platform is Mac.
254
211
  */
255
212
  exports.platformIsMac = function(userAgent) {
256
- if (!userAgent) {
257
- if (typeof Window !== undefined) {
258
- userAgent = Window.navigator.userAgent
259
- }
260
- }
261
-
262
- var agent = userAgent;
213
+ var agent = getUserAgent(userAgent);
263
214
  if (/macintosh|mac os x/i.test(agent)) {
264
215
  return true;
265
216
  }