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

Runtime error: passing empty string to Html.Attributes.attribute #46

Closed
strickinato opened this issue Jun 29, 2016 · 8 comments
Closed

Comments

@strickinato
Copy link

strickinato commented Jun 29, 2016

import Html exposing (div, text)
import Html.Attributes exposing (attribute)

main =
  div [ attribute "" "" ] [ text "boom!" ]

Gives:

Main.elm:6123 Uncaught InvalidCharacterError: Failed to execute 'setAttribute' on 'Element': '' is not a valid attribute name.

Using:

    "dependencies": {
        "elm-lang/core": "4.0.1 <= v < 5.0.0",
        "elm-lang/html": "1.1.0 <= v < 2.0.0"
    },
@process-bot
Copy link

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

@evancz
Copy link
Member

evancz commented Jun 30, 2016

What should happen instead? Is it worth it to add a check on every setAttribute that ever happens under any circumstances?

@rtfeldman
Copy link
Member

I'd say the same thing here as what I said over there. 😄

@strickinato
Copy link
Author

Agree - https://github.com/elm-lang/html/issues/47#issuecomment-229562502 fits in well with what I'd expect- given that it can fail

@douglascorrea
Copy link

As commented in #47, setAttribute can raise INVALID_CHARACTER_ERR DOMException, but also can raise NO_MODIFICATION_ALLOWED_ERR, which not fits in this error case. And I think @rtfeldman suggestion fits here too.

@evancz
Copy link
Member

evancz commented Jul 13, 2016

I think the same resolution from #47 makes sense here as well, so I am going to close. I am not certain this is the right thing, but I don't think we have a workable alternative in either case at this point in time.

@strickinato
Copy link
Author

Hi!! Me again, but now in 2020.

I just ran into this same issue, and was shocked to find myself from 4 years ago! Hi me of the past!!

So here's some information for myself in the future, or for anyone else who lands here:

This time (and I assume last time), I hit this because I was trying to conditionally add an attribute to an element, like:

Html.div
    [ if importantThing then
        Html.Event.onClick GoWild
      else
        Html.Attribute.attribute "" ""
    ]
    []

Seems like people are talkin' about doing stuff like this all over!

It turns out though, that a nice way to add an attribute that has no effect on the DOM is to add a blank class.

attributeIf : Bool -> Html.Attribute msg -> Html.Attribute msg
attributeIf condition attr =
    if condition then
        attr
    else
        Html.Attributes.class ""
...

Html.div
    [  attributeIf importantThing (Html.Event.onClick GoWild) ] 
    []

Anyway, see you again in 4 years future Aaron!

@rtfeldman
Copy link
Member

This comment totally made my day 😂

Miss ya, @strickinato!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants