From a15360838b4f2787dda481af79921086f883663b Mon Sep 17 00:00:00 2001 From: surunzi Date: Mon, 9 Apr 2018 23:57:30 +0800 Subject: [PATCH] Del: SafeMutationObserver --- doc/UTIL_API.md | 45 +-- eustia/SafeMutationObserver.js | 23 -- src/Elements/Elements.js | 4 +- src/Resources/Resources.js | 4 +- src/lib/util.js | 617 +++++++++++++++++---------------- test/util.js | 524 ++++++++++++++-------------- 6 files changed, 606 insertions(+), 611 deletions(-) delete mode 100644 eustia/SafeMutationObserver.js diff --git a/doc/UTIL_API.md b/doc/UTIL_API.md index 6737e3f..ed79f71 100644 --- a/doc/UTIL_API.md +++ b/doc/UTIL_API.md @@ -446,7 +446,7 @@ Logging methods. TRACE, DEBUG, INFO, WARN, ERROR and SILENT. ```javascript -var logger = new Logger('eris', logger.level.ERROR); +var logger = new Logger('eris', Logger.level.ERROR); logger.trace('test'); // Format output. @@ -468,12 +468,12 @@ logger.on('debug', function (argList) }); ``` -## SafeMutationObserver +## MutationObserver Safe MutationObserver, does nothing if MutationObserver is not supported. ```javascript -var observer = new DomObserver(function (mutations) +var observer = new MutationObserver(function (mutations) { // Do something. }); @@ -678,15 +678,16 @@ Perform an asynchronous HTTP request. Available options: -|Name |Type |Desc | -|-------------|-------------|------------------------| -|url |string |Request url | -|data |string object|Request data | -|dataType=json|string |Response type(json, xml)| -|success |function |Success callback | -|error |function |Error callback | -|complete |function |Callback after request | -|timeout |number |Request timeout | +|Name |Type |Desc | +|---------------------------------------------|-------------|---------------------------| +|url |string |Request url | +|data |string object|Request data | +|dataType=json |string |Response type(json, xml) | +|contentType=application/x-www-form-urlencoded|string |Request header Content-Type| +|success |function |Success callback | +|error |function |Error callback | +|complete |function |Callback after request | +|timeout |number |Request timeout | ### get @@ -792,10 +793,10 @@ castPath('a.b.c', {'a.b.c': true}); // -> ['a.b.c'] Split array into groups the length of given size. -|Name |Type |Desc | -|--------|------|--------------------| -|arr |array |Array to process | -|[size=1]|number|Length of each chunk| +|Name |Type |Desc | +|------|------|--------------------| +|arr |array |Array to process | +|size=1|number|Length of each chunk| ```javascript chunk([1, 2, 3, 4], 2); // -> [[1, 2], [3, 4]] @@ -1257,11 +1258,11 @@ identity('a'); // -> 'a' Get the index at which the first occurrence of value. -|Name |Type |Desc | -|-----------|------|--------------------| -|arr |array |Array to search | -|val |* |Value to search for | -|[fromIdx=0]|number|Index to search from| +|Name |Type |Desc | +|---------|------|--------------------| +|arr |array |Array to search | +|val |* |Value to search for | +|fromIdx=0|number|Index to search from| ```javascript idxOf([1, 2, 1, 2], 2, 2); // -> 3 @@ -1996,7 +1997,7 @@ This accumulates the arguments passed into an array, after a given index. |return |function|Generated function with rest parameters| ```javascript -var paramArr = _.restArgs(function (rest) { return rest }); +var paramArr = restArgs(function (rest) { return rest }); paramArr(1, 2, 3, 4); // -> [1, 2, 3, 4] ``` diff --git a/eustia/SafeMutationObserver.js b/eustia/SafeMutationObserver.js deleted file mode 100644 index ebe319d..0000000 --- a/eustia/SafeMutationObserver.js +++ /dev/null @@ -1,23 +0,0 @@ -/* Safe MutationObserver, does nothing if MutationObserver is not supported. - * - * ```javascript - * var observer = new DomObserver(function (mutations) - * { - * // Do something. - * }); - * observer.observe(document.htmlElement); - * observer.disconnect(); - * ``` - */ - -exports = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver; - -if (!exports) -{ - exports = class MutationObserver { - constructor() {} - observe() {} - disconnect() {} - takeRecords() {} - }; -} diff --git a/src/Elements/Elements.js b/src/Elements/Elements.js index fe7428f..bcf0d6b 100644 --- a/src/Elements/Elements.js +++ b/src/Elements/Elements.js @@ -8,7 +8,7 @@ import { evalCss, $, keys, - SafeMutationObserver, + MutationObserver, each, isErudaEl, toStr, @@ -350,7 +350,7 @@ export default class Elements extends Tool } _initObserver() { - this._observer = new SafeMutationObserver(mutations => + this._observer = new MutationObserver(mutations => { each(mutations, mutation => this._handleMutation(mutation)); }); diff --git a/src/Resources/Resources.js b/src/Resources/Resources.js index 4a9d195..5c7ed03 100644 --- a/src/Resources/Resources.js +++ b/src/Resources/Resources.js @@ -12,7 +12,7 @@ import { orientation, isCrossOrig, ajax, - SafeMutationObserver, + MutationObserver, isErudaEl, toArr, concat, @@ -408,7 +408,7 @@ export default class Resources extends Tool } _initObserver() { - this._observer = new SafeMutationObserver(mutations => + this._observer = new MutationObserver(mutations => { let needToRender = false; each(mutations, mutation => diff --git a/src/lib/util.js b/src/lib/util.js index b82ae98..df38db2 100644 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -359,37 +359,6 @@ export var freeze = _.freeze = (function () return exports; })(); -/* ------------------------------ SafeMutationObserver ------------------------------ */ - -export var SafeMutationObserver = _.SafeMutationObserver = (function (exports) -{ - /* Safe MutationObserver, does nothing if MutationObserver is not supported. - * - * ```javascript - * var observer = new DomObserver(function (mutations) - * { - * // Do something. - * }); - * observer.observe(document.htmlElement); - * observer.disconnect(); - * ``` - */ - - exports = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver; - - if (!exports) - { - exports = class MutationObserver { - constructor() {} - observe() {} - disconnect() {} - takeRecords() {} - }; - } - - return exports; -})({}); - /* ------------------------------ noop ------------------------------ */ export var noop = _.noop = (function () @@ -626,10 +595,10 @@ export var chunk = _.chunk = (function () { /* Split array into groups the length of given size. * - * |Name |Type |Desc | - * |--------|------|--------------------| - * |arr |array |Array to process | - * |[size=1]|number|Length of each chunk| + * |Name |Type |Desc | + * |------|------|--------------------| + * |arr |array |Array to process | + * |size=1|number|Length of each chunk| * * ```javascript * chunk([1, 2, 3, 4], 2); // -> [[1, 2], [3, 4]] @@ -718,11 +687,11 @@ export var idxOf = _.idxOf = (function () { /* Get the index at which the first occurrence of value. * - * |Name |Type |Desc | - * |-----------|------|--------------------| - * |arr |array |Array to search | - * |val |* |Value to search for | - * |[fromIdx=0]|number|Index to search from| + * |Name |Type |Desc | + * |---------|------|--------------------| + * |arr |array |Array to search | + * |val |* |Value to search for | + * |fromIdx=0|number|Index to search from| * * ```javascript * idxOf([1, 2, 1, 2], 2, 2); // -> 3 @@ -778,78 +747,78 @@ export var toStr = _.toStr = (function () export var ucs2 = _.ucs2 = (function (exports) { - /* UCS-2 encoding and decoding. - * - * ### encode - * - * Create a string using an array of code point values. - * - * |Name |Type |Desc | - * |------|------|--------------------| - * |arr |array |Array of code points| - * |return|string|Encoded string | - * - * ### decode - * - * Create an array of code point values using a string. - * - * |Name |Type |Desc | - * |------|------|--------------------| - * |str |string|Input string | - * |return|array |Array of code points| - * - * ```javascript - * ucs2.encode([0x61, 0x62, 0x63]); // -> 'abc' - * ucs2.decode('abc'); // -> [0x61, 0x62, 0x63] - * '𝌆'.length; // -> 2 - * ucs2.decode('𝌆').length; // -> 1 - * ``` - */ + /* UCS-2 encoding and decoding. + * + * ### encode + * + * Create a string using an array of code point values. + * + * |Name |Type |Desc | + * |------|------|--------------------| + * |arr |array |Array of code points| + * |return|string|Encoded string | + * + * ### decode + * + * Create an array of code point values using a string. + * + * |Name |Type |Desc | + * |------|------|--------------------| + * |str |string|Input string | + * |return|array |Array of code points| + * + * ```javascript + * ucs2.encode([0x61, 0x62, 0x63]); // -> 'abc' + * ucs2.decode('abc'); // -> [0x61, 0x62, 0x63] + * '𝌆'.length; // -> 2 + * ucs2.decode('𝌆').length; // -> 1 + * ``` + */ - /* module - * env: all - * test: all - */ + /* module + * env: all + * test: all + */ - // https://mathiasbynens.be/notes/javascript-encoding - exports = { - encode: function (arr) - { - return String.fromCodePoint.apply(String, arr); - }, - decode: function (str) - { - var ret = []; + // https://mathiasbynens.be/notes/javascript-encoding + exports = { + encode: function (arr) + { + return String.fromCodePoint.apply(String, arr); + }, + decode: function (str) + { + var ret = []; - var i = 0, - len = str.length; + var i = 0, + len = str.length; - while(i < len) - { - var c = str.charCodeAt(i++); + while(i < len) + { + var c = str.charCodeAt(i++); - // A high surrogate - if (c >= 0xD800 && c <= 0xDBFF && i < len) - { - var tail = str.charCodeAt(i++); - // nextC >= 0xDC00 && nextC <= 0xDFFF - if ((tail & 0xFC00) === 0xDC00) - { - // C = (H - 0xD800) * 0x400 + L - 0xDC00 + 0x10000 - ret.push(((c & 0x3FF) << 10) + (tail & 0x3FF) + 0x10000); - } else - { - ret.push(c); - i--; - } - } else - { - ret.push(c); - } - } + // A high surrogate + if (c >= 0xD800 && c <= 0xDBFF && i < len) + { + var tail = str.charCodeAt(i++); + // nextC >= 0xDC00 && nextC <= 0xDFFF + if ((tail & 0xFC00) === 0xDC00) + { + // C = (H - 0xD800) * 0x400 + L - 0xDC00 + 0x10000 + ret.push(((c & 0x3FF) << 10) + (tail & 0x3FF) + 0x10000); + } else + { + ret.push(c); + i--; + } + } else + { + ret.push(c); + } + } - return ret; - } + return ret; + } }; return exports; @@ -859,223 +828,223 @@ export var ucs2 = _.ucs2 = (function (exports) export var utf8 = _.utf8 = (function (exports) { - /* UTF-8 encoding and decoding. - * - * ### encode - * - * Turn any UTF-8 decoded string into UTF-8 encoded string. - * - * |Name |Type |Desc | - * |------|------|----------------| - * |str |string|String to encode| - * |return|string|Encoded string | - * - * ### decode - * - * |Name |Type |Desc | - * |------------|-------|----------------------| - * |str |string |String to decode | - * |[safe=false]|boolean|Suppress error if true| - * |return |string |Decoded string | - * - * Turn any UTF-8 encoded string into UTF-8 decoded string. - * - * ```javascript - * utf8.encode('\uD800\uDC00'); // -> '\xF0\x90\x80\x80' - * utf8.decode('\xF0\x90\x80\x80'); // -> '\uD800\uDC00' - * ``` - */ + /* UTF-8 encoding and decoding. + * + * ### encode + * + * Turn any UTF-8 decoded string into UTF-8 encoded string. + * + * |Name |Type |Desc | + * |------|------|----------------| + * |str |string|String to encode| + * |return|string|Encoded string | + * + * ### decode + * + * |Name |Type |Desc | + * |------------|-------|----------------------| + * |str |string |String to decode | + * |[safe=false]|boolean|Suppress error if true| + * |return |string |Decoded string | + * + * Turn any UTF-8 encoded string into UTF-8 decoded string. + * + * ```javascript + * utf8.encode('\uD800\uDC00'); // -> '\xF0\x90\x80\x80' + * utf8.decode('\xF0\x90\x80\x80'); // -> '\uD800\uDC00' + * ``` + */ - /* module - * env: all - * test: all + /* module + * env: all + * test: all */ /* dependencies * ucs2 - */ + */ - // https://encoding.spec.whatwg.org/#utf-8 - exports = { - encode: function (str) - { - var codePoints = ucs2.decode(str); + // https://encoding.spec.whatwg.org/#utf-8 + exports = { + encode: function (str) + { + var codePoints = ucs2.decode(str); - var byteArr = ''; + var byteArr = ''; - for (var i = 0, len = codePoints.length; i < len; i++) - { - byteArr += encodeCodePoint(codePoints[i]); - } + for (var i = 0, len = codePoints.length; i < len; i++) + { + byteArr += encodeCodePoint(codePoints[i]); + } - return byteArr; - }, - decode: function decode(str, safe) - { - byteArr = ucs2.decode(str); - byteIdx = 0; - byteCount = byteArr.length; - codePoint = 0; - bytesSeen = 0; - bytesNeeded = 0; - lowerBoundary = 0x80; - upperBoundary = 0xBF; + return byteArr; + }, + decode: function decode(str, safe) + { + byteArr = ucs2.decode(str); + byteIdx = 0; + byteCount = byteArr.length; + codePoint = 0; + bytesSeen = 0; + bytesNeeded = 0; + lowerBoundary = 0x80; + upperBoundary = 0xBF; - var codePoints = []; + var codePoints = []; - var tmp; + var tmp; - while((tmp = decodeCodePoint(safe)) !== false) - { - codePoints.push(tmp); - } + while((tmp = decodeCodePoint(safe)) !== false) + { + codePoints.push(tmp); + } - return ucs2.encode(codePoints); - } - }; + return ucs2.encode(codePoints); + } + }; - var fromCharCode = String.fromCharCode; + var fromCharCode = String.fromCharCode; - function encodeCodePoint(codePoint) - { - // U+0000 to U+0080, ASCII code point - if ((codePoint & 0xFFFFFF80) === 0) - { - return fromCharCode(codePoint); - } + function encodeCodePoint(codePoint) + { + // U+0000 to U+0080, ASCII code point + if ((codePoint & 0xFFFFFF80) === 0) + { + return fromCharCode(codePoint); + } - var ret = '', - count, - offset; + var ret = '', + count, + offset; - // U+0080 to U+07FF, inclusive - if ((codePoint & 0xFFFFF800) === 0) - { - count = 1; - offset = 0xC0; - } else if ((codePoint & 0xFFFF0000) === 0) - { - // U+0800 to U+FFFF, inclusive - count = 2; - offset = 0xE0; - } else if ((codePoint & 0xFFE00000) == 0) - { - // U+10000 to U+10FFFF, inclusive - count = 3; - offset = 0xF0; - } + // U+0080 to U+07FF, inclusive + if ((codePoint & 0xFFFFF800) === 0) + { + count = 1; + offset = 0xC0; + } else if ((codePoint & 0xFFFF0000) === 0) + { + // U+0800 to U+FFFF, inclusive + count = 2; + offset = 0xE0; + } else if ((codePoint & 0xFFE00000) == 0) + { + // U+10000 to U+10FFFF, inclusive + count = 3; + offset = 0xF0; + } - ret += fromCharCode((codePoint >> (6 * count)) + offset); + ret += fromCharCode((codePoint >> (6 * count)) + offset); - while (count > 0) - { - var tmp = codePoint >> (6 * (count - 1)); - ret += fromCharCode(0x80 | tmp & 0x3F); - count--; - } + while (count > 0) + { + var tmp = codePoint >> (6 * (count - 1)); + ret += fromCharCode(0x80 | tmp & 0x3F); + count--; + } - return ret; - } + return ret; + } - var byteArr, - byteIdx, - byteCount, - codePoint, - bytesSeen, - bytesNeeded, - lowerBoundary, - upperBoundary; + var byteArr, + byteIdx, + byteCount, + codePoint, + bytesSeen, + bytesNeeded, + lowerBoundary, + upperBoundary; - function decodeCodePoint(safe) - { - /* eslint-disable no-constant-condition */ - while (true) - { - if (byteIdx >= byteCount && bytesNeeded) - { - if (safe) return goBack(); - throw new Error('Invalid byte index'); - } + function decodeCodePoint(safe) + { + /* eslint-disable no-constant-condition */ + while (true) + { + if (byteIdx >= byteCount && bytesNeeded) + { + if (safe) return goBack(); + throw new Error('Invalid byte index'); + } - if (byteIdx === byteCount) return false; + if (byteIdx === byteCount) return false; - var byte = byteArr[byteIdx]; - byteIdx++; + var byte = byteArr[byteIdx]; + byteIdx++; - if (!bytesNeeded) - { - // 0x00 to 0x7F - if ((byte & 0x80) === 0) - { - return byte; - } - // 0xC2 to 0xDF - if ((byte & 0xE0) === 0xC0) - { - bytesNeeded = 1; - codePoint = byte & 0x1F; - } else if ((byte & 0xF0) === 0xE0) - { - // 0xE0 to 0xEF - if (byte === 0xE0) lowerBoundary = 0xA0; - if (byte === 0xED) upperBoundary = 0x9F; - bytesNeeded = 2; - codePoint = byte & 0xF; - } else if ((byte & 0xF8) === 0xF0) - { - // 0xF0 to 0xF4 - if (byte === 0xF0) lowerBoundary = 0x90; - if (byte === 0xF4) upperBoundary = 0x8F; - bytesNeeded = 3; - codePoint = byte & 0x7; - } else - { - if (safe) return goBack(); - throw new Error('Invalid UTF-8 detected'); - } + if (!bytesNeeded) + { + // 0x00 to 0x7F + if ((byte & 0x80) === 0) + { + return byte; + } + // 0xC2 to 0xDF + if ((byte & 0xE0) === 0xC0) + { + bytesNeeded = 1; + codePoint = byte & 0x1F; + } else if ((byte & 0xF0) === 0xE0) + { + // 0xE0 to 0xEF + if (byte === 0xE0) lowerBoundary = 0xA0; + if (byte === 0xED) upperBoundary = 0x9F; + bytesNeeded = 2; + codePoint = byte & 0xF; + } else if ((byte & 0xF8) === 0xF0) + { + // 0xF0 to 0xF4 + if (byte === 0xF0) lowerBoundary = 0x90; + if (byte === 0xF4) upperBoundary = 0x8F; + bytesNeeded = 3; + codePoint = byte & 0x7; + } else + { + if (safe) return goBack(); + throw new Error('Invalid UTF-8 detected'); + } - continue; - } + continue; + } - if (byte < lowerBoundary || byte > upperBoundary) - { - if (safe) { - byteIdx--; - return goBack(); - } - throw new Error('Invalid continuation byte'); - } + if (byte < lowerBoundary || byte > upperBoundary) + { + if (safe) { + byteIdx--; + return goBack(); + } + throw new Error('Invalid continuation byte'); + } - lowerBoundary = 0x80; - upperBoundary = 0xBF; + lowerBoundary = 0x80; + upperBoundary = 0xBF; - codePoint = (codePoint << 6) | (byte & 0x3F); + codePoint = (codePoint << 6) | (byte & 0x3F); - bytesSeen++; + bytesSeen++; - if (bytesSeen !== bytesNeeded) continue; + if (bytesSeen !== bytesNeeded) continue; - var tmp = codePoint; + var tmp = codePoint; - codePoint = 0; - bytesNeeded = 0; - bytesSeen = 0; + codePoint = 0; + bytesNeeded = 0; + bytesSeen = 0; - return tmp; - } - } + return tmp; + } + } - function goBack() - { - var start = byteIdx - bytesSeen - 1; - byteIdx = start + 1; - codePoint = 0; - bytesNeeded = 0; - bytesSeen = 0; - lowerBoundary = 0x80; - upperBoundary = 0xBF; + function goBack() + { + var start = byteIdx - bytesSeen - 1; + byteIdx = start + 1; + codePoint = 0; + bytesNeeded = 0; + bytesSeen = 0; + lowerBoundary = 0x80; + upperBoundary = 0xBF; - return byteArr[start]; + return byteArr[start]; } return exports; @@ -3826,6 +3795,46 @@ export var Enum = _.Enum = (function (exports) return exports; })({}); +/* ------------------------------ MutationObserver ------------------------------ */ + +export var MutationObserver = _.MutationObserver = (function (exports) +{ + /* Safe MutationObserver, does nothing if MutationObserver is not supported. + * + * ```javascript + * var observer = new MutationObserver(function (mutations) + * { + * // Do something. + * }); + * observer.observe(document.htmlElement); + * observer.disconnect(); + * ``` + */ + + /* module + * env: browser + * test: browser + */ + + /* dependencies + * Class + */ + + exports = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver; + + if (!exports) + { + exports = Class({ + initialize: function MutationObserver() {}, + observe: function () {}, + disconnect: function () {}, + takeRecords: function () {} + }); + } + + return exports; +})({}); + /* ------------------------------ Select ------------------------------ */ export var Select = _.Select = (function (exports) @@ -5525,7 +5534,7 @@ export var restArgs = _.restArgs = (function () * |return |function|Generated function with rest parameters| * * ```javascript - * var paramArr = _.restArgs(function (rest) { return rest }); + * var paramArr = restArgs(function (rest) { return rest }); * paramArr(1, 2, 3, 4); // -> [1, 2, 3, 4] * ``` */ @@ -5787,7 +5796,7 @@ export var Logger = _.Logger = (function (exports) * TRACE, DEBUG, INFO, WARN, ERROR and SILENT. * * ```javascript - * var logger = new Logger('eris', logger.level.ERROR); + * var logger = new Logger('eris', Logger.level.ERROR); * logger.trace('test'); * * // Format output. @@ -6928,15 +6937,16 @@ export var ajax = _.ajax = (function () * * Available options: * - * |Name |Type |Desc | - * |-------------|-------------|------------------------| - * |url |string |Request url | - * |data |string object|Request data | - * |dataType=json|string |Response type(json, xml)| - * |success |function |Success callback | - * |error |function |Error callback | - * |complete |function |Callback after request | - * |timeout |number |Request timeout | + * |Name |Type |Desc | + * |---------------------------------------------|-------------|---------------------------| + * |url |string |Request url | + * |data |string object|Request data | + * |dataType=json |string |Response type(json, xml) | + * |contentType=application/x-www-form-urlencoded|string |Request header Content-Type| + * |success |function |Success callback | + * |error |function |Error callback | + * |complete |function |Callback after request | + * |timeout |number |Request timeout | * * ### get * @@ -7026,17 +7036,19 @@ export var ajax = _.ajax = (function () { data = query.stringify(data); url += url.indexOf('?') > -1 ? '&' + data : '?' + data; - } else + } else if (options.contentType === 'application/x-www-form-urlencoded') { if(isObj(data)) data = query.stringify(data); + } else if (options.contentType === 'application/json') { + if(isObj(data)) data = JSON.stringify(data); } xhr.open(type, url, true); - xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + xhr.setRequestHeader('Content-Type', options.contentType); - if (timeout > 0) + if (timeout > 0) { - abortTimeout = setTimeout(function () + abortTimeout = setTimeout(function () { xhr.onreadystatechange = noop; xhr.abort(); @@ -7055,6 +7067,7 @@ export var ajax = _.ajax = (function () error: noop, complete: noop, dataType: 'json', + contentType: 'application/x-www-form-urlencoded', data: {}, xhr: function () { return new XMLHttpRequest() }, timeout: 0 diff --git a/test/util.js b/test/util.js index f16b0fd..989fa9d 100644 --- a/test/util.js +++ b/test/util.js @@ -142,78 +142,78 @@ var ucs2 = _.ucs2 = (function (exports) { - /* UCS-2 encoding and decoding. - * - * ### encode - * - * Create a string using an array of code point values. - * - * |Name |Type |Desc | - * |------|------|--------------------| - * |arr |array |Array of code points| - * |return|string|Encoded string | - * - * ### decode - * - * Create an array of code point values using a string. - * - * |Name |Type |Desc | - * |------|------|--------------------| - * |str |string|Input string | - * |return|array |Array of code points| - * - * ```javascript - * ucs2.encode([0x61, 0x62, 0x63]); // -> 'abc' - * ucs2.decode('abc'); // -> [0x61, 0x62, 0x63] - * '𝌆'.length; // -> 2 - * ucs2.decode('𝌆').length; // -> 1 - * ``` - */ + /* UCS-2 encoding and decoding. + * + * ### encode + * + * Create a string using an array of code point values. + * + * |Name |Type |Desc | + * |------|------|--------------------| + * |arr |array |Array of code points| + * |return|string|Encoded string | + * + * ### decode + * + * Create an array of code point values using a string. + * + * |Name |Type |Desc | + * |------|------|--------------------| + * |str |string|Input string | + * |return|array |Array of code points| + * + * ```javascript + * ucs2.encode([0x61, 0x62, 0x63]); // -> 'abc' + * ucs2.decode('abc'); // -> [0x61, 0x62, 0x63] + * '𝌆'.length; // -> 2 + * ucs2.decode('𝌆').length; // -> 1 + * ``` + */ - /* module - * env: all - * test: all - */ + /* module + * env: all + * test: all + */ - // https://mathiasbynens.be/notes/javascript-encoding - exports = { - encode: function (arr) - { - return String.fromCodePoint.apply(String, arr); - }, - decode: function (str) - { - var ret = []; + // https://mathiasbynens.be/notes/javascript-encoding + exports = { + encode: function (arr) + { + return String.fromCodePoint.apply(String, arr); + }, + decode: function (str) + { + var ret = []; - var i = 0, - len = str.length; + var i = 0, + len = str.length; - while(i < len) - { - var c = str.charCodeAt(i++); + while(i < len) + { + var c = str.charCodeAt(i++); - // A high surrogate - if (c >= 0xD800 && c <= 0xDBFF && i < len) - { - var tail = str.charCodeAt(i++); - // nextC >= 0xDC00 && nextC <= 0xDFFF - if ((tail & 0xFC00) === 0xDC00) - { - // C = (H - 0xD800) * 0x400 + L - 0xDC00 + 0x10000 - ret.push(((c & 0x3FF) << 10) + (tail & 0x3FF) + 0x10000); - } else - { - ret.push(c); - i--; - } - } else - { - ret.push(c); - } - } + // A high surrogate + if (c >= 0xD800 && c <= 0xDBFF && i < len) + { + var tail = str.charCodeAt(i++); + // nextC >= 0xDC00 && nextC <= 0xDFFF + if ((tail & 0xFC00) === 0xDC00) + { + // C = (H - 0xD800) * 0x400 + L - 0xDC00 + 0x10000 + ret.push(((c & 0x3FF) << 10) + (tail & 0x3FF) + 0x10000); + } else + { + ret.push(c); + i--; + } + } else + { + ret.push(c); + } + } - return ret; - } + return ret; + } }; return exports; @@ -223,223 +223,223 @@ var utf8 = _.utf8 = (function (exports) { - /* UTF-8 encoding and decoding. - * - * ### encode - * - * Turn any UTF-8 decoded string into UTF-8 encoded string. - * - * |Name |Type |Desc | - * |------|------|----------------| - * |str |string|String to encode| - * |return|string|Encoded string | - * - * ### decode - * - * |Name |Type |Desc | - * |------------|-------|----------------------| - * |str |string |String to decode | - * |[safe=false]|boolean|Suppress error if true| - * |return |string |Decoded string | - * - * Turn any UTF-8 encoded string into UTF-8 decoded string. - * - * ```javascript - * utf8.encode('\uD800\uDC00'); // -> '\xF0\x90\x80\x80' - * utf8.decode('\xF0\x90\x80\x80'); // -> '\uD800\uDC00' - * ``` - */ + /* UTF-8 encoding and decoding. + * + * ### encode + * + * Turn any UTF-8 decoded string into UTF-8 encoded string. + * + * |Name |Type |Desc | + * |------|------|----------------| + * |str |string|String to encode| + * |return|string|Encoded string | + * + * ### decode + * + * |Name |Type |Desc | + * |------------|-------|----------------------| + * |str |string |String to decode | + * |[safe=false]|boolean|Suppress error if true| + * |return |string |Decoded string | + * + * Turn any UTF-8 encoded string into UTF-8 decoded string. + * + * ```javascript + * utf8.encode('\uD800\uDC00'); // -> '\xF0\x90\x80\x80' + * utf8.decode('\xF0\x90\x80\x80'); // -> '\uD800\uDC00' + * ``` + */ - /* module - * env: all - * test: all + /* module + * env: all + * test: all */ /* dependencies * ucs2 - */ + */ - // https://encoding.spec.whatwg.org/#utf-8 - exports = { - encode: function (str) - { - var codePoints = ucs2.decode(str); + // https://encoding.spec.whatwg.org/#utf-8 + exports = { + encode: function (str) + { + var codePoints = ucs2.decode(str); - var byteArr = ''; + var byteArr = ''; - for (var i = 0, len = codePoints.length; i < len; i++) - { - byteArr += encodeCodePoint(codePoints[i]); - } + for (var i = 0, len = codePoints.length; i < len; i++) + { + byteArr += encodeCodePoint(codePoints[i]); + } - return byteArr; - }, - decode: function decode(str, safe) - { - byteArr = ucs2.decode(str); - byteIdx = 0; - byteCount = byteArr.length; - codePoint = 0; - bytesSeen = 0; - bytesNeeded = 0; - lowerBoundary = 0x80; - upperBoundary = 0xBF; + return byteArr; + }, + decode: function decode(str, safe) + { + byteArr = ucs2.decode(str); + byteIdx = 0; + byteCount = byteArr.length; + codePoint = 0; + bytesSeen = 0; + bytesNeeded = 0; + lowerBoundary = 0x80; + upperBoundary = 0xBF; - var codePoints = []; + var codePoints = []; - var tmp; + var tmp; - while((tmp = decodeCodePoint(safe)) !== false) - { - codePoints.push(tmp); - } + while((tmp = decodeCodePoint(safe)) !== false) + { + codePoints.push(tmp); + } - return ucs2.encode(codePoints); - } - }; + return ucs2.encode(codePoints); + } + }; - var fromCharCode = String.fromCharCode; + var fromCharCode = String.fromCharCode; - function encodeCodePoint(codePoint) - { - // U+0000 to U+0080, ASCII code point - if ((codePoint & 0xFFFFFF80) === 0) - { - return fromCharCode(codePoint); - } + function encodeCodePoint(codePoint) + { + // U+0000 to U+0080, ASCII code point + if ((codePoint & 0xFFFFFF80) === 0) + { + return fromCharCode(codePoint); + } - var ret = '', - count, - offset; + var ret = '', + count, + offset; - // U+0080 to U+07FF, inclusive - if ((codePoint & 0xFFFFF800) === 0) - { - count = 1; - offset = 0xC0; - } else if ((codePoint & 0xFFFF0000) === 0) - { - // U+0800 to U+FFFF, inclusive - count = 2; - offset = 0xE0; - } else if ((codePoint & 0xFFE00000) == 0) - { - // U+10000 to U+10FFFF, inclusive - count = 3; - offset = 0xF0; - } + // U+0080 to U+07FF, inclusive + if ((codePoint & 0xFFFFF800) === 0) + { + count = 1; + offset = 0xC0; + } else if ((codePoint & 0xFFFF0000) === 0) + { + // U+0800 to U+FFFF, inclusive + count = 2; + offset = 0xE0; + } else if ((codePoint & 0xFFE00000) == 0) + { + // U+10000 to U+10FFFF, inclusive + count = 3; + offset = 0xF0; + } - ret += fromCharCode((codePoint >> (6 * count)) + offset); + ret += fromCharCode((codePoint >> (6 * count)) + offset); - while (count > 0) - { - var tmp = codePoint >> (6 * (count - 1)); - ret += fromCharCode(0x80 | tmp & 0x3F); - count--; - } + while (count > 0) + { + var tmp = codePoint >> (6 * (count - 1)); + ret += fromCharCode(0x80 | tmp & 0x3F); + count--; + } - return ret; - } + return ret; + } - var byteArr, - byteIdx, - byteCount, - codePoint, - bytesSeen, - bytesNeeded, - lowerBoundary, - upperBoundary; + var byteArr, + byteIdx, + byteCount, + codePoint, + bytesSeen, + bytesNeeded, + lowerBoundary, + upperBoundary; - function decodeCodePoint(safe) - { - /* eslint-disable no-constant-condition */ - while (true) - { - if (byteIdx >= byteCount && bytesNeeded) - { - if (safe) return goBack(); - throw new Error('Invalid byte index'); - } + function decodeCodePoint(safe) + { + /* eslint-disable no-constant-condition */ + while (true) + { + if (byteIdx >= byteCount && bytesNeeded) + { + if (safe) return goBack(); + throw new Error('Invalid byte index'); + } - if (byteIdx === byteCount) return false; + if (byteIdx === byteCount) return false; - var byte = byteArr[byteIdx]; - byteIdx++; + var byte = byteArr[byteIdx]; + byteIdx++; - if (!bytesNeeded) - { - // 0x00 to 0x7F - if ((byte & 0x80) === 0) - { - return byte; - } - // 0xC2 to 0xDF - if ((byte & 0xE0) === 0xC0) - { - bytesNeeded = 1; - codePoint = byte & 0x1F; - } else if ((byte & 0xF0) === 0xE0) - { - // 0xE0 to 0xEF - if (byte === 0xE0) lowerBoundary = 0xA0; - if (byte === 0xED) upperBoundary = 0x9F; - bytesNeeded = 2; - codePoint = byte & 0xF; - } else if ((byte & 0xF8) === 0xF0) - { - // 0xF0 to 0xF4 - if (byte === 0xF0) lowerBoundary = 0x90; - if (byte === 0xF4) upperBoundary = 0x8F; - bytesNeeded = 3; - codePoint = byte & 0x7; - } else - { - if (safe) return goBack(); - throw new Error('Invalid UTF-8 detected'); - } + if (!bytesNeeded) + { + // 0x00 to 0x7F + if ((byte & 0x80) === 0) + { + return byte; + } + // 0xC2 to 0xDF + if ((byte & 0xE0) === 0xC0) + { + bytesNeeded = 1; + codePoint = byte & 0x1F; + } else if ((byte & 0xF0) === 0xE0) + { + // 0xE0 to 0xEF + if (byte === 0xE0) lowerBoundary = 0xA0; + if (byte === 0xED) upperBoundary = 0x9F; + bytesNeeded = 2; + codePoint = byte & 0xF; + } else if ((byte & 0xF8) === 0xF0) + { + // 0xF0 to 0xF4 + if (byte === 0xF0) lowerBoundary = 0x90; + if (byte === 0xF4) upperBoundary = 0x8F; + bytesNeeded = 3; + codePoint = byte & 0x7; + } else + { + if (safe) return goBack(); + throw new Error('Invalid UTF-8 detected'); + } - continue; - } + continue; + } - if (byte < lowerBoundary || byte > upperBoundary) - { - if (safe) { - byteIdx--; - return goBack(); - } - throw new Error('Invalid continuation byte'); - } + if (byte < lowerBoundary || byte > upperBoundary) + { + if (safe) { + byteIdx--; + return goBack(); + } + throw new Error('Invalid continuation byte'); + } - lowerBoundary = 0x80; - upperBoundary = 0xBF; + lowerBoundary = 0x80; + upperBoundary = 0xBF; - codePoint = (codePoint << 6) | (byte & 0x3F); + codePoint = (codePoint << 6) | (byte & 0x3F); - bytesSeen++; + bytesSeen++; - if (bytesSeen !== bytesNeeded) continue; + if (bytesSeen !== bytesNeeded) continue; - var tmp = codePoint; + var tmp = codePoint; - codePoint = 0; - bytesNeeded = 0; - bytesSeen = 0; + codePoint = 0; + bytesNeeded = 0; + bytesSeen = 0; - return tmp; - } - } + return tmp; + } + } - function goBack() - { - var start = byteIdx - bytesSeen - 1; - byteIdx = start + 1; - codePoint = 0; - bytesNeeded = 0; - bytesSeen = 0; - lowerBoundary = 0x80; - upperBoundary = 0xBF; + function goBack() + { + var start = byteIdx - bytesSeen - 1; + byteIdx = start + 1; + codePoint = 0; + bytesNeeded = 0; + bytesSeen = 0; + lowerBoundary = 0x80; + upperBoundary = 0xBF; - return byteArr[start]; + return byteArr[start]; } return exports; @@ -1802,15 +1802,16 @@ * * Available options: * - * |Name |Type |Desc | - * |-------------|-------------|------------------------| - * |url |string |Request url | - * |data |string object|Request data | - * |dataType=json|string |Response type(json, xml)| - * |success |function |Success callback | - * |error |function |Error callback | - * |complete |function |Callback after request | - * |timeout |number |Request timeout | + * |Name |Type |Desc | + * |---------------------------------------------|-------------|---------------------------| + * |url |string |Request url | + * |data |string object|Request data | + * |dataType=json |string |Response type(json, xml) | + * |contentType=application/x-www-form-urlencoded|string |Request header Content-Type| + * |success |function |Success callback | + * |error |function |Error callback | + * |complete |function |Callback after request | + * |timeout |number |Request timeout | * * ### get * @@ -1900,17 +1901,19 @@ { data = query.stringify(data); url += url.indexOf('?') > -1 ? '&' + data : '?' + data; - } else + } else if (options.contentType === 'application/x-www-form-urlencoded') { if(isObj(data)) data = query.stringify(data); + } else if (options.contentType === 'application/json') { + if(isObj(data)) data = JSON.stringify(data); } xhr.open(type, url, true); - xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + xhr.setRequestHeader('Content-Type', options.contentType); - if (timeout > 0) + if (timeout > 0) { - abortTimeout = setTimeout(function () + abortTimeout = setTimeout(function () { xhr.onreadystatechange = noop; xhr.abort(); @@ -1929,6 +1932,7 @@ error: noop, complete: noop, dataType: 'json', + contentType: 'application/x-www-form-urlencoded', data: {}, xhr: function () { return new XMLHttpRequest() }, timeout: 0