Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 49e0548

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(core): fix#1108, window.onerror should have (message, source, lineno, colno, error) signiture (#1109)
1 parent 875086f commit 49e0548

5 files changed

Lines changed: 66 additions & 10 deletions

File tree

‎file-size-limit.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"path": "dist/zone.min.js",
55
"checkTarget": true,
6-
"limit": 41600
6+
"limit": 42000
77
}
88
]
99
}

‎lib/browser/property-descriptor.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @suppress {globalThis}
1111
*/
1212

13-
import{isBrowser,isMix,isNode,ObjectDefineProperty,ObjectGetOwnPropertyDescriptor,ObjectGetPrototypeOf,patchClass,patchOnProperties,wrapWithCurrentZone,zoneSymbol}from'../common/utils';
13+
import{isBrowser,isIE,isMix,isNode,ObjectDefineProperty,ObjectGetOwnPropertyDescriptor,ObjectGetPrototypeOf,patchClass,patchOnProperties,wrapWithCurrentZone,zoneSymbol}from'../common/utils';
1414

1515
import*aswebSocketPatchfrom'./websocket';
1616

@@ -241,7 +241,7 @@ export interface IgnoreProperty {
241241

242242
functionfilterProperties(
243243
target: any,onProperties: string[],ignoreProperties: IgnoreProperty[]): string[]{
244-
if(!ignoreProperties){
244+
if(!ignoreProperties||ignoreProperties.length===0){
245245
returnonProperties;
246246
}
247247

@@ -276,10 +276,13 @@ export function propertyDescriptorPatch(api: _ZonePrivate, _global: any) {
276276
// for browsers that we can patch the descriptor: Chrome & Firefox
277277
if(isBrowser){
278278
constinternalWindow: any=window;
279+
constignoreErrorProperties=
280+
isIE ? [{target: internalWindow,ignoreProperties: ['error']}] : [];
279281
// in IE/Edge, onProp not exist in window object, but in WindowPrototype
280282
// so we need to pass WindowPrototype to check onProp exist or not
281283
patchFilteredProperties(
282-
internalWindow,eventNames.concat(['messageerror']),ignoreProperties,
284+
internalWindow,eventNames.concat(['messageerror']),
285+
ignoreProperties ? ignoreProperties.concat(ignoreErrorProperties) : ignoreProperties,
283286
ObjectGetPrototypeOf(internalWindow));
284287
patchFilteredProperties(Document.prototype,eventNames,ignoreProperties);
285288

‎lib/common/utils.ts‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,26 @@ const wrapFn = function(event: Event) {
135135
}
136136
consttarget=this||event.target||_global;
137137
constlistener=target[eventNameSymbol];
138-
letresult=listener&&listener.apply(this,arguments);
139-
140-
if(result!=undefined&&!result){
141-
event.preventDefault();
138+
letresult;
139+
if(isBrowser&&target===internalWindow&&event.type==='error'){
140+
// window.onerror have different signiture
141+
// https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror#window.onerror
142+
// and onerror callback will prevent default when callback return true
143+
consterrorEvent: ErrorEvent=eventasany;
144+
result=listener&&
145+
listener.call(
146+
this,errorEvent.message,errorEvent.filename,errorEvent.lineno,errorEvent.colno,
147+
errorEvent.error);
148+
if(result===true){
149+
event.preventDefault();
150+
}
151+
}else{
152+
result=listener&&listener.apply(this,arguments);
153+
if(result!=undefined&&!result){
154+
event.preventDefault();
155+
}
142156
}
157+
143158
returnresult;
144159
};
145160

@@ -475,6 +490,17 @@ export function attachOriginToPatched(patched: Function, original: any) {
475490
letisDetectedIEOrEdge=false;
476491
letieOrEdge=false;
477492

493+
exportfunctionisIE(){
494+
try{
495+
constua=internalWindow.navigator.userAgent;
496+
if(ua.indexOf('MSIE ')!==-1||ua.indexOf('Trident/')!==-1){
497+
returntrue;
498+
}
499+
}catch(error){
500+
}
501+
returnfalse;
502+
}
503+
478504
exportfunctionisIEOrEdge(){
479505
if(isDetectedIEOrEdge){
480506
returnieOrEdge;

‎test/browser/browser.spec.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import{patchFilteredProperties}from'../../lib/browser/property-descriptor';
1010
import{patchEventTarget}from'../../lib/common/events';
1111
import{isBrowser,isIEOrEdge,isMix,zoneSymbol}from'../../lib/common/utils';
12-
import{getIEVersion,ifEnvSupports,ifEnvSupportsWithDone,isEdge}from'../test-util';
12+
import{getEdgeVersion,getIEVersion,ifEnvSupports,ifEnvSupportsWithDone,isEdge}from'../test-util';
1313

1414
importSpy=jasmine.Spy;
1515
declareconstglobal: any;
@@ -190,7 +190,7 @@ describe('Zone', function() {
190190
'onvrdisplayactivate','onvrdisplayblur','onvrdisplayconnect',
191191
'onvrdisplaydeactivate','onvrdisplaydisconnect','onvrdisplayfocus',
192192
'onvrdisplaypointerrestricted','onvrdisplaypointerunrestricted',
193-
'onorientationchange'
193+
'onorientationchange','onerror'
194194
]);
195195
});
196196

@@ -390,6 +390,24 @@ describe('Zone', function() {
390390
};
391391
expect(testFn).not.toThrow();
392392
}));
393+
394+
it('window.onerror callback signiture should be (message, source, lineno, colno, error)',
395+
ifEnvSupportsWithDone(canPatchOnProperty(window,'onerror'),function(done: DoneFn){
396+
lettestError=newError('testError');
397+
window.onerror=function(
398+
message: any,source?: string,lineno?: number,colno?: number,error?: any){
399+
expect(message).toContain('testError');
400+
if(getEdgeVersion()!==14){
401+
// Edge 14, error will be undefined.
402+
expect(error).toBe(testError);
403+
}
404+
setTimeout(done);
405+
returntrue;
406+
};
407+
setTimeout(()=>{
408+
throwtestError;
409+
},100);
410+
}));
393411
}));
394412

395413
describe('eventListener hooks',function(){

‎test/test-util.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,12 @@ export function isEdge() {
123123
constuserAgent=navigator.userAgent.toLowerCase();
124124
returnuserAgent.indexOf('edge')!==-1;
125125
}
126+
127+
exportfunctiongetEdgeVersion(){
128+
constua=navigator.userAgent.toLowerCase();
129+
constedge=ua.indexOf('edge/');
130+
if(edge===-1){
131+
return-1;
132+
}
133+
returnparseInt(ua.substring(edge+5,ua.indexOf('.',edge)),10);
134+
}

0 commit comments

Comments
 (0)