As a way to learn Elm I’m building a simple random quote application that I’ve previously built in JS. So far, I can receive a JSON response with the following format, and I’ve decoded the content field.
[
{
"ID": 1564,
"title": "Henry Ford",
"content":
"<p>You can’t build a reputation on what you’re going to do.</p>\n",
"link": "https://quotesondesign.com/henry-ford-4/"
}
]
In JS I would be able to inject the content string into the DOM as HTML, taking care of the escaped codes, p-tags and other special characters. Is there a simple way in Elm to achieve the same effect?
elm-community/html-extra provides innerHtml.
But be aware of the security implications (XSS) of doing this, especially if the content is user provided.
But since this is just a project for learning, this seems to be ok.
module Main exposing (main)
import Html exposing (Html, p, text)
import Html.Attributes exposing (property)
import Json.Encode as Json
main : Html msg
main =
p
[ property "innerHTML"
(Json.string "Hello <b>unsafe</b> <em>HTML</em>")
]
[]
Hi, for anyone who currently has the same problem/question:
The html-extra library does not have the innerHtml method in its last version and the “innerHTML” html property does not work in Elm 0.19
Related issue and alternative solution here (using Html.Parser package):