小编典典

如何使用聚合物将HTML注入模板

json

我正在使用polymer-jsonp执行JSONP请求,但响应有时包含html。

例如,假设post.content为"<strong>Foo</strong> bar",我如何"Foo"以粗体显示{{post.content}}

<polymer-element name="feed-element" attributes="">
  <template>
    <template repeat="{{post in posts.feed.entry}}">
      <p>{{post.content}}</p>
    </template>
    <polymer-jsonp url="url" response="{{posts}}"></polymer-jsonp>
  </template>
  <script>
    Polymer('feed-element', {
      created: function() { },
      attached: function() { },
      detached: function() { },
      attributeChanged: function(attrName, oldVal, newVal) { }
    });
  </script>
</polymer-element>

阅读 228

收藏
2020-07-27

共1个答案

小编典典

Polymer不会通过数据绑定标记未转义的HTML,因为它成为XSS攻击的漏洞。

关于在有限的情况下标记HTML或允许进行自定义过滤的讨论正在进行,但是在数据层尚未实现。

现在可以使用附加的自定义元素来做您想做的事情,但是再次提醒您,如果将不受信任的HTML呈现到页面中,则可能会发生不良情况。

这是显示此技术的示例:

http://jsbin.com/durajiwo/1/edit

2020-07-27