Skip to content

Commit b999015

Browse files
committed
fix: run render functions for dynamic void elements
1 parent 1b688ea commit b999015

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

.changeset/fifty-masks-give.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: run render functions for dynamic void elements

packages/svelte/src/internal/client/dom/blocks/svelte-element.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function swap_block_dom(effect, from, to) {
4040
* @param {Comment} anchor
4141
* @param {() => string} get_tag
4242
* @param {boolean} is_svg
43-
* @param {undefined | ((element: Element, anchor: Node) => void)} render_fn,
43+
* @param {undefined | ((element: Element, anchor: Node | null) => void)} render_fn,
4444
* @param {undefined | (() => string)} get_namespace
4545
* @returns {void}
4646
*/
@@ -115,13 +115,11 @@ export function element(anchor, get_tag, is_svg, render_fn, get_namespace) {
115115
? element.firstChild && hydrate_anchor(/** @type {Comment} */ (element.firstChild))
116116
: element.appendChild(empty());
117117

118-
if (child_anchor) {
119-
// `child_anchor` can be undefined if this is a void element with children,
120-
// i.e. `<svelte:element this={"hr"}>...</svelte:element>`. This is
121-
// user error, but we warn on it elsewhere (in dev) so here we just
122-
// silently ignore it
123-
render_fn(element, child_anchor);
124-
}
118+
// `child_anchor` is undefined if this is a void element, but we still,
119+
// need to call `render_fn` in order to run actions etc. If the element
120+
// contains children, it's a user error (which is warned on elsewhere)
121+
// and the DOM will be silently discarded
122+
render_fn(element, child_anchor);
125123
}
126124

127125
anchor.before(element);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
html: `<input><input>`,
5+
6+
async test({ assert, target }) {
7+
const inputs = target.querySelectorAll('input');
8+
assert.equal(inputs[0].value, 'set from action');
9+
assert.equal(inputs[1].value, 'set from action');
10+
}
11+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
/** @param {HTMLInputElement} node */
3+
function action(node) {
4+
node.value = 'set from action';
5+
}
6+
</script>
7+
8+
<input use:action>
9+
<svelte:element this={'input'} use:action />

0 commit comments

Comments
 (0)