Repository: sveltejs/svelte (JavaScript)

Issue:
<input autofocus> generates incorrect code in some situations
### Describe the bug

Code like this...

```svelte
<h1>wat</h1>
<input autofocus />
```

...yields this — note the `undefined`:

```js
export default function App($$anchor) {
	var fragment = root();

	$.autofocus(undefined, true);
	$.next(2);
	$.append($$anchor, fragment);
}
```

### Reproduction

[link](https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAExXITQqAIBAG0KvEtxairYjQOaqF1EiCf-RMEdHdo-V7D3yI1KCnB9klgsZYKxT4rj_aSZEJCq3Isf5j9sFejk2_D3bOJuQq3Dnh4ssqrevtnKGQyhZ8oA2aD6F3eT9rHSeWaQAAAA==)


Discussion:
[Another broken case](https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAExXJQQqAIBBG4avEvxbciwSdI1tEjjBQKjoTRHT3cPm-9yLxSR1ufZH3i-Cw1AoDeeqIftMpBINetB1DfOR7DjmI51xVpl2lpHJon-wcsrdjw-AqkRNThJOm9G3fD9Gp8mBqAAAA) — the `autofocus` code isn't generated at all. Presumably we need to mark the subtree as dynamic
Why the autofocus is set dynamically with `$.autofocus()` ?
It should be in the template. No ?

```js
import * as $ from "svelte/internal/client";

var root = $.template(`<h1>wat</h1> <input disabled>`, 1);

export default function App($$anchor) {
	var fragment = root();

	$.next(2);
	$.append($$anchor, fragment);
}
```
Inserting DOM elements with `autofocus` attributes doesn't autofocus them.



Patch:
diff --git a/.changeset/healthy-poets-float.md b/.changeset/healthy-poets-float.md
new file mode 100644
index 000000000000..4709b4495ed8
--- /dev/null
+++ b/.changeset/healthy-poets-float.md
@@ -0,0 +1,5 @@
+---
+'svelte': patch
+---
+
+fix: correctly applies autofocus to static elements
diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js
index 49a642eb497a..dd8a594e610e 100644
--- a/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js
+++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js
@@ -75,6 +75,14 @@ export function RegularElement(node, context) {
 		node.attributes.push(create_attribute('value', child.start, child.end, [child]));
 	}
 
+	if (
+		node.attributes.some(
+			(attribute) => attribute.type === 'Attribute' && attribute.name === 'autofocus'
+		)
+	) {
+		mark_subtree_dynamic(context.path);
+	}
+
 	const binding = context.state.scope.get(node.name);
 	if (
 		binding !== null &&
diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js
index 4008dbdfa3cb..4de1a11a97ee 100644
--- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js
+++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js
@@ -142,6 +142,10 @@ function is_static_element(node) {
 			return false;
 		}
 
+		if (attribute.name === 'autofocus') {
+			return false;
+		}
+
 		if (node.name === 'option' && attribute.name === 'value') {
 			return false;
 		}


Test patch:
diff --git a/packages/svelte/tests/runtime-legacy/samples/autofocus-2/_config.js b/packages/svelte/tests/runtime-legacy/samples/autofocus-2/_config.js
new file mode 100644
index 000000000000..24b45b376174
--- /dev/null
+++ b/packages/svelte/tests/runtime-legacy/samples/autofocus-2/_config.js
@@ -0,0 +1,7 @@
+import { test } from '../../test';
+
+export default test({
+	async test({ assert, target, window }) {
+		assert.equal(target.querySelector('input'), window.document.activeElement);
+	}
+});
diff --git a/packages/svelte/tests/runtime-legacy/samples/autofocus-2/main.svelte b/packages/svelte/tests/runtime-legacy/samples/autofocus-2/main.svelte
new file mode 100644
index 000000000000..e3ea9a76d40c
--- /dev/null
+++ b/packages/svelte/tests/runtime-legacy/samples/autofocus-2/main.svelte
@@ -0,0 +1,2 @@
+<h1>wat</h1>
+<input autofocus />
diff --git a/packages/svelte/tests/runtime-legacy/samples/autofocus-3/_config.js b/packages/svelte/tests/runtime-legacy/samples/autofocus-3/_config.js
new file mode 100644
index 000000000000..24b45b376174
--- /dev/null
+++ b/packages/svelte/tests/runtime-legacy/samples/autofocus-3/_config.js
@@ -0,0 +1,7 @@
+import { test } from '../../test';
+
+export default test({
+	async test({ assert, target, window }) {
+		assert.equal(target.querySelector('input'), window.document.activeElement);
+	}
+});
diff --git a/packages/svelte/tests/runtime-legacy/samples/autofocus-3/main.svelte b/packages/svelte/tests/runtime-legacy/samples/autofocus-3/main.svelte
new file mode 100644
index 000000000000..31d2cbc0ec62
--- /dev/null
+++ b/packages/svelte/tests/runtime-legacy/samples/autofocus-3/main.svelte
@@ -0,0 +1,3 @@
+<div>
+	<input autofocus />
+</div>