如何使用elm-html模块获取特殊字符? [英] How can I get special characters using elm-html module?

查看:38
本文介绍了如何使用elm-html模块获取特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我是Elm的新手

我在摆弄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 characters (copyright, trademark, etc.) to show up. I tried:

import Html exposing (text)

main =
  text "©"

显示的全部是实际文本& 。我还尝试使用Unicode字符 \u00A9 ,但这最终给了我一个语法错误:

All that showed up was the actual text ©. I also tried to use the unicode character for it \u00A9 but that ended up giving me a syntax error:

(line 1, column 16): unexpected "u" expecting space, "&" or escape code

我发现的唯一方法是实际访问某人的网站并复制/粘贴其版权进入我的应用的符号:

The only way I found was to actually go to someone's website and copy/paste their copyright symbol into my app:

import Html exposing (text)

main =
  text "©"

这有效,但是我宁愿能够快速键入这些字符而不是不得不寻找其他网站上的实际符号。在Elm中返回HTML时,是否存在一种首选/推荐的获取非转义文本的方法?

This works, but I would much rather be able to type these characters out quickly instead of having to hunt down the actual symbols on other websites. Is there a preferred/recommended method of getting non-escaped text when returning HTML in Elm?



编辑:

专门针对Mac:


  • option + g可以提供©

  • 选项+2为您提供

  • option + r为您提供®

  • option+g gives you ©
  • option+2 gives you
  • option+r gives you ®

全部在在线编辑器中进行了测试,并且他们工作了。这仍然不能解决核心问题,但是对于这些特定的特殊字符来说,它只是一个值得注意的东西。

All tested in the online editor and they worked. This still doesn't attack the core issue, but it's just something nice to note for these specific special characters.

推荐答案

为什么这样做(有意地)不太容易

榆木的制造者不愿给我们一种方法在HTML文本中插入特殊字符。主要原因有两个:

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


  1. 这将打开一个文本注入漏洞,恶意用户可以在其中插入任何HTML标记,甚至包括JavaScript代码。一个网页。想象一下,如果您可以在Stack Overflow之类的论坛站点中做到这一点:您可以欺骗任何阅读您的贡献的人,在他们的浏览器中执行您选择的代码。

  2. Elm努力产生最佳的DOM更新。 。这仅适用于Elm知道的标签内容,不适用于恰好包含标签的文本。当人们在Elm程序中插入包含HTML标记的文本时,最终会导致DOM的某些部分无法优化。

无论如何都是可能的

Elm用户社区发现了一个漏洞,可以提供解决方法。由于上述原因,我们不建议您尤其是如果您的文本不是恒定的,即不是来自程序外部的。尽管如此,人们还是会想这样做,所以我要记录在案,以免其他人挖到一切都无法正常工作:

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. 如果还没有,

    import Json.Encoding暴露(字符串)

    这在软件包 elm-lang / core 中,因此它应该已经存在于您的依赖项中。

  2. 类似地,

    导入Html.Attributes(属性)

  3. 最后,创建一个具有属性 innerHTML的标签和文本的JSON值表示形式,例如:

    span [属性 innerHTML(字符串& nbsp;)] []

  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 " ") ] []

这篇关于如何使用elm-html模块获取特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆