chore: consolidate checks for never-static attributes (#14372)

We're using string checks in various places, better to have it encapsulated in one function
pull/14366/head
Simon H 1 month ago committed by GitHub
parent 32a1453805
commit 4c98c2e4a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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);

@ -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);

@ -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;
}

@ -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.

Loading…
Cancel
Save