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

fix: add owner document config #632

Open
wants to merge 6 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 cypress/e2e/2-the-vm-sandbox/dom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('whole process vm sandbox set variable', () => {
disablePreloadApp: true,
sandbox: {
snapshot: false,
fixStaticResourceBaseUrl: true,
},
protectVariable: ['dynamicScriptOnloadTag', 'dynamicScriptOnerrorTag'],
},
Expand Down
5 changes: 3 additions & 2 deletions cypress/e2e/2-the-vm-sandbox/set-global-variable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('whole process vm sandbox set variable', () => {
sandbox: {
snapshot: false,
fixBaseUrl: true,
fixOwnerDocument: true,
},
},
});
Expand Down Expand Up @@ -70,12 +71,12 @@ describe('whole process vm sandbox set variable', () => {
cy.contains('[data-test=title]', ProxyVariableTitle).then(() => {
expect(
win.document
.querySelector('[data-test=iframe-pre-fix]')
.querySelector('[data-test=iframe-pre-fix-16]')
.getAttribute('src'),
).to.equal(`http:${getPublicPath('dev/react16')}iframe`);
expect(
win.document
.querySelector('[data-test=img-pre-fix]')
.querySelector('[data-test=img-pre-fix-16]')
.getAttribute('src'),
).to.equal(`http:${getPublicPath('dev/react16')}img`);
});
Expand Down
2 changes: 2 additions & 0 deletions dev/app-main/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ let defaultConfig: RunInfo = {
// 并且子应用的查询和添加行为仅会在 DOM 作用域内进行
strictIsolation: false,
fixBaseUrl: true,
fixStaticResourceBaseUrl: true,
fixOwnerDocument: true,

// 覆盖子应用的执行上下文,使用自定义的执行上下文,例如子应用 localStorage 使用当前主应用 localStorage
// 仅在 snapshot: false 时有效
Expand Down
23 changes: 18 additions & 5 deletions dev/app-react-16/src/sandbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,25 @@ export default function () {
add script test onerror
</Button>
<div className={'instanceof'}>
<div data-test={"document-instanceof"}>document instanceof Document: {String(document instanceof Document)}</div>
<div data-test={'document-parentNode'}>document.body.parentNode?.parentNode: {String(document === document.body.parentNode?.parentNode)}</div>
<div data-test={'document-instanceof'}>
document instanceof Document: {String(document instanceof Document)}
</div>
<div data-test={'document-parentNode'}>
document.body.parentNode?.parentNode:{' '}
{String(document === document.body.parentNode?.parentNode)}
</div>
</div>
<div className='pre-fix'>
<iframe data-test={'iframe-pre-fix'} src="/iframe" style={{display: 'none'}} />
<img data-test={'img-pre-fix'} src="/img" style={{display: 'none'}} />
<div className="pre-fix">
<iframe
data-test={'iframe-pre-fix-16'}
src="/iframe"
style={{ display: 'none' }}
/>
<img
data-test={'img-pre-fix-16'}
src="/img"
style={{ display: 'none' }}
/>
</div>
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/browser-vm/src/dynamicNode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ export function makeElInjector(sandboxConfig: SandboxOptions) {

if (typeof window.Element === 'function') {
// iframe can read html container this can't point to proxyDocument has Illegal invocation error
if (sandboxConfig.fixBaseUrl) safeWrapper(() => handleOwnerDocument());
if (sandboxConfig.fixOwnerDocument)
safeWrapper(() => handleOwnerDocument());
const rewrite = (
methods: Array<string>,
builder: typeof injector | typeof injectorRemoveChild,
Expand Down
1 change: 1 addition & 0 deletions packages/browser-vm/src/pluginify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ function createOptions(Garfish: interfaces.Garfish) {
fixStaticResourceBaseUrl: Boolean(
appInfo.sandbox?.fixStaticResourceBaseUrl,
),
fixOwnerDocument: Boolean(appInfo.sandbox?.fixOwnerDocument),
disableWith: Boolean(appInfo.sandbox?.disableWith),
disableElementtiming: Boolean(appInfo.sandbox?.disableElementtiming),
strictIsolation: Boolean(appInfo.sandbox?.strictIsolation),
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 @@ -23,6 +23,7 @@ export interface SandboxOptions {
baseUrl?: string;
fixBaseUrl?: boolean;
fixStaticResourceBaseUrl?: boolean;
fixOwnerDocument?: boolean;
disableWith?: boolean;
strictIsolation?: boolean;
disableElementtiming?: boolean;
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 @@ -114,6 +114,7 @@ export const createDefaultOptions = () => {
disableWith: false,
strictIsolation: false,
disableElementtiming: false,
fixOwnerDocument: false,
},
// global hooks
beforeLoad: () => {},
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 @@ -81,6 +81,7 @@ export namespace interfaces {
disableWith?: boolean;
strictIsolation?: boolean;
disableElementtiming?: boolean;
fixOwnerDocument?: boolean;
}

export interface Config {
Expand Down
2 changes: 2 additions & 0 deletions website/src/components/config/_sandbox.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface SandboxConfig {
modules?: Array<Module> | Record<string, Module>;
// disableElementtiming 1.14.4 版本提供,默认值为 false,将会给子应用元素注入 elementtiming 属性,可以通过此属性获取子应用元素的加载时间
disableElementtiming?: boolean;
// fixOwnerDocument 1.17.0 版本提供,默认值为 false,sandbox 在针对 Element 的 ownerDocument 会发生逃逸,fixOwnerDocument 设置为 true 会避免逃逸
fixOwnerDocument?: boolean;
}

type Module = (sandbox: Sandbox) => OverridesData | void;
Expand Down