🐛 set the event target during dynamic script onload/onerror events (#914)
Co-authored-by: wangzongxu <wangzongxu@vipkid.com.cn> Co-authored-by: Kuitos <kuitos.lau@gmail.com>
This commit is contained in:
parent
f9c6126528
commit
447454f97c
|
|
@ -61,6 +61,18 @@ function setCachedRules(element: HTMLStyleElement, cssRules: CSSRuleList) {
|
||||||
Object.defineProperty(element, styledComponentSymbol, { value: cssRules, configurable: true, enumerable: false });
|
Object.defineProperty(element, styledComponentSymbol, { value: cssRules, configurable: true, enumerable: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function patchCustomEvent(e: CustomEvent, elementGetter: () => HTMLScriptElement | null): CustomEvent {
|
||||||
|
Object.defineProperties(e, {
|
||||||
|
srcElement: {
|
||||||
|
get: elementGetter,
|
||||||
|
},
|
||||||
|
target: {
|
||||||
|
get: elementGetter,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
function getOverwrittenAppendChildOrInsertBefore(opts: {
|
function getOverwrittenAppendChildOrInsertBefore(opts: {
|
||||||
appName: string;
|
appName: string;
|
||||||
proxy: WindowProxy;
|
proxy: WindowProxy;
|
||||||
|
|
@ -159,7 +171,7 @@ function getOverwrittenAppendChildOrInsertBefore(opts: {
|
||||||
// 2. addEventListener way, which toast-loader used, see https://github.com/pyrsmk/toast/blob/master/src/Toast.ts#L64
|
// 2. addEventListener way, which toast-loader used, see https://github.com/pyrsmk/toast/blob/master/src/Toast.ts#L64
|
||||||
const loadEvent = new CustomEvent('load');
|
const loadEvent = new CustomEvent('load');
|
||||||
if (isFunction(element.onload)) {
|
if (isFunction(element.onload)) {
|
||||||
element.onload(loadEvent);
|
element.onload(patchCustomEvent(loadEvent, () => element));
|
||||||
} else {
|
} else {
|
||||||
element.dispatchEvent(loadEvent);
|
element.dispatchEvent(loadEvent);
|
||||||
}
|
}
|
||||||
|
|
@ -169,7 +181,7 @@ function getOverwrittenAppendChildOrInsertBefore(opts: {
|
||||||
error: () => {
|
error: () => {
|
||||||
const errorEvent = new CustomEvent('error');
|
const errorEvent = new CustomEvent('error');
|
||||||
if (isFunction(element.onerror)) {
|
if (isFunction(element.onerror)) {
|
||||||
element.onerror(errorEvent);
|
element.onerror(patchCustomEvent(errorEvent, () => element));
|
||||||
} else {
|
} else {
|
||||||
element.dispatchEvent(errorEvent);
|
element.dispatchEvent(errorEvent);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user