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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭