Discussion:
[elm-discuss] Making useless HTML Attributes impossible
h***@gmail.com
2017-07-01 18:35:40 UTC
Permalink
Hi everyone.

It's been some time that I'm thinking about type checking HTML Attributes to ensure at compile time that useless attributes can't be set on HTML. For instance, it makes no sense to set Html.Attributes.src to an h1 title.

I was imagining simple cases :

Html.a [ Html.Attributes.href "#" ] [] => compile
Html.a [ Html.Attributes.src "#" ] [] => does not compile
Html.img [ Html.Attributes.src "#"] [] => compile
Html.iframe [ Html.Attributes.src "#" ] [] => compile

Obviously, this behavior should be extended to every attributes on every HTML elements. 🙂
I was thinking about Phantom Types, but I can't figure a correct way to do it.

Anyone has an idea on what I should give a try?

Just in case, I know the cost to do it will probably be higher than what it actually, but, hey! Error checking at compile time is so good that no trying it would be too bad!
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Peter Damoc
2017-07-02 04:33:19 UTC
Permalink
Post by h***@gmail.com
It's been some time that I'm thinking about type checking HTML Attributes
to ensure at compile time that useless attributes can't be set on HTML. For
instance, it makes no sense to set Html.Attributes.src to an h1 title.
To my understanding this restriction would have to be expressed in the type
signature.
How would the type signature for the `h1` would look like exactly?
--
There is NO FATE, we are the creators.
blog: http://damoc.ro/
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
David Andrews
2017-07-02 05:22:28 UTC
Permalink
We could define a type for the attributes of each html element and pass
that as the list of attributes when defining the Html.

Html/Attributes.elm
type A msg = Href String | OnClick msg | ...

Html.elm
a : (List Html.Attributes.A msg) -> (List Html msg) -> Html msg

main.elm
Html.a [Html.Attributes.Href "#"] []

This would cause some naming woes for us, though, since both image and
iframe couldn't both have attributes named Src in the same package. We
would either have to move them to separate packages or name them ImgSrc and
IframeSrc or the like.
Post by Peter Damoc
Post by h***@gmail.com
It's been some time that I'm thinking about type checking HTML Attributes
to ensure at compile time that useless attributes can't be set on HTML. For
instance, it makes no sense to set Html.Attributes.src to an h1 title.
To my understanding this restriction would have to be expressed in the
type signature.
How would the type signature for the `h1` would look like exactly?
--
There is NO FATE, we are the creators.
blog: http://damoc.ro/
--
You received this message because you are subscribed to the Google Groups
"Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Steve Schafer
2017-07-02 14:30:30 UTC
Permalink
Putting aside the practicality for a moment, a couple of things:


1. A distinction needs to be made between merely senseless and
prohibited by spec.For example, the *hr* element allows the *lang*
attribute, even though it makes no sense. We should not prohibit attributes
that make no sense if the spec allows them.
2. Whether or not an attribute is allowed by spec is not always simple.
For example, on the *a* element, the *target* attribute is permitted,
but only if the *href* attribute is also present.


-Steve
Post by h***@gmail.com
Hi everyone.
It's been some time that I'm thinking about type checking HTML Attributes
to ensure at compile time that useless attributes can't be set on HTML. For
instance, it makes no sense to set Html.Attributes.src to an h1 title.
Html.a [ Html.Attributes.href "#" ] [] => compile
Html.a [ Html.Attributes.src "#" ] [] => does not compile
Html.img [ Html.Attributes.src "#"] [] => compile
Html.iframe [ Html.Attributes.src "#" ] [] => compile
Obviously, this behavior should be extended to every attributes on every
HTML elements. 🙂
I was thinking about Phantom Types, but I can't figure a correct way to do it.
Anyone has an idea on what I should give a try?
Just in case, I know the cost to do it will probably be higher than what
it actually, but, hey! Error checking at compile time is so good that no
trying it would be too bad!
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Zachary Kessin
2017-07-03 06:54:46 UTC
Permalink
I think some of this would require a dependent type system, which we don't
have. And while in this case, it would be really nice I can't imagine that
it is worth it.

Maybe a post processor that could do HTML sanity testing would be useful? I
can imagine using the Elm AST package and some kind of rules engine you
could build something like this, but it would be a different set of
constraints

Zach
ᐧ
Post by Steve Schafer
1. A distinction needs to be made between merely senseless and
prohibited by spec.For example, the *hr* element allows the *lang*
attribute, even though it makes no sense. We should not prohibit attributes
that make no sense if the spec allows them.
2. Whether or not an attribute is allowed by spec is not always
simple. For example, on the *a* element, the *target* attribute is
permitted, but only if the *href* attribute is also present.
-Steve
Post by h***@gmail.com
Hi everyone.
It's been some time that I'm thinking about type checking HTML Attributes
to ensure at compile time that useless attributes can't be set on HTML. For
instance, it makes no sense to set Html.Attributes.src to an h1 title.
Html.a [ Html.Attributes.href "#" ] [] => compile
Html.a [ Html.Attributes.src "#" ] [] => does not compile
Html.img [ Html.Attributes.src "#"] [] => compile
Html.iframe [ Html.Attributes.src "#" ] [] => compile
Obviously, this behavior should be extended to every attributes on every
HTML elements. 🙂
I was thinking about Phantom Types, but I can't figure a correct way to do it.
Anyone has an idea on what I should give a try?
Just in case, I know the cost to do it will probably be higher than what
it actually, but, hey! Error checking at compile time is so good that no
trying it would be too bad!
--
You received this message because you are subscribed to the Google Groups
"Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Zach Kessin
Teaching Web Developers to test code to find more bugs in less time
Skype: zachkessin
+972 54 234 3956 / +44 203 734 9790 / +1 617 778 7213
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...