Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ bak
.DS_Store
.*.swp
.idea
.vscode
node_modules
bower_components
coverage
PLAN.md
npm-debug.log
npm-debug.log
2 changes: 1 addition & 1 deletion dist/mock-min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mock-min.js.map

Large diffs are not rendered by default.

32 changes: 30 additions & 2 deletions dist/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -8162,6 +8162,19 @@ return /******/ (function(modules) { // webpackBootstrap
return event
}
}
try {
new window.ProgressEvent('process')
} catch (exception) {
// 如果低版本的浏览器不支持
window.ProgressEvent = function(type, nativeEvent) {
var progressEvent = document.createEvent('CustomEvent')
progressEvent.initCustomEvent(type, nativeEvent.cancelBubble, nativeEvent.cancelable, null)
progressEvent.lengthComputable = nativeEvent.lengthComputable;
progressEvent.loaded = nativeEvent.loaded;
progressEvent.total = nativeEvent.total;
return progressEvent
}
}

var XHR_STATES = {
// The object has been constructed.
Expand Down Expand Up @@ -8300,8 +8313,13 @@ return /******/ (function(modules) { // webpackBootstrap
that[XHR_RESPONSE_PROPERTIES[i]] = xhr[XHR_RESPONSE_PROPERTIES[i]]
} catch (e) {}
}
// 触发 MockXMLHttpRequest 上的同名事件
that.dispatchEvent(new Event(event.type /*, false, false, that*/ ))
if (event.type === 'progress' && event.lengthComputable) {
// 单独处理下载事件,下载的文件计算完成时发布原生的ProcessEvent
that.dispatchEvent(new window.ProgressEvent(event.type, event))
} else {
// 触发 MockXMLHttpRequest 上的同名事件
that.dispatchEvent(new Event(event.type /*, false, false, that*/ ))
}
}

// 如果未找到匹配的数据模板,则采用原生 XHR 发送请求。
Expand Down Expand Up @@ -8476,6 +8494,15 @@ return /******/ (function(modules) { // webpackBootstrap
if (this[ontype]) this[ontype](event)
}
})
// upload EventTarget
Util.extend(MockXMLHttpRequest.prototype.upload, {
addEventListener: function addEventListener() {
},
removeEventListener: function removeEventListener() {
},
dispatchEvent: function dispatchEvent() {
}
})

// Inspired by jQuery
function createNativeXMLHttpRequest() {
Expand Down Expand Up @@ -8537,6 +8564,7 @@ return /******/ (function(modules) { // webpackBootstrap

module.exports = MockXMLHttpRequest


/***/ })
/******/ ])
});
Expand Down
33 changes: 30 additions & 3 deletions src/mock/xhr/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ try {
return event
}
}
try {
new window.ProgressEvent('process')
} catch (exception) {
// 如果低版本的浏览器不支持
window.ProgressEvent = function(type, nativeEvent) {
var progressEvent = document.createEvent('CustomEvent')
progressEvent.initCustomEvent(type, nativeEvent.cancelBubble, nativeEvent.cancelable, null)
progressEvent.lengthComputable = nativeEvent.lengthComputable;
progressEvent.loaded = nativeEvent.loaded;
progressEvent.total = nativeEvent.total;
return progressEvent
}
}

var XHR_STATES = {
// The object has been constructed.
Expand Down Expand Up @@ -204,8 +217,13 @@ Util.extend(MockXMLHttpRequest.prototype, {
that[XHR_RESPONSE_PROPERTIES[i]] = xhr[XHR_RESPONSE_PROPERTIES[i]]
} catch (e) {}
}
// 触发 MockXMLHttpRequest 上的同名事件
that.dispatchEvent(new Event(event.type /*, false, false, that*/ ))
if (event.type === 'progress' && event.lengthComputable) {
// 单独处理下载事件,下载的文件计算完成时发布原生的ProcessEvent
that.dispatchEvent(new window.ProgressEvent(event.type, event))
} else {
// 触发 MockXMLHttpRequest 上的同名事件
that.dispatchEvent(new Event(event.type /*, false, false, that*/ ))
}
}

// 如果未找到匹配的数据模板,则采用原生 XHR 发送请求。
Expand Down Expand Up @@ -380,6 +398,15 @@ Util.extend(MockXMLHttpRequest.prototype, {
if (this[ontype]) this[ontype](event)
}
})
// upload EventTarget
Util.extend(MockXMLHttpRequest.prototype.upload, {
addEventListener: function addEventListener() {
},
removeEventListener: function removeEventListener() {
},
dispatchEvent: function dispatchEvent() {
}
})

// Inspired by jQuery
function createNativeXMLHttpRequest() {
Expand Down Expand Up @@ -439,4 +466,4 @@ function convert(item, options) {
item.template(options) : MockXMLHttpRequest.Mock.mock(item.template)
}

module.exports = MockXMLHttpRequest
module.exports = MockXMLHttpRequest