Skip to content

Commit

Permalink
fix: add check for is attribute (#15086)
Browse files Browse the repository at this point in the history
Fixes #15085

Add a check for is attribute in is_custom_element_node function.
  • Loading branch information
edoardocavazza authored Jan 22, 2025
1 parent 7740d45 commit d17f5c7
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-doors-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: add check for `is` attribute to correctly detect custom elements
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export function RegularElement(node, context) {
node_id,
attributes_id,
(node.metadata.svg || node.metadata.mathml || is_custom_element_node(node)) && b.true,
node.name.includes('-') && b.true,
is_custom_element_node(node) && b.true,
context.state
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { cannot_be_set_statically } from '../../../../../../utils.js';
import { is_event_attribute, is_text_attribute } from '../../../../../utils/ast.js';
import * as b from '../../../../../utils/builders.js';
import { is_custom_element_node } from '../../../../nodes.js';
import { build_template_chunk } from './utils.js';

/**
Expand Down Expand Up @@ -128,7 +129,7 @@ export function process_children(nodes, initial, is_element, { visit, state }) {
function is_static_element(node, state) {
if (node.type !== 'RegularElement') return false;
if (node.fragment.metadata.dynamic) return false;
if (node.name.includes('-')) return false; // we're setting all attributes on custom elements through properties
if (is_custom_element_node(node)) return false; // we're setting all attributes on custom elements through properties

for (const attribute of node.attributes) {
if (attribute.type !== 'Attribute') {
Expand Down
8 changes: 6 additions & 2 deletions packages/svelte/src/compiler/phases/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ export function is_element_node(node) {

/**
* @param {AST.RegularElement | AST.SvelteElement} node
* @returns {node is AST.RegularElement}
* @returns {boolean}
*/
export function is_custom_element_node(node) {
return node.type === 'RegularElement' && node.name.includes('-');
return (
node.type === 'RegularElement' &&
(node.name.includes('-') ||
node.attributes.some((attr) => attr.type === 'Attribute' && attr.name === 'is'))
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { test } from '../../test';

export default test({
mode: ['client', 'server'],
async test({ assert, target }) {
const my_element = /** @type HTMLElement & { object: { test: true }; } */ (
target.querySelector('my-element')
);
const my_link = /** @type HTMLAnchorElement & { object: { test: true }; } */ (
target.querySelector('a')
);
assert.equal(my_element.getAttribute('string'), 'test');
assert.equal(my_element.hasAttribute('object'), false);
assert.deepEqual(my_element.object, { test: true });
assert.equal(my_link.getAttribute('string'), 'test');
assert.equal(my_link.hasAttribute('object'), false);
assert.deepEqual(my_link.object, { test: true });
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<my-element string="test" object={{ test: true }}></my-element>
<a is="my-link" string="test" object={{ test: true }}></a>

0 comments on commit d17f5c7

Please sign in to comment.