Skip to content

Commit d17f5c7

Browse files
fix: add check for is attribute (#15086)
Fixes #15085 Add a check for is attribute in is_custom_element_node function.
1 parent 7740d45 commit d17f5c7

File tree

6 files changed

+35
-4
lines changed

6 files changed

+35
-4
lines changed

.changeset/two-doors-exercise.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: add check for `is` attribute to correctly detect custom elements

packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export function RegularElement(node, context) {
227227
node_id,
228228
attributes_id,
229229
(node.metadata.svg || node.metadata.mathml || is_custom_element_node(node)) && b.true,
230-
node.name.includes('-') && b.true,
230+
is_custom_element_node(node) && b.true,
231231
context.state
232232
);
233233

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { cannot_be_set_statically } from '../../../../../../utils.js';
55
import { is_event_attribute, is_text_attribute } from '../../../../../utils/ast.js';
66
import * as b from '../../../../../utils/builders.js';
7+
import { is_custom_element_node } from '../../../../nodes.js';
78
import { build_template_chunk } from './utils.js';
89

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

133134
for (const attribute of node.attributes) {
134135
if (attribute.type !== 'Attribute') {

packages/svelte/src/compiler/phases/nodes.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ export function is_element_node(node) {
2323

2424
/**
2525
* @param {AST.RegularElement | AST.SvelteElement} node
26-
* @returns {node is AST.RegularElement}
26+
* @returns {boolean}
2727
*/
2828
export function is_custom_element_node(node) {
29-
return node.type === 'RegularElement' && node.name.includes('-');
29+
return (
30+
node.type === 'RegularElement' &&
31+
(node.name.includes('-') ||
32+
node.attributes.some((attr) => attr.type === 'Attribute' && attr.name === 'is'))
33+
);
3034
}
3135

3236
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
mode: ['client', 'server'],
5+
async test({ assert, target }) {
6+
const my_element = /** @type HTMLElement & { object: { test: true }; } */ (
7+
target.querySelector('my-element')
8+
);
9+
const my_link = /** @type HTMLAnchorElement & { object: { test: true }; } */ (
10+
target.querySelector('a')
11+
);
12+
assert.equal(my_element.getAttribute('string'), 'test');
13+
assert.equal(my_element.hasAttribute('object'), false);
14+
assert.deepEqual(my_element.object, { test: true });
15+
assert.equal(my_link.getAttribute('string'), 'test');
16+
assert.equal(my_link.hasAttribute('object'), false);
17+
assert.deepEqual(my_link.object, { test: true });
18+
}
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<my-element string="test" object={{ test: true }}></my-element>
2+
<a is="my-link" string="test" object={{ test: true }}></a>

0 commit comments

Comments
 (0)