[WICG/webcomponents] case-sensitive attribute APIs would be useful. (Issue #1076)

We cannot change how the HTML parser works, because it is difficult and dangerous or so I hear.

However, I think that we can easily *expand* it so that it will be able to *additionally* write case-sensitive attribute names to new structures accessible in JavaScript in a backwards-comaptible way.

For example, we would add APIs that still function case-insensitively, but allow reading case-sensitive names. Namely, I'm thinking that as an alternative to `el.attributes`, there could be a new `el.attributesCaseSensitive` that allows reading attribute names with case sensitivity the way they were written. But setting an attribute would still be case insensitive for backwards compat, so it would not be possible to have two attributes names `foobar` and `fooBar`.

Or, another possible way to do it could be that `Attr` objects simply contain their original parsed name.

## Use Case

People making frameworks with `html` tagged templates want to know the `case-sensitive` names as they were written in the templates.

For example, Lit's `html` has syntax like `.fooBar=` for setting JS properties, which is case sensitive. Lit currently uses `RegExp` instead of DOMParser to be able to read the values in a custom way that is case sensitive.

However, if the DOM provided a way to read the original names as written, then it could elimlinate the need for complex custom RegExp usage.

As an example of a very simplified `html` template tag, see [Pota's `html`](https://pota.quack.uy/HTML):

https://github.com/potahtml/pota/blob/7db5873ad7e95582c06e8810b2d9e219f299714c/src/html.js

The **whole thing** is 425 of clean simple code, because it uses `DOMParser`. It is very very easy to understand.

The only problem with Pota's HTML is that it has the case insensitive issue because it uses HTML mode (instead of XML mode) for particular reasons.

If we had case sensitive access to original attribute names, Lit's `html` could be greatly simplified to be more like Pota's.

Solid's `html` is also a [bit messy](https://github.com/ryansolid/dom-expressions/blob/main/packages/lit-dom-expressions/src/index.ts) with its custom parsing with RegExp, but still quite a bit shorter than Lit's.

The point is, with access to the original case sensitive names of attributes, we can simplify both Lit's and Solid's, to be more like Pota's, and also it could provide a basis for a native `html` template tag shipped in the browser with explainable behavior instead of bespoke parsing that is not easily doable in userland:

- https://github.com/ryansolid/dom-expressions/blob/main/packages/lit-dom-expressions/src/index.ts



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

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

Received on Sunday, 13 October 2024 20:57:15 UTC