robb / Swim

A DSL for writing HTML in Swift
310 stars 9 forks source link

Missing media attribute for the meta tag #31

Open kevinrenskers opened 3 years ago

kevinrenskers commented 3 years ago

The "media" attribute is not available for meta tags, so something like this is currently impossible:

<meta name="theme-color" 
      content="#ecd96f" 
      media="(prefers-color-scheme: light)">
<meta name="theme-color" 
      content="#0b3e05" 
      media="(prefers-color-scheme: dark)">

Would you want me to create a PR?

kevinrenskers commented 3 years ago

Luckily it is possible after all with customAttributes 😅

chriseidhof commented 3 years ago

That's what I've done as well for similar issues. I think the attributes are generated automatically from a list (here), but I'm not sure how @robb compiled that list or how we can add stuff.

In my code base, I used property, label, autocorrect and some custom data properties. Using custom attributes works fine for that, although I also don't mind creating a PR.

robb commented 3 years ago

To add properties, you would have to add them to the extension on Attribute in https://github.com/robb/Swim/blob/main/Generator/Sources/Generator/Definitions.swift, then add them to the Tag definition, e.g.:

 static let meta = Tag(
         name: "Meta",
         description: "Text metadata.",
-        attributes: .charset, .content, .httpEquiv, .name5
+        attributes: .charset, .content, .httpEquiv, .name5, .media
         properties: .emptyElement
     )

(If you do, please keep everything alphabetized)

I took the list from the WHATWG, don't remember exactly which version but something like this: https://html.spec.whatwg.org/review-drafts/2020-01/#elements-3

Not super happy with the current state of things, maybe this could be ported to a couple of XML or JSON files to generate the attributes from? Part of the complexity lies in different semantics for attributes with the same name on different tags (e.g. name vs name) and that some of the definitions are in different specs, e.g. role is defined in the ARIA spec.

Ideally, we would also be able to automatically perform safe conversions, e.g. from URL or Int to String but so far, it did seem to be a huge problem in practice.

robb commented 3 years ago

Luckily it is possible after all with customAttributes 😅

Yup, I think having an escape hatch is always important for this kind of API design.