Skip to content

Commit 193cc37

Browse files
authored
fix: correctly applies autofocus to static elements (#13648)
* fix: correctly applies autofocus to static elements * lint * fix other case
1 parent e21e624 commit 193cc37

File tree

7 files changed

+36
-0
lines changed

7 files changed

+36
-0
lines changed

.changeset/healthy-poets-float.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: correctly applies autofocus to static elements

packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ export function RegularElement(node, context) {
7575
node.attributes.push(create_attribute('value', child.start, child.end, [child]));
7676
}
7777

78+
if (
79+
node.attributes.some(
80+
(attribute) => attribute.type === 'Attribute' && attribute.name === 'autofocus'
81+
)
82+
) {
83+
mark_subtree_dynamic(context.path);
84+
}
85+
7886
const binding = context.state.scope.get(node.name);
7987
if (
8088
binding !== null &&

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ function is_static_element(node) {
142142
return false;
143143
}
144144

145+
if (attribute.name === 'autofocus') {
146+
return false;
147+
}
148+
145149
if (node.name === 'option' && attribute.name === 'value') {
146150
return false;
147151
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
async test({ assert, target, window }) {
5+
assert.equal(target.querySelector('input'), window.document.activeElement);
6+
}
7+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>wat</h1>
2+
<input autofocus />
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
async test({ assert, target, window }) {
5+
assert.equal(target.querySelector('input'), window.document.activeElement);
6+
}
7+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
<input autofocus />
3+
</div>

0 commit comments

Comments
 (0)