Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure spread attribute events are attached synchronously #14387

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rich-worms-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: attach spread attribute events synchronously
27 changes: 2 additions & 25 deletions packages/svelte/src/internal/client/dom/elements/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { create_event, delegate } from './events.js';
import { add_form_reset_listener, autofocus } from './misc.js';
import * as w from '../../warnings.js';
import { LOADING_ATTR_SYMBOL } from '../../constants.js';
import { queue_idle_task, queue_micro_task } from '../task.js';
import { queue_idle_task } from '../task.js';
import { is_capture_event, is_delegated, normalize_attribute } from '../../../../utils.js';
import {
active_effect,
Expand Down Expand Up @@ -209,8 +209,6 @@ export function set_attributes(

// @ts-expect-error
var attributes = /** @type {Record<string, unknown>} **/ (element.__attributes ??= {});
/** @type {Array<[string, any, () => void]>} */
var events = [];

// since key is captured we use const
for (const key in next) {
Expand Down Expand Up @@ -277,15 +275,7 @@ export function set_attributes(
current[key].call(this, evt);
}

if (!prev) {
events.push([
key,
value,
() => (current[event_handle_key] = create_event(event_name, element, handle, opts))
]);
} else {
current[event_handle_key] = create_event(event_name, element, handle, opts);
}
current[event_handle_key] = create_event(event_name, element, handle, opts);
} else {
// @ts-ignore
element[`__${event_name}`] = value;
Expand Down Expand Up @@ -325,19 +315,6 @@ export function set_attributes(
}
}

// On the first run, ensure that events are added after bindings so
// that their listeners fire after the binding listeners
if (!prev) {
queue_micro_task(() => {
if (!element.isConnected) return;
for (const [key, value, evt] of events) {
if (current[key] === value) {
evt();
}
}
});
}

return current;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from '../../test';

export default test({
test({ assert, logs }) {
assert.deepEqual(logs, ['onfocus']);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
const focus = (input) => {
input.focus();
};
</script>

<input {...({})} onfocus={() => console.log("onfocus")} use:focus />
Loading