du20150401 2015-01-12 16:45
浏览 37
已采纳

在HTML中取消转义CSS输入

How do I unescape html?

I'm passing a css file into html like this

<style>{{.file}}</style>

I get this

<style>ZgotmplZ</style>

I've tried wrapping the field with template.HTML(data), didn't work.

  • 写回答

1条回答 默认 最新

  • doudang4568 2015-01-12 16:55
    关注

    The Go HTML template package properly excapes CSS. Quoting from the documentation of the template package:

    The escaping is contextual, so actions can appear within JavaScript, CSS, and URI contexts.

    "ZgotmplZ" is a special value, it is used as a replacement if the value you're trying to include is invalid or unsafe in the context.

    So the problem is the CSS value you're trying to include, it is not safe. Try something simple first and see if it works, like:

    body {background-color: #000}
    

    Found the discussion of "ZgotmplZ" in the documentation (at type ErrorCode), quoting it:

    "ZgotmplZ" explanation:

    Example Template:

    <img src="{{.X}}">
    where {{.X}} evaluates to `javascript:...`
    

    Discussion:

    "ZgotmplZ" is a special value that indicates that unsafe content reached a
    CSS or URL context at runtime. The output of the example will be
      <img src="#ZgotmplZ">
    If the data comes from a trusted source, use content types to exempt it
    from filtering: URL(`javascript:...`).
    

    Solution

    Since the code you try to insert is in the context of CSS code and not HTML, you can't/shouldn't use template.HTML(data).

    There is a predefined type CSS for safe inclusion of CSS code coming from trusted source, e.g. CSS code you specify and is not coming from an HTML form filled by the user. Example:

    var safeCss = template.CSS(`body {background-image: url("paper.gif");}`)
    

    And pass the safeCss value to your template parameter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算