Re: [WICG/webcomponents] Reference Target: How should we treat invalid reference targets for relations set via Element IDL attributes? (Issue #1089)

@alice To make sure I understand you, are you thinking that the `referenceTarget` attribute's type should be a nullable `DOMString`?

```js
partial interface ShadowRoot {
  attribute DOMString? referenceTarget;
}
```

I had been assuming the attribute's type would just be `DOMString`, and that's how it's [prototyped today](https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/dom/shadow_root.idl;l=58;drc=86547d0ef65244867832bba1721ef3d0ed826eea). But looking at the explainer again I guess it's not specific on this point.

Assuming I'm reading the specs correctly, keeping it as a non-nullable `DOMString` triggers [this string conversion](https://webidl.spec.whatwg.org/#js-DOMString) when trying to set the attribute to null, and this conversion happens before the implementation of the property "sees" the value:
```js
document.querySelector("#fancy-listbox").shadowRoot.referenceTarget = null;
document.querySelector("#fancy-listbox").shadowRoot.referenceTarget; // logs 'null'
```
But if we make it `DOMString?` then we'd get the behavior you suggest. That's definitely seems like it'd avoid a pitfall when someone wants to clear the referenceTarget value. An inconsistency it introduces though is that the `Element.id` property is itself a non-nullable `DOMString`. So you get this today:

```js
const div = document.createElement("div");
div.id = null;
div.id; // logs 'null'
```

It's tempting to treat the `referenceTarget` property as kind of equivalent to setting an ID elsewhere, so keeping the behavior consistent could be nice. What do you think? (Maybe this question should be its own thread.)

-- 
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/1089#issuecomment-2606132790
You are receiving this because you are subscribed to this thread.

Message ID: <WICG/webcomponents/issues/1089/2606132790@github.com>

Received on Wednesday, 22 January 2025 02:14:08 UTC