Skip to content

Commit

Permalink
fix(runtime): apply textnodes to shadow DOM instead of light DOM (#4946)
Browse files Browse the repository at this point in the history
* fix(runtime): apply textnodes to shadow DOM instead of light dom

fixes #4231

By default the `vdomRender` build flag is `false`. The Stencil parser detects any usage of a `h` function, this flag will be switched. In the component provided by author of the bug there hasn't been any vDOM to be parsed, therefor there was no usage of the function `h`.

Now, in `callRender` if we end up not having to render any vDOM we used to just attach the string (can also be a boolean or number) as text to the host element. This however doesn't work when a shadow DOM is registered for the component. In this case the text content is added to the light dom which is not being rendered.

To fix this we check if the component has a shadow DOM if attach the text node to that node.

* fix unit test

* use component meta flags to detect shadow dom

---------

Co-authored-by: Hans Claasen <no-reply@github.com>
Co-authored-by: Christian Bromann <git@bromann.dev>
  • Loading branch information
3 people committed Nov 17, 2023
1 parent ca53dbb commit 217d588
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/runtime/test/render-text.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ describe('render-text', () => {
`);
});

@Component({ tag: 'cmp-a', shadow: true })
class CmpAShadow {
render() {
return 'Hello World';
}
}

it('Hello World, innerHTML, await waitForChanges, shadow component', async () => {
const { body, waitForChanges } = await newSpecPage({
components: [CmpAShadow],
});

body.innerHTML = `<cmp-a></cmp-a>`;
await waitForChanges();

expect(body).toEqualHtml(`
<cmp-a>
<mock:shadow-root>
Hello World
</mock:shadow-root>
</cmp-a>
`);
});

it('Hello World, innerHTML, await waitForChanges', async () => {
const { body, waitForChanges } = await newSpecPage({
components: [CmpA],
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/update-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,12 @@ const callRender = (hostRef: d.HostRef, instance: any, elm: HTMLElement, isIniti
renderVdom(hostRef, instance, isInitialLoad);
}
} else {
elm.textContent = instance;
const shadowRoot = elm.shadowRoot;
if (hostRef.$cmpMeta$.$flags$ & CMP_FLAGS.shadowDomEncapsulation) {
shadowRoot.textContent = instance;
} else {
elm.textContent = instance;
}
}
}
} catch (e) {
Expand Down

0 comments on commit 217d588

Please sign in to comment.