# Valid code with Runtime exception

**URL:** <https://discourse.elm-lang.org/t/valid-code-with-runtime-exception/8787>\
**Category:** Show and Tell\
**Created:** [November 28, 2022, 5:01pm UTC](https://discourse.elm-lang.org/t/valid-code-with-runtime-exception/8787 "2022-11-28T17:01:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andre-dietrich](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/andre-dietrich/32/4106_2.png) [@andre-dietrich](https://discourse.elm-lang.org/u/andre-dietrich)\
**Post date:** [November 28, 2022, 5:01pm UTC](https://discourse.elm-lang.org/t/valid-code-with-runtime-exception/8787/1 "2022-11-28T17:01:03Z")

</div>

Actually I was playing around with RDFa and Microdata as HTML attributes and by accident I discovered a bug, which is actually not a bug, since no one would use such a parameter. I was just wondering if, someone else had discovered this and how HTML parsers, written in elm might reproduce this error:

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

main =
  div [attribute "@style" "color: red"]
      [text "Hello!"]

```

Unfortunately I cannot post a screenshot of the result, but it looks like this:

```auto
Initialization Error
InvalidCharacterError: String contains an invalid character

> Uncaught DOMException: String contains an invalid character

```

… which is the same as if you use javascript `elem.setAttribute("@style", "color: red")` …

---

<div class="post-metadata">

**Author:** ![andre-dietrich](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/andre-dietrich/32/4106_2.png) [@andre-dietrich](https://discourse.elm-lang.org/u/andre-dietrich)\
**Post date:** [November 28, 2022, 5:01pm UTC](https://discourse.elm-lang.org/t/valid-code-with-runtime-exception/8787/2 "2022-11-28T17:01:50Z")

</div>

I mean, it is not just the warning, it crashes the entire program …

---

<div class="post-metadata">

**Author:** ![lydell](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lydell/32/178_2.png) [@lydell](https://discourse.elm-lang.org/u/lydell)\
**Post date:** [November 28, 2022, 6:31pm UTC](https://discourse.elm-lang.org/t/valid-code-with-runtime-exception/8787/3 "2022-11-28T18:31:36Z")

</div>

Yep, the DOM is very tricky. It’s possible to crash things when using the functions that accept strings. Luckily, we can use the predefined `Html.foo` and `Html.Attributes.foo` functions 99 % of the time so we stay safe.

If you search for “runtime” in [elm/html](https://github.com/elm/html/issues?q=runtime+is%3Aopen) and [elm/virtual-dom](https://github.com/elm/html/issues?q=runtime+is%3Aopen) you’ll see a couple of open issues.

Here’s one issue showing that’s it’s difficult to “fix” or unclear how/if to fix sometimes: [Runtime error: passing empty string to Html.Attributes.attribute · Issue #46 · elm/html · GitHub](https://github.com/elm/html/issues/46)

* * *

It’s interesting that if you put `<div @style="color: red">Hello!</div>` in an HTML file, the browser will parse it. You can do `.getAttribute("@style")` to get `"color: red"` back, but `.setAttribute("@style", "foo")` throws an error (like you say). So the browser both does and does not let me use that at sign!

Which format uses `@style`?

---

<div class="post-metadata">

**Author:** ![andre-dietrich](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/andre-dietrich/32/4106_2.png) [@andre-dietrich](https://discourse.elm-lang.org/u/andre-dietrich)\
**Post date:** [November 29, 2022, 10:20am UTC](https://discourse.elm-lang.org/t/valid-code-with-runtime-exception/8787/4 "2022-11-29T10:20:53Z")

</div>

I was trying out RDFa, within a book they used some `@href` and `@hrel` syntax within HTML, which does not seem to be correct, looking into the spec, which refers with `@property` to the properties itself.

But thanks for answering, it was just strange from a first glance, that a successfully compiled Elm-program crashed, after 4 years …

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [November 29, 2022, 11:40am UTC](https://discourse.elm-lang.org/t/valid-code-with-runtime-exception/8787/5 "2022-11-29T11:40:17Z")

</div>

I suppose you could have:

```auto
module Html.Attribute exposing (..)

attribute : String -> String -> Maybe (Attribute msg)

```

and

```auto
module Html exposing (..)

node : String -> List (Maybe (Attribute msg)) -> List (Html msg) -> Html msg

```

So that doing this:

```auto
Html.node attrs innerHtml

```

becomes equivalent to doing this currently:

```auto
Html.node (attrs |> List.filterMap identity) innerHtml

```

I use `List.filterMap identity` on attribute lists quite often, since we don’t have an `Attribute.none`, although some people prefer to use an empty class name to cover that.

That would allow runtime checking of attribute names to ensure they are valid. On the other hand, its a big change to swallow from how things currently work.

A better way might be to have `elm/virtual-dom` internally have its own representation of a badly named attribute, and then silently ignore them. Changing the internal representation of attributes would yield a convenient place to implement `Attribute.none` too.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [December 9, 2022, 11:40am UTC](https://discourse.elm-lang.org/t/valid-code-with-runtime-exception/8787/6 "2022-12-09T11:40:23Z")

</div>

Closed on December 9, 2022, 11:40am UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
