Skip to content

Commit bbd5103

Browse files
committed
fix
1 parent 2108417 commit bbd5103

File tree

5 files changed

+52
-26
lines changed

5 files changed

+52
-26
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/** @import { SourceLocation } from '#shared' */
44
/** @import { ComponentClientTransformState, ComponentContext } from '../types' */
55
/** @import { Scope } from '../../../scope' */
6-
import { is_boolean_attribute, is_void } from '../../../../../utils.js';
6+
import { is_boolean_attribute, is_dom_property, is_void } from '../../../../../utils.js';
77
import { escape_html } from '../../../../../escaping.js';
88
import { dev, is_ignored, locator } from '../../../../state.js';
99
import {
@@ -12,7 +12,7 @@ import {
1212
is_text_attribute
1313
} from '../../../../utils/ast.js';
1414
import * as b from '../../../../utils/builders.js';
15-
import { DOMProperties, LoadErrorElements } from '../../../constants.js';
15+
import { LoadErrorElements } from '../../../constants.js';
1616
import { is_custom_element_node } from '../../../nodes.js';
1717
import { clean_nodes, determine_namespace_for_children } from '../../utils.js';
1818
import { serialize_get_binding } from '../utils.js';
@@ -598,7 +598,7 @@ function serialize_element_attribute_update_assignment(element, node_id, attribu
598598
update = b.stmt(b.call('$.set_value', node_id, value));
599599
} else if (name === 'checked') {
600600
update = b.stmt(b.call('$.set_checked', node_id, value));
601-
} else if (DOMProperties.includes(name)) {
601+
} else if (is_dom_property(name)) {
602602
update = b.stmt(b.assignment('=', b.member(node_id, b.id(name)), value));
603603
} else {
604604
const callee = name.startsWith('xlink') ? '$.set_xlink_attribute' : '$.set_attribute';

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
/** @import { Expression, Identifier } from 'estree' */
22
/** @import { Attribute, ClassDirective, DelegatedEvent, ExpressionMetadata, ExpressionTag, Namespace, OnDirective, RegularElement, StyleDirective, SvelteElement, SvelteNode } from '#compiler' */
33
/** @import { ComponentContext } from '../../types' */
4-
import { ATTRIBUTE_ALIASES } from '../../../../../../constants.js';
5-
import { is_capture_event, is_passive_event } from '../../../../../../utils.js';
4+
import {
5+
is_capture_event,
6+
is_passive_event,
7+
normalize_attribute
8+
} from '../../../../../../utils.js';
69
import { get_attribute_expression } from '../../../../../utils/ast.js';
710
import * as b from '../../../../../utils/builders.js';
811
import { serialize_get_binding } from '../../utils.js';
@@ -118,18 +121,15 @@ export function serialize_attribute_value(value, context) {
118121
* @param {{ state: { metadata: { namespace: Namespace }}}} context
119122
*/
120123
export function get_attribute_name(element, attribute, context) {
121-
let name = attribute.name;
122124
if (
123125
!element.metadata.svg &&
124126
!element.metadata.mathml &&
125127
context.state.metadata.namespace !== 'foreign'
126128
) {
127-
name = name.toLowerCase();
128-
if (name in ATTRIBUTE_ALIASES) {
129-
name = ATTRIBUTE_ALIASES[name];
130-
}
129+
return normalize_attribute(attribute.name);
131130
}
132-
return name;
131+
132+
return attribute.name;
133133
}
134134

135135
/**

packages/svelte/src/compiler/phases/constants.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
import { ATTRIBUTE_ALIASES } from '../../constants.js';
2-
import { DOM_BOOLEAN_ATTRIBUTES } from '../../utils.js';
3-
4-
export const DOMProperties = [
5-
...Object.values(ATTRIBUTE_ALIASES),
6-
'value',
7-
'inert',
8-
'volume',
9-
...DOM_BOOLEAN_ATTRIBUTES
10-
];
11-
121
export const Runes = /** @type {const} */ ([
132
'$state',
143
'$state.frozen',

packages/svelte/src/internal/client/dom/elements/attributes.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { DEV } from 'esm-env';
22
import { hydrating } from '../hydration.js';
33
import { get_descriptors, get_prototype_of } from '../../../shared/utils.js';
4-
import { ATTRIBUTE_ALIASES, NAMESPACE_SVG } from '../../../../constants.js';
4+
import { NAMESPACE_SVG } from '../../../../constants.js';
55
import { create_event, delegate } from './events.js';
66
import { add_form_reset_listener, autofocus } from './misc.js';
77
import * as w from '../../warnings.js';
88
import { LOADING_ATTR_SYMBOL } from '../../constants.js';
99
import { queue_idle_task, queue_micro_task } from '../task.js';
10-
import { is_capture_event, is_delegated } from '../../../../utils.js';
10+
import { is_capture_event, is_delegated, normalize_attribute } from '../../../../utils.js';
1111

1212
/**
1313
* The value/checked attribute in the template actually corresponds to the defaultValue property, so we need
@@ -268,8 +268,7 @@ export function set_attributes(element, prev, next, lowercase_attributes, css_ha
268268
} else {
269269
var name = key;
270270
if (lowercase_attributes) {
271-
name = name.toLowerCase();
272-
name = ATTRIBUTE_ALIASES[name] || name;
271+
name = normalize_attribute(name);
273272
}
274273

275274
if (setters.includes(name)) {

packages/svelte/src/utils.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,44 @@ export function is_boolean_attribute(name) {
180180
return DOM_BOOLEAN_ATTRIBUTES.includes(name);
181181
}
182182

183+
/**
184+
* @type {Record<string, string>}
185+
* List of attribute names that should be aliased to their property names
186+
* because they behave differently between setting them as an attribute and
187+
* setting them as a property.
188+
*/
189+
const ATTRIBUTE_ALIASES = {
190+
// no `class: 'className'` because we handle that separately
191+
formnovalidate: 'formNoValidate',
192+
ismap: 'isMap',
193+
nomodule: 'noModule',
194+
playsinline: 'playsInline',
195+
readonly: 'readOnly'
196+
};
197+
198+
/**
199+
* @param {string} name
200+
*/
201+
export function normalize_attribute(name) {
202+
name = name.toLowerCase();
203+
return ATTRIBUTE_ALIASES[name] ?? name;
204+
}
205+
206+
const DOM_PROPERTIES = [
207+
...Object.values(ATTRIBUTE_ALIASES),
208+
'value',
209+
'inert',
210+
'volume',
211+
...DOM_BOOLEAN_ATTRIBUTES
212+
];
213+
214+
/**
215+
* @param {string} name
216+
*/
217+
export function is_dom_property(name) {
218+
return DOM_PROPERTIES.includes(name);
219+
}
220+
183221
const PASSIVE_EVENTS = ['wheel', 'touchstart', 'touchmove', 'touchend', 'touchcancel'];
184222

185223
/**

0 commit comments

Comments
 (0)