Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sandbox): add strictDomSelector config #658

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/browser-vm/src/pluginify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function createOptions(Garfish: interfaces.Garfish) {
baseUrl: appInstance.entryManager.url,
modules: compatibleOldModule(appInfo.sandbox?.modules || []),
fixBaseUrl: Boolean(appInfo.sandbox?.fixBaseUrl),
strictDomSelector: Boolean(appInfo.sandbox?.strictDomSelector),
fixStaticResourceBaseUrl: Boolean(
appInfo.sandbox?.fixStaticResourceBaseUrl,
),
Expand Down
20 changes: 16 additions & 4 deletions packages/browser-vm/src/proxyInterceptor/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

const rootNode = rootElm(sandbox);
const strictIsolation = sandbox.options.strictIsolation;
const strictDomSelector = sandbox.options.strictDomSelector;
const value = hasOwn(target, p)
? Reflect.get(target, p, receiver)
: Reflect.get(document, p);
Expand Down Expand Up @@ -81,15 +82,26 @@
}

// rootNode is a Shadow dom
if (strictIsolation) {
if (strictIsolation || strictDomSelector) {
if (p === 'body') {
// When the node is inserted, if it is a pop-up scene,
// it needs to be placed globally, so it is not placed outside by default.
return findTarget(rootNode, ['body', `div[${__MockBody__}]`]);
} else if (queryFunctions(p)) {
return p === 'getElementById'
? (id) => rootNode.querySelector(`#${id}`)
: rootNode[p].bind(rootNode);
if (p === 'querySelector') {
return (selector) => {
if (selector === 'body') {
return findTarget(rootNode, ['body', `div[${__MockBody__}]`]);
} else if (selector === 'head') {
return findTarget(rootNode, ['head', `div[${__MockHead__}]`]);
}
return rootNode.querySelector(selector);
};
}
if (p === 'getElementById') {
return (id) => rootNode.querySelector(`#${id}`);
}
return rootNode[p].bind(rootNode);

Check warning on line 104 in packages/browser-vm/src/proxyInterceptor/document.ts

View check run for this annotation

Codecov / codecov/patch

packages/browser-vm/src/proxyInterceptor/document.ts#L91-L104

Added lines #L91 - L104 were not covered by tests
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/browser-vm/src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class Sandbox {
fixStaticResourceBaseUrl: true,
disableWith: false,
strictIsolation: false,
strictDomSelector: false,
disableCollect: false,
el: () => null,
styleScopeId: () => '',
Expand Down
1 change: 1 addition & 0 deletions packages/browser-vm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface SandboxOptions {
disableWith?: boolean;
strictIsolation?: boolean;
disableElementtiming?: boolean;
strictDomSelector?: boolean;
disableCollect?: boolean;
modules?: Array<Module>;
addSourceList?: (
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export const createDefaultOptions = () => {
fixStaticResourceBaseUrl: true,
disableWith: false,
strictIsolation: false,
strictDomSelector: false,
disableElementtiming: false,
fixOwnerDocument: false,
},
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export namespace interfaces {
fixStaticResourceBaseUrl?: boolean;
disableWith?: boolean;
strictIsolation?: boolean;
strictDomSelector?: boolean;
disableElementtiming?: boolean;
fixOwnerDocument?: boolean;
}
Expand Down
1 change: 0 additions & 1 deletion packages/utils/src/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ export function filterAndWrapEventListener(
// Through error stack source file, and the source of the static resources determine whether the current error belongs to the current application
// if belong to the current application environment monitoring error
if (sourceList) {
console.log('**********', computeErrorUrl(e));
const res = sourceList.find((item) => {
return item.indexOf(computeErrorUrl(e)) !== -1;
});
Expand Down
Loading
Loading