lwc icon indicating copy to clipboard operation
lwc copied to clipboard

`null`/`undefined` value does not remove `xlink:`/`xml:` attribute

Open nolanlawson opened this issue 1 year ago • 3 comments

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.

nolanlawson avatar May 16 '23 19:05 nolanlawson

This issue has been linked to a new work item: W-13217587

git2gus[bot] avatar May 16 '23 19:05 git2gus[bot]

@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

AllanOricil avatar Sep 17 '23 18:09 AllanOricil

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.

nolanlawson avatar Sep 18 '23 15:09 nolanlawson