From f9a00f71479956cf196eefb6ebba672c9a80ba26 Mon Sep 17 00:00:00 2001 From: Tito Date: Mon, 9 Sep 2024 18:05:12 -0300 Subject: [PATCH] add an aditional check for `is` in element attributes/props for custom elements (#348) --- .../babel-plugin-jsx-dom-expressions/src/dom/element.js | 6 +++--- packages/dom-expressions/src/client.js | 8 ++++---- packages/lit-dom-expressions/src/index.ts | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/babel-plugin-jsx-dom-expressions/src/dom/element.js b/packages/babel-plugin-jsx-dom-expressions/src/dom/element.js index 6aa0c920..c8090325 100644 --- a/packages/babel-plugin-jsx-dom-expressions/src/dom/element.js +++ b/packages/babel-plugin-jsx-dom-expressions/src/dom/element.js @@ -61,7 +61,7 @@ export function transformElement(path, info) { config = getConfig(path), wrapSVG = info.topLevel && tagName != "svg" && SVGElements.has(tagName), voidTag = VoidElements.indexOf(tagName) > -1, - isCustomElement = tagName.indexOf("-") > -1 || !!path.get("openingElement").get("attributes").find(a => a.node.name?.name === "is"), + isCustomElement = tagName.indexOf("-") > -1 || path.get("openingElement").get("attributes").some(a => a.node?.name?.name === "is" || a.name?.name === "is"), isImportNode = (tagName === 'img'||tagName === 'iframe') && path.get("openingElement").get("attributes").some(a => a.node.name?.name === "loading" && a.node.value?.value === "lazy" ), results = { @@ -273,7 +273,7 @@ function transformAttributes(path, results) { attributes = path.get("openingElement").get("attributes"); const tagName = getTagName(path.node), isSVG = SVGElements.has(tagName), - isCE = tagName.includes("-"), + isCE = tagName.includes("-") || attributes.some(a => a.node.name?.name === 'is'), hasChildren = path.node.children.length > 0, config = getConfig(path); @@ -1000,7 +1000,7 @@ function detectExpressions(children, index, config) { } else if (t.isJSXElement(child)) { const tagName = getTagName(child); if (isComponent(tagName)) return true; - if (config.contextToCustomElements && (tagName === "slot" || tagName.indexOf("-") > -1)) + if (config.contextToCustomElements && (tagName === "slot" || tagName.indexOf("-") > -1 || child.openingElement.attributes.some(a => a.name?.name === 'is'))) return true; if ( child.openingElement.attributes.some( diff --git a/packages/dom-expressions/src/client.js b/packages/dom-expressions/src/client.js index dba46f3e..dc9ade46 100644 --- a/packages/dom-expressions/src/client.js +++ b/packages/dom-expressions/src/client.js @@ -214,7 +214,7 @@ export function assign(node, props, isSVG, skipChildren, prevProps = {}, skipRef for (const prop in prevProps) { if (!(prop in props)) { if (prop === "children") continue; - prevProps[prop] = assignProp(node, prop, null, prevProps[prop], isSVG, skipRef); + prevProps[prop] = assignProp(node, prop, null, prevProps[prop], isSVG, skipRef, props); } } for (const prop in props) { @@ -223,7 +223,7 @@ export function assign(node, props, isSVG, skipChildren, prevProps = {}, skipRef continue; } const value = props[prop]; - prevProps[prop] = assignProp(node, prop, value, prevProps[prop], isSVG, skipRef); + prevProps[prop] = assignProp(node, prop, value, prevProps[prop], isSVG, skipRef, props); } } @@ -323,7 +323,7 @@ function toggleClassKey(node, key, value) { node.classList.toggle(classNames[i], value); } -function assignProp(node, prop, value, prev, isSVG, skipRef) { +function assignProp(node, prop, value, prev, isSVG, skipRef, props) { let isCE, isProp, isChildProp, propAlias, forceProp; if (prop === "style") return style(node, value, prev); if (prop === "classList") return classList(node, value, prev); @@ -356,7 +356,7 @@ function assignProp(node, prop, value, prev, isSVG, skipRef) { (isChildProp = ChildProperties.has(prop)) || (!isSVG && ((propAlias = getPropAlias(prop, node.tagName)) || (isProp = Properties.has(prop)))) || - (isCE = node.nodeName.includes("-")) + (isCE = (node.nodeName.includes("-") || 'is' in props)) ) { if (forceProp) { prop = prop.slice(5); diff --git a/packages/lit-dom-expressions/src/index.ts b/packages/lit-dom-expressions/src/index.ts index caf20641..3a3392e6 100644 --- a/packages/lit-dom-expressions/src/index.ts +++ b/packages/lit-dom-expressions/src/index.ts @@ -432,7 +432,7 @@ export function createHTML(r: Runtime, { delegateEvents = true, functionBuilder topDecl ? "" : `${tag} = ${options.path}.${options.first ? "firstChild" : "nextSibling"}` ); const isSVG = r.SVGElements.has(node.name); - const isCE = node.name.includes("-"); + const isCE = node.name.includes("-") || node.attrs.some((e) => e.name === "is"); options.hasCustomElement = isCE; options.isImportNode = (node.name === 'img'||node.name === 'iframe') && node.attrs.some((e) => e.name === "loading" && e.value ==='lazy');