diff --git a/src/jsx-loader.js b/src/jsx-loader.js index e84aad4..370b945 100644 --- a/src/jsx-loader.js +++ b/src/jsx-loader.js @@ -38,6 +38,7 @@ export function getParser(moduleURL) { }; } +// replace all instances of __this__ marker with relative reference to the custom element parent node function applyDomDepthSubstitutions(tree, currentDepth = 1, hasShadowRoot = false) { try { for (const node of tree.childNodes) { @@ -50,9 +51,9 @@ function applyDomDepthSubstitutions(tree, currentDepth = 1, hasShadowRoot = fals const { value } = attrs[attr]; if (value.indexOf('__this__.') >= 0) { - const root = hasShadowRoot ? 'parentNode.host' : 'parentElement'; + const root = hasShadowRoot ? '.getRootNode().host' : `${'.parentElement'.repeat(currentDepth)}`; - node.attrs[attr].value = value.replace(/__this__/g, `this${'.parentElement'.repeat(currentDepth - 1)}.${root}`); + node.attrs[attr].value = value.replace(/__this__/g, `this${root}`); } } } diff --git a/test/cases/jsx-shadow-dom/jsx-shadow-dom.spec.js b/test/cases/jsx-shadow-dom/jsx-shadow-dom.spec.js index 5bdf3ee..5721c53 100644 --- a/test/cases/jsx-shadow-dom/jsx-shadow-dom.spec.js +++ b/test/cases/jsx-shadow-dom/jsx-shadow-dom.spec.js @@ -54,7 +54,7 @@ describe('Run WCC For ', function() { const wrapper = new JSDOM(heading.innerHTML); const button = wrapper.window.document.querySelector('button'); - expect(button.getAttribute('onclick')).to.be.equal('this.parentElement.parentNode.host.sayHello()'); + expect(button.getAttribute('onclick')).to.be.equal('this.getRootNode().host.sayHello()'); }); });