Skip to content

Commit 4c98c2e

Browse files
authored
chore: consolidate checks for never-static attributes (#14372)
We're using string checks in various places, better to have it encapsulated in one function
1 parent 32a1453 commit 4c98c2e

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @import { AST } from '#compiler' */
22
/** @import { Context } from '../types' */
3-
import { is_mathml, is_svg, is_void } from '../../../../utils.js';
3+
import { cannot_be_set_statically, is_mathml, is_svg, is_void } from '../../../../utils.js';
44
import {
55
is_tag_valid_with_ancestor,
66
is_tag_valid_with_parent
@@ -77,9 +77,7 @@ export function RegularElement(node, context) {
7777

7878
if (
7979
node.attributes.some(
80-
(attribute) =>
81-
attribute.type === 'Attribute' &&
82-
(attribute.name === 'autofocus' || attribute.name === 'muted')
80+
(attribute) => attribute.type === 'Attribute' && cannot_be_set_statically(attribute.name)
8381
)
8482
) {
8583
mark_subtree_dynamic(context.path);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/** @import { Scope } from '../../../scope' */
66
import { escape_html } from '../../../../../escaping.js';
77
import {
8+
cannot_be_set_statically,
89
is_boolean_attribute,
910
is_dom_property,
1011
is_load_error_element,
@@ -262,8 +263,7 @@ export function RegularElement(node, context) {
262263

263264
if (
264265
!is_custom_element &&
265-
attribute.name !== 'autofocus' &&
266-
attribute.name !== 'muted' &&
266+
!cannot_be_set_statically(attribute.name) &&
267267
(attribute.value === true || is_text_attribute(attribute))
268268
) {
269269
const name = get_attribute_name(node, attribute);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/** @import { Scope } from '../../../../scope.js' */
44
/** @import { ComponentContext } from '../../types' */
55
import { escape_html } from '../../../../../../escaping.js';
6+
import { cannot_be_set_statically } from '../../../../../../utils.js';
67
import { is_event_attribute } from '../../../../../utils/ast.js';
78
import * as b from '../../../../../utils/builders.js';
89
import { build_template_chunk, build_update } from './utils.js';
@@ -142,7 +143,7 @@ function is_static_element(node) {
142143
return false;
143144
}
144145

145-
if (attribute.name === 'autofocus' || attribute.name === 'muted') {
146+
if (cannot_be_set_statically(attribute.name)) {
146147
return false;
147148
}
148149

packages/svelte/src/utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,17 @@ export function is_dom_property(name) {
222222
return DOM_PROPERTIES.includes(name);
223223
}
224224

225+
const NON_STATIC_PROPERTIES = ['autofocus', 'muted'];
226+
227+
/**
228+
* Returns `true` if the given attribute cannot be set through the template
229+
* string, i.e. needs some kind of JavaScript handling to work.
230+
* @param {string} name
231+
*/
232+
export function cannot_be_set_statically(name) {
233+
return NON_STATIC_PROPERTIES.includes(name);
234+
}
235+
225236
/**
226237
* Subset of delegated events which should be passive by default.
227238
* These two are already passive via browser defaults on window, document and body.

0 commit comments

Comments
 (0)