Skip to content

Commit 0c34327

Browse files
committed
simplify
1 parent 5919542 commit 0c34327

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

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

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ export function RegularElement(node, context) {
8585
const is_custom_element = is_custom_element_node(node);
8686
let needs_content_reset = false;
8787

88-
/** @type {AST.BindDirective | null} */
89-
let value_binding = null;
90-
9188
let is_content_editable = false;
9289

9390
if (is_custom_element) {
@@ -105,15 +102,17 @@ export function RegularElement(node, context) {
105102
}
106103
}
107104

108-
/** @type {Set<string>} */
109-
const names = new Set();
105+
/** @type {Map<string, AST.Attribute>} */
106+
const lookup = new Map();
107+
108+
/** @type {Map<string, AST.BindDirective>} */
109+
const bindings = new Map();
110110

111-
/** @type {Set<string>} */
112-
const bindings = new Set();
111+
let has_spread = false;
113112

114113
for (const attribute of node.attributes) {
115114
if (attribute.type === 'Attribute') {
116-
names.add(attribute.name);
115+
lookup.set(attribute.name, attribute);
117116

118117
if (
119118
(attribute.name === 'value' || attribute.name === 'checked') &&
@@ -128,16 +127,11 @@ export function RegularElement(node, context) {
128127
is_content_editable = true;
129128
}
130129
} else if (attribute.type === 'SpreadAttribute') {
131-
names.add('*');
132-
needs_content_reset = true;
130+
has_spread = true;
133131
}
134-
if (attribute.type === 'BindDirective') {
135-
bindings.add(attribute.name);
136132

137-
if (attribute.name === 'value') {
138-
value_binding = attribute;
139-
needs_content_reset = true;
140-
}
133+
if (attribute.type === 'BindDirective') {
134+
bindings.set(attribute.name, attribute);
141135
}
142136
}
143137

@@ -184,12 +178,12 @@ export function RegularElement(node, context) {
184178
context.state.init.push(b.stmt(b.call('$.remove_input_defaults', context.state.node)));
185179
}
186180

187-
if (needs_content_reset && node.name === 'textarea') {
181+
if (node.name === 'textarea' && (has_spread || bindings.has('value') || needs_content_reset)) {
188182
context.state.init.push(b.stmt(b.call('$.remove_textarea_child', context.state.node)));
189183
}
190184

191-
if (value_binding !== null && node.name === 'select') {
192-
setup_select_synchronization(value_binding, context);
185+
if (node.name === 'select' && bindings.has('value')) {
186+
setup_select_synchronization(/** @type {AST.BindDirective} */ (bindings.get('value')), context);
193187
}
194188

195189
const node_id = context.state.node;
@@ -206,7 +200,7 @@ export function RegularElement(node, context) {
206200
node,
207201
node_id,
208202
// If value binding exists, that one takes care of calling $.init_select
209-
value_binding === null && node.name === 'select'
203+
node.name === 'select' && !bindings.has('value')
210204
);
211205
is_attributes_reactive = true;
212206
} else {
@@ -256,7 +250,7 @@ export function RegularElement(node, context) {
256250
}
257251

258252
// Apply the src and loading attributes for <img> elements after the element is appended to the document
259-
if (node.name === 'img' && (names.has('*') || names.has('loading'))) {
253+
if (node.name === 'img' && (has_spread || lookup.has('loading'))) {
260254
context.state.after_update.push(b.stmt(b.call('$.handle_lazy_img', node_id)));
261255
}
262256

@@ -267,14 +261,14 @@ export function RegularElement(node, context) {
267261
node_id,
268262
context,
269263
is_attributes_reactive,
270-
names.has('style') || node.metadata.has_spread
264+
lookup.has('style') || node.metadata.has_spread
271265
);
272266

273267
if (
274268
is_load_error_element(node.name) &&
275-
(names.has('*') ||
276-
names.has('onload') ||
277-
names.has('onerror') ||
269+
(has_spread ||
270+
lookup.has('onload') ||
271+
lookup.has('onerror') ||
278272
node.attributes.some((attribute) => attribute.type === 'UseDirective'))
279273
) {
280274
context.state.after_update.push(b.stmt(b.call('$.replay_events', node_id)));
@@ -372,7 +366,7 @@ export function RegularElement(node, context) {
372366
context.state.after_update.push(...child_state.after_update);
373367
}
374368

375-
if (names.has('dir')) {
369+
if (lookup.has('dir')) {
376370
// This fixes an issue with Chromium where updates to text content within an element
377371
// does not update the direction when set to auto. If we just re-assign the dir, this fixes it.
378372
const dir = b.member(node_id, 'dir');

0 commit comments

Comments
 (0)