diff --git a/src/Network/XhrRequest.js b/src/Network/XhrRequest.js index 1d083c5..2096cc8 100644 --- a/src/Network/XhrRequest.js +++ b/src/Network/XhrRequest.js @@ -1,4 +1,4 @@ -import { getType, lenToUtf8Bytes } from './util' +import { getType, lenToUtf8Bytes, readBlobAsText } from './util' import { Emitter, fullUrl, @@ -47,21 +47,37 @@ export default class XhrRequest extends Emitter { }) } handleDone() { - let xhr = this._xhr, - resType = xhr.responseType - + let xhr = this._xhr + let resType = xhr.responseType let resTxt = '' - if (resType === '' || resType === 'text') resTxt = xhr.responseText - if (resType === 'json') resTxt = JSON.stringify(xhr.response) + const update = () => { + this.emit('update', this._id, { + status: xhr.status, + done: true, + size: getSize(xhr, false, this._url), + time: now(), + resTxt + }) + } - this.emit('update', this._id, { - status: xhr.status, - done: true, - size: getSize(xhr, false, this._url), - time: now(), - resTxt: resTxt - }) + const type = getType(xhr.getResponseHeader('Content-Type')) + if ( + resType === 'blob' && + (type.type === 'text' || + type.subType === 'javascript' || + type.subType === 'json') + ) { + readBlobAsText(xhr.response, (err, result) => { + if (result) resTxt = result + update() + }) + } else { + if (resType === '' || resType === 'text') resTxt = xhr.responseText + if (resType === 'json') resTxt = JSON.stringify(xhr.response) + + update() + } } } diff --git a/src/Network/util.js b/src/Network/util.js index 639725a..cda8ef7 100644 --- a/src/Network/util.js +++ b/src/Network/util.js @@ -16,3 +16,14 @@ export function lenToUtf8Bytes(str) { return str.length + (m ? m.length : 0) } + +export function readBlobAsText(blob, callback) { + const reader = new FileReader() + reader.onload = () => { + callback(null, reader.result) + } + reader.onerror = err => { + callback(err) + } + reader.readAsText(blob) +} diff --git a/src/lib/stringifyUtil.js b/src/lib/stringifyUtil.js index 348cde4..70cdc87 100644 --- a/src/lib/stringifyUtil.js +++ b/src/lib/stringifyUtil.js @@ -252,12 +252,12 @@ export var escapeJsStr = _.escapeJsStr = (function (exports) { */ exports = function exports(str) { - return toStr(str).replace(regEscapeChars, function(_char) { - switch (_char) { + return toStr(str).replace(regEscapeChars, function(char) { + switch (char) { case '"': case "'": case '\\': - return '\\' + _char; + return '\\' + char; case '\n': return '\\n'; diff --git a/src/lib/util.js b/src/lib/util.js index a5332cf..543089f 100644 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -804,30 +804,30 @@ export var utf8 = _.utf8 = (function (exports) { } if (byteIdx === byteCount) return false; - var _byte = byteArr[byteIdx]; + var byte = byteArr[byteIdx]; byteIdx++; if (!bytesNeeded) { // 0x00 to 0x7F - if ((_byte & 0x80) === 0) { - return _byte; + if ((byte & 0x80) === 0) { + return byte; } // 0xC2 to 0xDF - if ((_byte & 0xe0) === 0xc0) { + if ((byte & 0xe0) === 0xc0) { bytesNeeded = 1; - codePoint = _byte & 0x1f; - } else if ((_byte & 0xf0) === 0xe0) { + codePoint = byte & 0x1f; + } else if ((byte & 0xf0) === 0xe0) { // 0xE0 to 0xEF - if (_byte === 0xe0) lowerBoundary = 0xa0; - if (_byte === 0xed) upperBoundary = 0x9f; + if (byte === 0xe0) lowerBoundary = 0xa0; + if (byte === 0xed) upperBoundary = 0x9f; bytesNeeded = 2; - codePoint = _byte & 0xf; - } else if ((_byte & 0xf8) === 0xf0) { + codePoint = byte & 0xf; + } else if ((byte & 0xf8) === 0xf0) { // 0xF0 to 0xF4 - if (_byte === 0xf0) lowerBoundary = 0x90; - if (_byte === 0xf4) upperBoundary = 0x8f; + if (byte === 0xf0) lowerBoundary = 0x90; + if (byte === 0xf4) upperBoundary = 0x8f; bytesNeeded = 3; - codePoint = _byte & 0x7; + codePoint = byte & 0x7; } else { if (safe) return goBack(); throw new Error('Invalid UTF-8 detected'); @@ -836,7 +836,7 @@ export var utf8 = _.utf8 = (function (exports) { continue; } - if (_byte < lowerBoundary || _byte > upperBoundary) { + if (byte < lowerBoundary || byte > upperBoundary) { if (safe) { byteIdx--; return goBack(); @@ -847,7 +847,7 @@ export var utf8 = _.utf8 = (function (exports) { lowerBoundary = 0x80; upperBoundary = 0xbf; - codePoint = (codePoint << 6) | (_byte & 0x3f); + codePoint = (codePoint << 6) | (byte & 0x3f); bytesSeen++; if (bytesSeen !== bytesNeeded) continue; var tmp = codePoint; @@ -1230,12 +1230,12 @@ export var escapeJsStr = _.escapeJsStr = (function (exports) { */ exports = function exports(str) { - return toStr(str).replace(regEscapeChars, function(_char) { - switch (_char) { + return toStr(str).replace(regEscapeChars, function(char) { + switch (char) { case '"': case "'": case '\\': - return '\\' + _char; + return '\\' + char; case '\n': return '\\n'; @@ -2892,10 +2892,10 @@ export var dateFormat = _.dateFormat = (function (exports) { Z: gmt ? 'GMT' : utc - ? 'UTC' - : (toStr(date).match(regTimezone) || ['']) - .pop() - .replace(regTimezoneClip, ''), + ? 'UTC' + : (toStr(date).match(regTimezone) || ['']) + .pop() + .replace(regTimezoneClip, ''), o: (o > 0 ? '-' : '+') + padZero( @@ -4892,8 +4892,10 @@ export var $class = _.$class = (function (exports) { each(names, function(name) { if (!exports.has(el, name)) classList.push(name); }); - if (classList.length !== 0) - el.className += ' ' + classList.join(' '); + + if (classList.length !== 0) { + el.className += (el.className ? ' ' : '') + classList.join(' '); + } }); }, has: function has(els, name) { @@ -6775,7 +6777,7 @@ export var Url = _.Url = (function (exports) { * port: string; * pathname: string; * slashes: boolean; - * constructor(url: string); + * constructor(url?: string); * setQuery(name: string, value: string): Url; * setQuery(query: { [name: string]: string }): Url; * rmQuery(name: string | string[]): Url; diff --git a/test/util.js b/test/util.js index ac107a4..7462b9e 100644 --- a/test/util.js +++ b/test/util.js @@ -342,30 +342,30 @@ } if (byteIdx === byteCount) return false; - var _byte = byteArr[byteIdx]; + var byte = byteArr[byteIdx]; byteIdx++; if (!bytesNeeded) { // 0x00 to 0x7F - if ((_byte & 0x80) === 0) { - return _byte; + if ((byte & 0x80) === 0) { + return byte; } // 0xC2 to 0xDF - if ((_byte & 0xe0) === 0xc0) { + if ((byte & 0xe0) === 0xc0) { bytesNeeded = 1; - codePoint = _byte & 0x1f; - } else if ((_byte & 0xf0) === 0xe0) { + codePoint = byte & 0x1f; + } else if ((byte & 0xf0) === 0xe0) { // 0xE0 to 0xEF - if (_byte === 0xe0) lowerBoundary = 0xa0; - if (_byte === 0xed) upperBoundary = 0x9f; + if (byte === 0xe0) lowerBoundary = 0xa0; + if (byte === 0xed) upperBoundary = 0x9f; bytesNeeded = 2; - codePoint = _byte & 0xf; - } else if ((_byte & 0xf8) === 0xf0) { + codePoint = byte & 0xf; + } else if ((byte & 0xf8) === 0xf0) { // 0xF0 to 0xF4 - if (_byte === 0xf0) lowerBoundary = 0x90; - if (_byte === 0xf4) upperBoundary = 0x8f; + if (byte === 0xf0) lowerBoundary = 0x90; + if (byte === 0xf4) upperBoundary = 0x8f; bytesNeeded = 3; - codePoint = _byte & 0x7; + codePoint = byte & 0x7; } else { if (safe) return goBack(); throw new Error('Invalid UTF-8 detected'); @@ -374,7 +374,7 @@ continue; } - if (_byte < lowerBoundary || _byte > upperBoundary) { + if (byte < lowerBoundary || byte > upperBoundary) { if (safe) { byteIdx--; return goBack(); @@ -385,7 +385,7 @@ lowerBoundary = 0x80; upperBoundary = 0xbf; - codePoint = (codePoint << 6) | (_byte & 0x3f); + codePoint = (codePoint << 6) | (byte & 0x3f); bytesSeen++; if (bytesSeen !== bytesNeeded) continue; var tmp = codePoint;