@ -46,7 +46,7 @@ export function build_element_attributes(node, context) {
/** @type {Expression | null} */
let content = null ;
let ha s_spread = false ;
let need s_spread = false ;
// Use the index to keep the attributes order which is important for spreading
let class _index = - 1 ;
let style _index = - 1 ;
@ -82,8 +82,28 @@ export function build_element_attributes(node, context) {
) {
events _to _capture . add ( attribute . name ) ;
}
// the defaultValue/defaultChecked properties don't exist as attributes
} else if ( attribute . name !== 'defaultValue' && attribute . name !== 'defaultChecked' ) {
} else if (
( node . name === 'input' || node . name === 'textarea' ) &&
( attribute . name === 'defaultValue' || attribute . name === 'defaultChecked' )
) {
// If there's a defaultValue but not value attribute, we turn it into a value attribute (same for checked).
// If we can't, then we need to use a spread in order to be able to detect at runtime whether or not
// the value/checked value is nullish, in which case defaultValue/defaultChecked should be used.
const replacement _name = attribute . name === 'defaultValue' ? 'value' : 'checked' ;
if (
! node . attributes . some (
( attr ) =>
attr . type === 'SpreadAttribute' ||
( ( attr . type === 'BindDirective' || attr . type === 'Attribute' ) &&
attr . name === replacement _name )
)
) {
attributes . push ( { ... attribute , name : replacement _name } ) ;
} else {
needs _spread = true ;
attributes . push ( attribute ) ;
}
} else {
if ( attribute . name === 'class' ) {
class _index = attributes . length ;
} else if ( attribute . name === 'style' ) {
@ -173,7 +193,7 @@ export function build_element_attributes(node, context) {
}
} else if ( attribute . type === 'SpreadAttribute' ) {
attributes . push ( attribute ) ;
ha s_spread = true ;
need s_spread = true ;
if ( is _load _error _element ( node . name ) ) {
events _to _capture . add ( 'onload' ) ;
events _to _capture . add ( 'onerror' ) ;
@ -194,7 +214,7 @@ export function build_element_attributes(node, context) {
}
}
if ( class _directives . length > 0 && ! ha s_spread ) {
if ( class _directives . length > 0 && ! need s_spread ) {
const class _attribute = build _class _directives (
class _directives ,
/** @type {AST.Attribute | null} */ ( attributes [ class _index ] ? ? null )
@ -204,7 +224,7 @@ export function build_element_attributes(node, context) {
}
}
if ( style _directives . length > 0 && ! ha s_spread ) {
if ( style _directives . length > 0 && ! need s_spread ) {
build _style _directives (
style _directives ,
/** @type {AST.Attribute | null} */ ( attributes [ style _index ] ? ? null ) ,
@ -215,7 +235,7 @@ export function build_element_attributes(node, context) {
}
}
if ( ha s_spread ) {
if ( need s_spread ) {
build _element _spread _attributes ( node , attributes , style _directives , class _directives , context ) ;
} else {
for ( const attribute of /** @type {AST.Attribute[]} */ ( attributes ) ) {