Skip to content

Commit 4e079b5

Browse files
committed
fix: add css hash to custom element rendered with svelte:element
1 parent 8e04a91 commit 4e079b5

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

.changeset/chilly-carpets-switch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: add css hash to custom element rendered with `svelte:element`

packages/svelte/src/internal/client/dom/elements/attributes.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,20 @@ export function set_xlink_attribute(dom, attribute, value) {
129129
* @param {any} node
130130
* @param {string} prop
131131
* @param {any} value
132+
* @param {string} [css_hash]
132133
*/
133-
export function set_custom_element_data(node, prop, value) {
134+
export function set_custom_element_data(node, prop, value, css_hash) {
134135
if (prop in node) {
135136
var curr_val = node[prop];
136137
var next_val = typeof curr_val === 'boolean' && value === '' ? true : value;
137138
if (typeof curr_val !== 'object' || curr_val !== next_val) {
138139
node[prop] = next_val;
139140
}
140141
} else {
142+
if (css_hash && css_hash.length !== 0 && prop === 'class') {
143+
if (value) value += ' ';
144+
value += css_hash;
145+
}
141146
set_attribute(node, prop, value);
142147
}
143148
}
@@ -320,7 +325,7 @@ export function set_dynamic_element_attributes(node, prev, next, css_hash) {
320325
}
321326

322327
for (key in next) {
323-
set_custom_element_data(node, key, next[key]);
328+
set_custom_element_data(node, key, next[key], css_hash);
324329
}
325330

326331
return next;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ok, test } from '../../test';
2+
3+
export default test({
4+
html: `<custom-element class="red svelte-p153w3"></custom-element><custom-element class="red svelte-p153w3"></custom-element>`,
5+
6+
async test({ assert, target }) {
7+
const [el, el2] = target.querySelectorAll('custom-element');
8+
ok(el);
9+
ok(el2);
10+
11+
assert.deepEqual(el.className, 'red svelte-p153w3');
12+
assert.deepEqual(el2.className, 'red svelte-p153w3');
13+
}
14+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<svelte:element this={'custom-element'} class="red"></svelte:element>
2+
<custom-element class="red"></custom-element>
3+
4+
<style>
5+
.red {
6+
color: red;
7+
}
8+
</style>

0 commit comments

Comments
 (0)