This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(toString): fix #666, Zone patched method toString should like bef…
…ore patched
- Loading branch information
1 parent
5c4e24d
commit c1a2175
Showing
7 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
import {zoneSymbol} from './utils'; | ||
|
||
// override Function.prototype.toString to make zone.js patched function | ||
// look like native function | ||
export function patchFuncToString() { | ||
const originalFunctionToString = Function.prototype.toString; | ||
const g: any = | ||
typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global; | ||
Function.prototype.toString = function() { | ||
if (typeof this === 'function') { | ||
if (this[zoneSymbol('OriginalDelegate')]) { | ||
return originalFunctionToString.apply(this[zoneSymbol('OriginalDelegate')], arguments); | ||
} | ||
if (this === Promise) { | ||
const nativePromise = g[zoneSymbol('Promise')]; | ||
if (nativePromise) { | ||
return originalFunctionToString.apply(nativePromise, arguments); | ||
} | ||
} | ||
if (this === Error) { | ||
const nativeError = g[zoneSymbol('Error')]; | ||
if (nativeError) { | ||
return originalFunctionToString.apply(nativeError, arguments); | ||
} | ||
} | ||
} | ||
return originalFunctionToString.apply(this, arguments); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {zoneSymbol} from '../../lib/common/utils'; | ||
import {ifEnvSupports} from '../test-util'; | ||
|
||
const zoneSymbolString = '__zone_symbol__'; | ||
const g: any = | ||
typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global; | ||
describe('global function patch', () => { | ||
describe('isOriginal', () => { | ||
it('setTimeout toString should be the same with non patched setTimeout', () => { | ||
expect(Function.prototype.toString.call(setTimeout)) | ||
.toEqual(Function.prototype.toString.call(g[zoneSymbol('setTimeout')])); | ||
}); | ||
}); | ||
|
||
describe('isNative', () => { | ||
it('ZoneAwareError toString should look like native', () => { | ||
expect(Function.prototype.toString.call(Error)).toContain('[native code]'); | ||
}); | ||
|
||
it('EventTarget addEventListener should look like native', ifEnvSupports('HTMLElement', () => { | ||
expect(Function.prototype.toString.call(HTMLElement.prototype.addEventListener)) | ||
.toContain('[native code]'); | ||
})); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters