🐛 support Node.prototype.compareDocumentPosition in sandbox (#2503)
This commit is contained in:
parent
8440aa5483
commit
ad23bbf459
|
|
@ -44,11 +44,7 @@ const proxyAttachContainerConfigMap: WeakMap<WindowProxy, ContainerConfig> =
|
||||||
|
|
||||||
const elementAttachContainerConfigMap = new WeakMap<HTMLElement, ContainerConfig>();
|
const elementAttachContainerConfigMap = new WeakMap<HTMLElement, ContainerConfig>();
|
||||||
const docCreatePatchedMap = new WeakMap<typeof document.createElement, typeof document.createElement>();
|
const docCreatePatchedMap = new WeakMap<typeof document.createElement, typeof document.createElement>();
|
||||||
const mutationObserverPatchedMap = new WeakMap<
|
const patchMap = new WeakMap<any, any>();
|
||||||
typeof MutationObserver.prototype.observe,
|
|
||||||
typeof MutationObserver.prototype.observe
|
|
||||||
>();
|
|
||||||
const parentNodePatchedMap = new WeakMap<PropertyDescriptor, PropertyDescriptor>();
|
|
||||||
|
|
||||||
function patchDocument(cfg: { sandbox: SandBox; speedy: boolean }) {
|
function patchDocument(cfg: { sandbox: SandBox; speedy: boolean }) {
|
||||||
const { sandbox, speedy } = cfg;
|
const { sandbox, speedy } = cfg;
|
||||||
|
|
@ -129,20 +125,30 @@ function patchDocument(cfg: { sandbox: SandBox; speedy: boolean }) {
|
||||||
// patch MutationObserver.prototype.observe to avoid type error
|
// patch MutationObserver.prototype.observe to avoid type error
|
||||||
// https://github.com/umijs/qiankun/issues/2406
|
// https://github.com/umijs/qiankun/issues/2406
|
||||||
const nativeMutationObserverObserveFn = MutationObserver.prototype.observe;
|
const nativeMutationObserverObserveFn = MutationObserver.prototype.observe;
|
||||||
if (!mutationObserverPatchedMap.has(nativeMutationObserverObserveFn)) {
|
if (!patchMap.has(nativeMutationObserverObserveFn)) {
|
||||||
const observe = function observe(this: MutationObserver, target: Node, options: MutationObserverInit) {
|
const observe = function observe(this: MutationObserver, target: Node, options: MutationObserverInit) {
|
||||||
const realTarget = target instanceof Document ? nativeDocument : target;
|
const realTarget = target instanceof Document ? nativeDocument : target;
|
||||||
return nativeMutationObserverObserveFn.call(this, realTarget, options);
|
return nativeMutationObserverObserveFn.call(this, realTarget, options);
|
||||||
};
|
};
|
||||||
|
|
||||||
MutationObserver.prototype.observe = observe;
|
MutationObserver.prototype.observe = observe;
|
||||||
mutationObserverPatchedMap.set(nativeMutationObserverObserveFn, observe);
|
patchMap.set(nativeMutationObserverObserveFn, observe);
|
||||||
|
}
|
||||||
|
|
||||||
|
// patch Node.prototype.compareDocumentPosition to avoid type error
|
||||||
|
const prevCompareDocumentPosition = Node.prototype.compareDocumentPosition;
|
||||||
|
if (!patchMap.has(prevCompareDocumentPosition)) {
|
||||||
|
Node.prototype.compareDocumentPosition = function compareDocumentPosition(this: Node, node) {
|
||||||
|
const realNode = node instanceof Document ? nativeDocument : node;
|
||||||
|
return prevCompareDocumentPosition.call(this, realNode);
|
||||||
|
};
|
||||||
|
patchMap.set(prevCompareDocumentPosition, Node.prototype.compareDocumentPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
// patch parentNode getter to avoid document === html.parentNode
|
// patch parentNode getter to avoid document === html.parentNode
|
||||||
// https://github.com/umijs/qiankun/issues/2408#issuecomment-1446229105
|
// https://github.com/umijs/qiankun/issues/2408#issuecomment-1446229105
|
||||||
const parentNodeDescriptor = Object.getOwnPropertyDescriptor(Node.prototype, 'parentNode');
|
const parentNodeDescriptor = Object.getOwnPropertyDescriptor(Node.prototype, 'parentNode');
|
||||||
if (parentNodeDescriptor && !parentNodePatchedMap.has(parentNodeDescriptor)) {
|
if (parentNodeDescriptor && !patchMap.has(parentNodeDescriptor)) {
|
||||||
const { get: parentNodeGetter, configurable } = parentNodeDescriptor;
|
const { get: parentNodeGetter, configurable } = parentNodeDescriptor;
|
||||||
if (parentNodeGetter && configurable) {
|
if (parentNodeGetter && configurable) {
|
||||||
const patchedParentNodeDescriptor = {
|
const patchedParentNodeDescriptor = {
|
||||||
|
|
@ -161,17 +167,20 @@ function patchDocument(cfg: { sandbox: SandBox; speedy: boolean }) {
|
||||||
};
|
};
|
||||||
Object.defineProperty(Node.prototype, 'parentNode', patchedParentNodeDescriptor);
|
Object.defineProperty(Node.prototype, 'parentNode', patchedParentNodeDescriptor);
|
||||||
|
|
||||||
parentNodePatchedMap.set(parentNodeDescriptor, patchedParentNodeDescriptor);
|
patchMap.set(parentNodeDescriptor, patchedParentNodeDescriptor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
MutationObserver.prototype.observe = nativeMutationObserverObserveFn;
|
MutationObserver.prototype.observe = nativeMutationObserverObserveFn;
|
||||||
mutationObserverPatchedMap.delete(nativeMutationObserverObserveFn);
|
patchMap.delete(nativeMutationObserverObserveFn);
|
||||||
|
|
||||||
|
Node.prototype.compareDocumentPosition = prevCompareDocumentPosition;
|
||||||
|
patchMap.delete(prevCompareDocumentPosition);
|
||||||
|
|
||||||
if (parentNodeDescriptor) {
|
if (parentNodeDescriptor) {
|
||||||
Object.defineProperty(Node.prototype, 'parentNode', parentNodeDescriptor);
|
Object.defineProperty(Node.prototype, 'parentNode', parentNodeDescriptor);
|
||||||
parentNodePatchedMap.delete(parentNodeDescriptor);
|
patchMap.delete(parentNodeDescriptor);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user