Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide escape hatch for non-standard HTML attributes #295

Closed
cemerick opened this issue Jan 12, 2022 · 9 comments · Fixed by #296
Closed

Provide escape hatch for non-standard HTML attributes #295

cemerick opened this issue Jan 12, 2022 · 9 comments · Fixed by #296

Comments

@cemerick
Copy link
Contributor

Many Javascript libraries/frameworks depend upon HTML markup that includes non-standard attributes. Examples I've come across the past couple of days include:

Of course, tyxml rejects these attributes, e.g. this hello-world example from htmx:

<button hx-post="/clicked" hx-swap="outerHTML">
  Click Me
</button>

It is desirable to be able to use tyxml and benefit from the things it does well, while still being able to produce markup as required by libraries like these.

Conceptually, these custom attributes are basically just data-* attributes, and so there's no need for tyxml to do any kind of validation or checking on their values. All that is required is for tyxml to pass them along unharmed.

(The notion of configuring tyxml ppx and jsx-ppx (via dune ppx arguments) with a set of allowable additional attribute names was suggested in discord and discarded.)

@cemerick
Copy link
Contributor Author

Another idea would be to use e.g. an underscore prefix as an escape hatch for arbitrary attributes, not unlike the treatment of data- (or data_ in jsx) prefixes. Then the example above would be constructed via

<button _hx-post="/clicked" _hx-swap="outerHTML">
  Click Me
</button>

cemerick added a commit to cemerick/tyxml that referenced this issue Jan 12, 2022
@cemerick
Copy link
Contributor Author

I was eager to push along using htmx with my existing tyxml-based rendering setups, thus the quick PR. I understand that the approach suggested above wasn't agreed to, but it was easy enough and a pretty tiny addition that can be stacked on top of HEAD as need be (just in case it is decided that supporting non-standard attributes is a non-goal after all). 👋

@Drup
Copy link
Member

Drup commented Mar 7, 2022

I agree it's a decent idea. The syntactic marker is rather reasonable.

Another option, would be to have something of the form [@@@tyxml.foreign_prefix "hx"] at the top of the document, and then all things that start with hx- are treated as data-. Would you prefer that, or just the unstructured version with a starting underscore ?

@cemerick
Copy link
Contributor Author

cemerick commented Mar 7, 2022

I don't personally find the floating attribute option compelling; the underscore prefix happens to rhyme nicely IMO with the escape hatch for attributes with reserved keyword names like type and class. Doing more than that feels unnecessary?

FWIW, my plan was to tweak the PR to address the minor critiques you provided in #296 (comment) (once I got back to front-end-land, sometime this month hopefully). If you'd prefer to move forward with a floating attribute approach, then I can do that, but it will probably take a little longer since it is a distinct approach.

@Drup
Copy link
Member

Drup commented Mar 7, 2022

Well, _type is of a pretty different nature (it's a work around caml's reserved keywords) so the analogy doesn't hold very well.

Maybe we should have another syntactic marker.

@cemerick
Copy link
Contributor Author

cemerick commented Mar 7, 2022

Well, _type is of a pretty different nature (it's a work around caml's reserved keywords) so the analogy doesn't hold very well.

Sure, that's why I said it rhymed; in both cases, we're saying something that the underlying language(s) do not allow in their "external" form.

Maybe we should have another syntactic marker.

I'd be happy with any alternative you'd prefer. The syntactic window is pretty narrow given what refmt will allow IIRC.

cemerick added a commit to cemerick/tyxml that referenced this issue Mar 16, 2022
cemerick added a commit to cemerick/tyxml that referenced this issue Mar 16, 2022
cemerick added a commit to cemerick/tyxml that referenced this issue Mar 18, 2022
@Drup Drup closed this as completed in #296 Jun 21, 2022
Drup added a commit to Drup/opam-repository that referenced this issue Sep 29, 2023
CHANGES:

* Update for OCaml 5.0 and drop support for OCaml 4.2.0
  (ocsigen/tyxml#312 by @rr0gi)

* Add additional variants to `linktype` for the `rel` attribute
  (Leon @LogicalOverflow Vack)

* Expand options for `autocomplete` attribute on `<input>` elements
  (ocsigen/tyxml#302 by Aron @aronerben Erben)

* Fix the SVG element `<animate>` (by the way, deprecate `animation` et
  al. in favor of `animate` et al.)
  (ocsigen/tyxml#306 by Idir @ilankri Lankri)

* Add support for `dialog` element and `onclose` attribute
  (ocsigen/tyxml#301 by Julien Sagot)
* Add an escape hatch for emitting attributes with non-standard names
  in jsx or ppx code (a leading `_` character on attribute name)
  (ocsigen/tyxml#295 Chas @cemerick Emerick)
* Add support for `type` attribute on `<script>` elements
  (ocsigen/tyxml#293 by Ulrik @ulrikstrid Strid and Chas @cemerick Emerick)

* Add svg `fill-rule` attribute
  (ocsigen/tyxml#294 by Eric @dedbox Griffis)
@ethanthoma
Copy link

Hello, I am trying to use hyperscript with tyxml. My issue is that there is no support for alternatives like hs or data-hs in hyperscript (which I created an issue for here on their GitHub). However, I cannot use _ as __ becomes -. Is there no escape hatch for this case? Thanks.

@cemerick
Copy link
Contributor Author

cemerick commented Mar 1, 2024

@ethanthoma Sure, I use hyperscript, too. Replied on the issue you created on the hyperscript repo, here.

@Drup
Copy link
Member

Drup commented Mar 3, 2024

thanks @cemerick.

I must admit not being fully convinced by the whole thing, I hoped we had a more straightforward general solution ...

nberth pushed a commit to nberth/opam-repository that referenced this issue Jun 18, 2024
CHANGES:

* Update for OCaml 5.0 and drop support for OCaml 4.2.0
  (ocsigen/tyxml#312 by @rr0gi)

* Add additional variants to `linktype` for the `rel` attribute
  (Leon @LogicalOverflow Vack)

* Expand options for `autocomplete` attribute on `<input>` elements
  (ocsigen/tyxml#302 by Aron @aronerben Erben)

* Fix the SVG element `<animate>` (by the way, deprecate `animation` et
  al. in favor of `animate` et al.)
  (ocsigen/tyxml#306 by Idir @ilankri Lankri)

* Add support for `dialog` element and `onclose` attribute
  (ocsigen/tyxml#301 by Julien Sagot)
* Add an escape hatch for emitting attributes with non-standard names
  in jsx or ppx code (a leading `_` character on attribute name)
  (ocsigen/tyxml#295 Chas @cemerick Emerick)
* Add support for `type` attribute on `<script>` elements
  (ocsigen/tyxml#293 by Ulrik @ulrikstrid Strid and Chas @cemerick Emerick)

* Add svg `fill-rule` attribute
  (ocsigen/tyxml#294 by Eric @dedbox Griffis)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants