How can I get special characters using elm-html module?

后端 未结 5 1235
伪装坚强ぢ
伪装坚强ぢ 2021-02-02 12:58

Disclaimer: I\'m brand new to Elm

I\'m fiddling around with the online Elm editor and I\'ve run into an issue. I can\'t find a way to get certain special charac

相关标签:
5条回答
  • 2021-02-02 13:42

    I recently created an Elm package that solves this. If you use text' "©" it'll render the copyright symbol © instead of the escape code. Also works with "©" and "©". Hope this helps!

    0 讨论(0)
  • 2021-02-02 13:44

    Why this is (intentionally) not so easy

    The "makers" of Elm are understandably reluctant to give us a way to insert "special" characters into HTML text. Two main reasons:

    1. This would open a "text injection" hole where a malicious user could insert any HTML tags, even JavaScript code, into a Web page. Imagine if you could do that in a forum site like Stack Overflow: you could trick anyone reading your contribution into executing code of your choosing in their browser.
    2. Elm works hard to produce optimal DOM updates. This only works with the content of tags that Elm is aware of, not with text that happens to contain tags. When people insert text containing HTML tags in an Elm program, there end up being parts of the DOM that can't be optimized.

    How it's possible anyway

    That said, the Elm user community has found a loophole that affords a workaround. For the reasons above, it's not recommended, especially not if your text is non-constant, i.e. comes from a source outside your program. Still, people will be wanting to do this anyway so I'm going to document it to save others the trouble I had digging everything up and getting it working:

    1. If you don't already have it,
      import Json.Encode exposing (string)
      This is in package elm-lang/core so it should already be in your dependencies.
    2. Similarly,
      import Html.Attributes exposing (property)
    3. Finally, create a tag having a property "innerHTML" and a JSON-Value representation of your text, e.g.:
      span [ property "innerHTML" (string " ") ] []
    0 讨论(0)
  • 2021-02-02 13:54

    You don't need to hunt the symbols, you can get them from a list like this one.

    If it's too bothersome to copy & paste, you can also create a helper function that you can use with your escaped characters like this:

    import Html exposing (..) 
    import String
    
    htmlDecode str = 
      let 
        replace (s1, s2) src= String.join s2 <| String.split s1 src    
        chrmap = 
          [ ("&reg;", "®")
          , ("&copy;", "©" )
          ] 
      in  
        List.foldl replace str chrmap
    
    main = text <| htmlDecode "hello &copy;" 
    
    0 讨论(0)
  • 2021-02-02 13:57

    You can use directly the unicode escape code in Elm strings and chars:

    We have a util module containing all our special chars like:

    module Utils.SpecialChars exposing (noBreakSpace)
    
    noBreakSpace : Char
    noBreakSpace = '\x00A0'
    

    Which can be used as:

    let
      emptyLabel = String.fromChar noBreakSpace
    in
    label []
        [ span [ ] [ text emptyLabel ]
        ]
    

    This will render a <span>&nbsp;</span>

    0 讨论(0)
  • 2021-02-02 14:02

    I found, that there is a better solution: you can convert special characters from Unicode to char, and then create a string from char:

    resString = String.fromChar (Char.fromCode 187)
    
    0 讨论(0)
提交回复
热议问题