event-target-legacy.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @license
  3. * Copyright Google Inc. All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. export function eventTargetLegacyPatch(_global: any, api: _ZonePrivate) {
  9. const {eventNames, globalSources, zoneSymbolEventNames, TRUE_STR, FALSE_STR, ZONE_SYMBOL_PREFIX} =
  10. api.getGlobalObjects()!;
  11. const WTF_ISSUE_555 =
  12. 'Anchor,Area,Audio,BR,Base,BaseFont,Body,Button,Canvas,Content,DList,Directory,Div,Embed,FieldSet,Font,Form,Frame,FrameSet,HR,Head,Heading,Html,IFrame,Image,Input,Keygen,LI,Label,Legend,Link,Map,Marquee,Media,Menu,Meta,Meter,Mod,OList,Object,OptGroup,Option,Output,Paragraph,Pre,Progress,Quote,Script,Select,Source,Span,Style,TableCaption,TableCell,TableCol,Table,TableRow,TableSection,TextArea,Title,Track,UList,Unknown,Video';
  13. const NO_EVENT_TARGET =
  14. 'ApplicationCache,EventSource,FileReader,InputMethodContext,MediaController,MessagePort,Node,Performance,SVGElementInstance,SharedWorker,TextTrack,TextTrackCue,TextTrackList,WebKitNamedFlow,Window,Worker,WorkerGlobalScope,XMLHttpRequest,XMLHttpRequestEventTarget,XMLHttpRequestUpload,IDBRequest,IDBOpenDBRequest,IDBDatabase,IDBTransaction,IDBCursor,DBIndex,WebSocket'
  15. .split(',');
  16. const EVENT_TARGET = 'EventTarget';
  17. let apis: any[] = [];
  18. const isWtf = _global['wtf'];
  19. const WTF_ISSUE_555_ARRAY = WTF_ISSUE_555.split(',');
  20. if (isWtf) {
  21. // Workaround for: https://github.com/google/tracing-framework/issues/555
  22. apis = WTF_ISSUE_555_ARRAY.map((v) => 'HTML' + v + 'Element').concat(NO_EVENT_TARGET);
  23. } else if (_global[EVENT_TARGET]) {
  24. apis.push(EVENT_TARGET);
  25. } else {
  26. // Note: EventTarget is not available in all browsers,
  27. // if it's not available, we instead patch the APIs in the IDL that inherit from EventTarget
  28. apis = NO_EVENT_TARGET;
  29. }
  30. const isDisableIECheck = _global['__Zone_disable_IE_check'] || false;
  31. const isEnableCrossContextCheck = _global['__Zone_enable_cross_context_check'] || false;
  32. const ieOrEdge = api.isIEOrEdge();
  33. const ADD_EVENT_LISTENER_SOURCE = '.addEventListener:';
  34. const FUNCTION_WRAPPER = '[object FunctionWrapper]';
  35. const BROWSER_TOOLS = 'function __BROWSERTOOLS_CONSOLE_SAFEFUNC() { [native code] }';
  36. // predefine all __zone_symbol__ + eventName + true/false string
  37. for (let i = 0; i < eventNames.length; i++) {
  38. const eventName = eventNames[i];
  39. const falseEventName = eventName + FALSE_STR;
  40. const trueEventName = eventName + TRUE_STR;
  41. const symbol = ZONE_SYMBOL_PREFIX + falseEventName;
  42. const symbolCapture = ZONE_SYMBOL_PREFIX + trueEventName;
  43. zoneSymbolEventNames[eventName] = {};
  44. zoneSymbolEventNames[eventName][FALSE_STR] = symbol;
  45. zoneSymbolEventNames[eventName][TRUE_STR] = symbolCapture;
  46. }
  47. // predefine all task.source string
  48. for (let i = 0; i < WTF_ISSUE_555.length; i++) {
  49. const target: any = WTF_ISSUE_555_ARRAY[i];
  50. const targets: any = globalSources[target] = {};
  51. for (let j = 0; j < eventNames.length; j++) {
  52. const eventName = eventNames[j];
  53. targets[eventName] = target + ADD_EVENT_LISTENER_SOURCE + eventName;
  54. }
  55. }
  56. const checkIEAndCrossContext = function(
  57. nativeDelegate: any, delegate: any, target: any, args: any) {
  58. if (!isDisableIECheck && ieOrEdge) {
  59. if (isEnableCrossContextCheck) {
  60. try {
  61. const testString = delegate.toString();
  62. if ((testString === FUNCTION_WRAPPER || testString == BROWSER_TOOLS)) {
  63. nativeDelegate.apply(target, args);
  64. return false;
  65. }
  66. } catch (error) {
  67. nativeDelegate.apply(target, args);
  68. return false;
  69. }
  70. } else {
  71. const testString = delegate.toString();
  72. if ((testString === FUNCTION_WRAPPER || testString == BROWSER_TOOLS)) {
  73. nativeDelegate.apply(target, args);
  74. return false;
  75. }
  76. }
  77. } else if (isEnableCrossContextCheck) {
  78. try {
  79. delegate.toString();
  80. } catch (error) {
  81. nativeDelegate.apply(target, args);
  82. return false;
  83. }
  84. }
  85. return true;
  86. };
  87. const apiTypes: any[] = [];
  88. for (let i = 0; i < apis.length; i++) {
  89. const type = _global[apis[i]];
  90. apiTypes.push(type && type.prototype);
  91. }
  92. // vh is validateHandler to check event handler
  93. // is valid or not(for security check)
  94. api.patchEventTarget(_global, apiTypes, {vh: checkIEAndCrossContext});
  95. (Zone as any)[api.symbol('patchEventTarget')] = !!_global[EVENT_TARGET];
  96. return true;
  97. }
  98. export function patchEvent(global: any, api: _ZonePrivate) {
  99. api.patchEventPrototype(global, api);
  100. }