From 4c98c2e4a6aaa54650cc834e109a0e2dabd5a574 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Wed, 20 Nov 2024 15:52:03 +0100 Subject: [PATCH] chore: consolidate checks for never-static attributes (#14372) We're using string checks in various places, better to have it encapsulated in one function --- .../phases/2-analyze/visitors/RegularElement.js | 6 ++---- .../3-transform/client/visitors/RegularElement.js | 4 ++-- .../3-transform/client/visitors/shared/fragment.js | 3 ++- packages/svelte/src/utils.js | 11 +++++++++++ 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js index 60bd1dd0c5..d5964f9ae1 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js @@ -1,6 +1,6 @@ /** @import { AST } from '#compiler' */ /** @import { Context } from '../types' */ -import { is_mathml, is_svg, is_void } from '../../../../utils.js'; +import { cannot_be_set_statically, is_mathml, is_svg, is_void } from '../../../../utils.js'; import { is_tag_valid_with_ancestor, is_tag_valid_with_parent @@ -77,9 +77,7 @@ export function RegularElement(node, context) { if ( node.attributes.some( - (attribute) => - attribute.type === 'Attribute' && - (attribute.name === 'autofocus' || attribute.name === 'muted') + (attribute) => attribute.type === 'Attribute' && cannot_be_set_statically(attribute.name) ) ) { mark_subtree_dynamic(context.path); diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js index 455327a712..19948464d2 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js @@ -5,6 +5,7 @@ /** @import { Scope } from '../../../scope' */ import { escape_html } from '../../../../../escaping.js'; import { + cannot_be_set_statically, is_boolean_attribute, is_dom_property, is_load_error_element, @@ -262,8 +263,7 @@ export function RegularElement(node, context) { if ( !is_custom_element && - attribute.name !== 'autofocus' && - attribute.name !== 'muted' && + !cannot_be_set_statically(attribute.name) && (attribute.value === true || is_text_attribute(attribute)) ) { const name = get_attribute_name(node, attribute); diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js index ac6e0f8f9f..102988781a 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js @@ -3,6 +3,7 @@ /** @import { Scope } from '../../../../scope.js' */ /** @import { ComponentContext } from '../../types' */ import { escape_html } from '../../../../../../escaping.js'; +import { cannot_be_set_statically } from '../../../../../../utils.js'; import { is_event_attribute } from '../../../../../utils/ast.js'; import * as b from '../../../../../utils/builders.js'; import { build_template_chunk, build_update } from './utils.js'; @@ -142,7 +143,7 @@ function is_static_element(node) { return false; } - if (attribute.name === 'autofocus' || attribute.name === 'muted') { + if (cannot_be_set_statically(attribute.name)) { return false; } diff --git a/packages/svelte/src/utils.js b/packages/svelte/src/utils.js index 60ec364e6f..84c7ca1efb 100644 --- a/packages/svelte/src/utils.js +++ b/packages/svelte/src/utils.js @@ -222,6 +222,17 @@ export function is_dom_property(name) { return DOM_PROPERTIES.includes(name); } +const NON_STATIC_PROPERTIES = ['autofocus', 'muted']; + +/** + * Returns `true` if the given attribute cannot be set through the template + * string, i.e. needs some kind of JavaScript handling to work. + * @param {string} name + */ +export function cannot_be_set_statically(name) { + return NON_STATIC_PROPERTIES.includes(name); +} + /** * Subset of delegated events which should be passive by default. * These two are already passive via browser defaults on window, document and body.