lwc
lwc copied to clipboard
`null`/`undefined` value does not remove `xlink:`/`xml:` attribute
Description
Setting an attribute to a value of null
or undefined
will typically cause a removeAttribute
call in LWC. However, this doesn't work for svg/xml namespace attributes (repro).
Template:
<template>
<div title={prop}></div>
<label xml:lang={prop}></label>
<svg>
<image xlink:title={prop}></image>
</svg>
</template>
Result:
<div></div>
<label xml:lang="null"></label>
<svg>
<image xlink:title="null"></image>
</svg>
Expected result:
<div></div>
<label></label>
<svg>
<image></image>
</svg>
The root cause is these lines of code. Note that svg/xml namespace attributes will never result in a removeAttribute
:
https://github.com/salesforce/lwc/blob/def03ea159cdc5e49dfcfc1118e7c4fe62b0f2af/packages/%40lwc/engine-core/src/framework/modules/attrs.ts#L39-L49
Note that fixing this would be an observable change.
This issue has been linked to a new work item: W-13217587
@nolanlawson @jmsjtu do you think this can be done now? Is it an easy issue to fix? Looking at it really quickly I see that it is only a matter of adding another branch here, like @nolanlawson said, to call the removeAtrribute
method when the prop's value is null || undefined
for xml and xlink attributes
This one might be tricky because it would be an observable change, meaning it could break existing components. We're still figuring out how API versioning can work, so maybe it should wait until we've finalized on that.