virtual-dom icon indicating copy to clipboard operation
virtual-dom copied to clipboard

Empty string as attribute name causes exception.

Open Carlsson87 opened this issue 5 years ago • 7 comments

I just upgraded to version 1.0.1 and ran in to problems when using Html.attribute "" "". It didn't use to cause an exception, and now it does. Let me know if you need more info.

SSCCE: https://ellie-app.com/3kjwsTXvQNTa1

Carlsson87 avatar Sep 13 '18 08:09 Carlsson87

Caused by https://github.com/elm/virtual-dom/commit/d32f9b4a2f6ceb1d30097818e39b33d3435bafe0

Probably fix would be to also check if attribute key is not empty, since that is not allowed.

norpan avatar Sep 13 '18 10:09 norpan

Here is @Carlsson87's sscce:

module Main exposing (main)

import Html
import Html.Attributes

main =
    Html.div [ Html.Attributes.attribute "" "" ] [] 

harrysarson avatar Sep 13 '18 10:09 harrysarson

To be extra specific. The problem seems to occur when the key is an empty string. The value being an empty string does not cause any problems for me.

Carlsson87 avatar Sep 13 '18 11:09 Carlsson87

Why are you doing this?

evancz avatar Sep 13 '18 14:09 evancz

When I conditionally want to set an attribute on an element. I use the same approach with Html.text "" when I want to conditionally render some elements.

showButton : Maybe msg -> Html msg
showButton clickMsg =
    Html.button
        [ case clickMsg of
            Just msg ->
                Html.Events.onClick msg
            Nothing ->
                Html.Attributes.attribute "" ""
        ]
        [ Html.text "Click here"
        ]

I'd love to know if there is a better way.

Carlsson87 avatar Sep 13 '18 16:09 Carlsson87

We use a similar pattern for conditional attributes, we have it defined as

none : Attribute msg
none =
    Html.Attributes.property "" Json.Encode.null

Which seems to be valid still https://ellie-app.com/3ks2h7XJWnTa1

hpate-omicron avatar Sep 13 '18 16:09 hpate-omicron

~@norpan how is d32f9b4 causing this bug?~ ah ok. I understand now. The bug was present before d32f9b4 for all cases Html.Attributes.attribute "" val except for val = "". Now using an empty string also triggers the bug.

reiner-dolp avatar Oct 14 '18 20:10 reiner-dolp