From 94fc5945d1738426abcd249b10639a39e7ab131a Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Tue, 2 Jan 2024 16:37:12 -0500 Subject: [PATCH 01/11] fix shadow root rendering for JSX --- src/jsx-loader.js | 7 +- .../jsx-shadow-dom/jsx-shadow-dom.spec.js | 80 +++++++++++++++++++ test/cases/jsx-shadow-dom/src/heading.jsx | 20 +++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 test/cases/jsx-shadow-dom/jsx-shadow-dom.spec.js create mode 100644 test/cases/jsx-shadow-dom/src/heading.jsx diff --git a/src/jsx-loader.js b/src/jsx-loader.js index bef481b..413ea0b 100644 --- a/src/jsx-loader.js +++ b/src/jsx-loader.js @@ -265,7 +265,12 @@ export function parseJsx(moduleURL) { applyDomDepthSubstitutions(elementTree, undefined, hasShadowRoot); - const finalHtml = serialize(elementTree); + const serializedHtml = serialize(elementTree); + // would be nice to reuse HTMLTemplateElement here... + const finalHtml = hasShadowRoot + ? `` + : serializedHtml; + const transformed = acorn.parse(`${elementRoot}.innerHTML = \`${finalHtml}\`;`, { ecmaVersion: 'latest', sourceType: 'module' diff --git a/test/cases/jsx-shadow-dom/jsx-shadow-dom.spec.js b/test/cases/jsx-shadow-dom/jsx-shadow-dom.spec.js new file mode 100644 index 0000000..8fd3018 --- /dev/null +++ b/test/cases/jsx-shadow-dom/jsx-shadow-dom.spec.js @@ -0,0 +1,80 @@ +/* + * Use Case + * Run wcc against a nested custom elements using JSX render function and Declarative Shadow DOM. + * + * User Result + * Should return the expected HTML and JavaScript output. + * + * User Workspace + * src/ + * heading.jsx + */ +import chai from 'chai'; +import { JSDOM } from 'jsdom'; +import { renderToString } from '../../../src/wcc.js'; + +const expect = chai.expect; + +describe('Run WCC For ', function() { + const LABEL = 'Single Custom Element using JSX and Declarative Shadow DOM'; + let dom; + let meta; + + before(async function() { + const { html, metadata } = await renderToString(new URL('./src/heading.jsx', import.meta.url)); + + meta = metadata; + dom = new JSDOM(html); + }); + + describe(LABEL, function() { + + describe(' component', function() { + let heading; + + before(async function() { + heading = dom.window.document.querySelector('wcc-heading template[shadowrootmode="open"]'); + }); + + describe('Metadata', () => { + it('should return a JSX definition in metadata', () => { + expect(Object.keys(meta).length).to.equal(1); + expect(meta['wcc-heading'].source).to.not.be.undefined; + }); + }); + + describe('Declarative Shadow DOM (