Re: [WICG/webcomponents] [DOM Parts] Add new declarative syntax and update iterative proposal (PR #1023)

@rictic commented on this pull request.



>  
 ```js
-const [firstName, lastName] = AttributePart.create(element, "title", null, [
-  null,
-  " ",
-  null,
-]);
-// Syntax to be improved. Here, a new AttributePart is created between each string.
+const part = AttributePart(document.getPartRoot(), element, "href");
+part.value = ["mailto: ", email];

I think we should think about making this behavior consistent and intuitive across the case where there are statics vs when there aren't. We also need to work with trusted types, so that we can set `part.value` to a trusted type in a page where trusted types are enforced.

One proposal:

An attribute is said to be fully controlled by an AttributePart's value if the statics are `['', '']`. In that case, commit behaves like:

```js
  #commitFullyControlled() {
    const value = Array.isArray(this.value) ? this.value[0] : this.value;
    if (value == null || value === false) {
      this.element.removeAttributeNS(this.namespaceURI, this.localName);
      return;
    }
    this.element.setAttributeNS(this.namespaceURI, this.localName, value);
  }
```

Note: if `value` is an array, we use its first element. This is to be consistent with the behavior when there are nonempty statics. `null`, `undefined`, and `false` when set on a fully controlled attribute remove it from the element.

If the attribute is not fully controlled by the value, then commit behaves like:

```js
  #commitWithStatics() {
    const value = Array.isArray(this.value) ? this.value : [this.value];
    let pieces = [this.statics[0]];
    for (let i = 1; i < statics.length; i++) {
      pieces.push(value[i - 1] ?? '', this.statics[i]);
    }
    return pieces.join('');
  }
```

Note: if the values array is too short, then the `?? ''` will write it as empty string, and excess elements are ignored.

And for completeness:

```js
  commit() {
    if (this.statics.length === 2 && this.statics[0] === '' && this.statics[1] === '') {
      this.#commitFullyControlled();
    } else {
      this.#commitWithStatics();
    }
  }
```


>  ```
 
-- **Pros** No need for new `PartialAttributePart` or `AttributePart`
-  coordination. Does not block a future API object that represents partial
-  attributes.
-- **Cons** Need a way to represent partial attributes that are declaratively
-  defined.
+At this time there is no specific native representation of templates that could
+be inspected to determine with certainty which strings were compile time
+constants, but if some a representation were to exist, this API could provide
+better security for attributes that were sens

Looks like a bit was cut off in editing

-- 
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/pull/1023#pullrequestreview-1568129442
You are receiving this because you are subscribed to this thread.

Message ID: <WICG/webcomponents/pull/1023/review/1568129442@github.com>

Received on Tuesday, 8 August 2023 20:28:21 UTC