fix: handle autofocus in `set_attributes`

pull/18058/head
paoloricciuti 5 months ago
parent 8a8b779586
commit 9c1d17e51f

@ -431,7 +431,16 @@ function set_attributes(
// avoid using the setter // avoid using the setter
set_attribute(element, key, value); set_attribute(element, key, value);
} else if (key === 'autofocus') { } else if (key === 'autofocus') {
if (renderer == null) {
autofocus(/** @type {HTMLElement} */ (element), Boolean(value)); autofocus(/** @type {HTMLElement} */ (element), Boolean(value));
} else {
// In custom renderer mode, just set autofocus as a regular attribute
if (value) {
set_attribute_op(element, key, value);
} else {
remove_attribute(element, key);
}
}
} else if (!is_custom_element && (key === '__value' || (key === 'value' && value != null))) { } else if (!is_custom_element && (key === '__value' || (key === 'value' && value != null))) {
// @ts-ignore We're not running this for custom elements because __value is actually // @ts-ignore We're not running this for custom elements because __value is actually
// how Lit stores the current value on the element, and messing with that would break things. // how Lit stores the current value on the element, and messing with that would break things.

@ -0,0 +1,18 @@
import { test } from '../../test';
export default test({
test({ assert, target }) {
// If we got here, the component mounted without crashing on document.body access.
// Verify autofocus is set as a regular attribute.
const input = target.children.find(
(/** @type {any} */ n) => n.type === 'element' && n.name === 'input'
);
assert.ok(input, 'input element should exist');
assert.equal(
input.attributes['autofocus'],
'true',
'autofocus should be set as a regular attribute'
);
assert.equal(input.attributes['value'], 'test', 'value should be set as a regular attribute');
}
});

@ -0,0 +1,5 @@
<script>
let input_tag = 'input';
</script>
<svelte:element this={input_tag} autofocus value="test" />
Loading…
Cancel
Save