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: Rendering for multiple nested head and body nodes #812

Merged
merged 2 commits into from
Jul 21, 2020
Merged
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
4 changes: 4 additions & 0 deletions src/core/vdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,10 @@ export function renderer(renderer: () => RenderResult): Renderer {
while (!insertBefore) {
const nextSibling = _wrapperSiblingMap.get(searchNode);
if (nextSibling) {
if (isBodyWrapper(nextSibling) || isHeadWrapper(nextSibling)) {
searchNode = nextSibling;
continue;
}
let domNode = nextSibling.domNode;
if (isWNodeWrapper(nextSibling) || isVirtualWrapper(nextSibling)) {
if (!nextSibling.childDomWrapperId) {
Expand Down
136 changes: 136 additions & 0 deletions tests/core/unit/vdom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4848,6 +4848,74 @@ jsdomDescribe('vdom', () => {
results = document.querySelectorAll('.body-span');
assert.lengthOf(results, 0);
});

it('should support attaching body nodes in nested widgets', () => {
const factory = create({ icache }).properties<any>();

const Foo = factory(function Foo() {
return (
<div id="wrapper-2">
<body>
<div id="body-2" />
</body>
</div>
);
});

const Bar = factory(function Bar({ properties }) {
return (
<div id="wrapper-1">
<Foo close={() => properties().close()} />
<body>
<div id="body-1" />
</body>
</div>
);
});

const App = factory(function App({ middleware: { icache } }) {
const show = icache.getOrSet('show', false);
return (
<div>
<button
onclick={() => {
icache.set('show', (current) => !current);
}}
>
Open/Close
</button>
{show && (
<Bar
close={() => {
icache.set('show', false);
}}
/>
)}
<h2>Start editing to see some magic happen</h2>
</div>
);
});

const r = renderer(() => w(App, {}));
r.mount({ domNode: root });
(root.children[0].children[0] as any).click();
resolvers.resolve();
assert.strictEqual(
root.innerHTML,
'<div><button>Open/Close</button><div id="wrapper-1"><div id="wrapper-2"></div></div><h2>Start editing to see some magic happen</h2></div>'
);
assert.lengthOf(document.querySelectorAll('#body-1'), 1);
assert.lengthOf(document.querySelectorAll('#body-2'), 1);
(root.children[0].children[0] as any).click();
resolvers.resolve();
resolvers.resolve();
assert.strictEqual(
root.innerHTML,
'<div><button>Open/Close</button><h2>Start editing to see some magic happen</h2></div>'
);
assert.lengthOf(document.querySelectorAll('#body-1'), 0);
assert.lengthOf(document.querySelectorAll('#body-2'), 0);
});
});

describe('head node', () => {
Expand Down Expand Up @@ -5100,6 +5168,74 @@ jsdomDescribe('vdom', () => {
results = document.querySelectorAll('.head-span');
assert.lengthOf(results, 0);
});

it('should support attaching head nodes in nested widgets', () => {
const factory = create({ icache }).properties<any>();

const Foo = factory(function Foo() {
return (
<div id="wrapper-2">
<head>
<div id="head-2" />
</head>
</div>
);
});

const Bar = factory(function Bar({ properties }) {
return (
<div id="wrapper-1">
<Foo close={() => properties().close()} />
<head>
<div id="head-1" />
</head>
</div>
);
});

const App = factory(function App({ middleware: { icache } }) {
const show = icache.getOrSet('show', false);
return (
<div>
<button
onclick={() => {
icache.set('show', (current) => !current);
}}
>
Open/Close
</button>
{show && (
<Bar
close={() => {
icache.set('show', false);
}}
/>
)}
<h2>Start editing to see some magic happen</h2>
</div>
);
});

const r = renderer(() => w(App, {}));
r.mount({ domNode: root });
(root.children[0].children[0] as any).click();
resolvers.resolve();
assert.strictEqual(
root.innerHTML,
'<div><button>Open/Close</button><div id="wrapper-1"><div id="wrapper-2"></div></div><h2>Start editing to see some magic happen</h2></div>'
);
assert.lengthOf(document.querySelectorAll('#head-1'), 1);
assert.lengthOf(document.querySelectorAll('#head-2'), 1);
(root.children[0].children[0] as any).click();
resolvers.resolve();
resolvers.resolve();
assert.strictEqual(
root.innerHTML,
'<div><button>Open/Close</button><h2>Start editing to see some magic happen</h2></div>'
);
assert.lengthOf(document.querySelectorAll('#head-1'), 0);
assert.lengthOf(document.querySelectorAll('#head-2'), 0);
});
});

describe('virtual node', () => {
Expand Down