Skip to content

Reference Target: what type should the referenceTarget attribute be? #1093

@alice

Description

@alice

Originally posted by @dandclark in #1089

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

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. 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 when trying to set the attribute to null, and this conversion happens before the implementation of the property "sees" the value:

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:

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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions