Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用聚合物将HTML注入模板_Javascript_Json_Polymer - Fatal编程技术网

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

Javascript 如何使用聚合物将HTML注入模板,javascript,json,polymer,Javascript,Json,Polymer,我使用polymer jsonp来执行jsonp请求,但响应有时包含html 例如,假设post.content是“Foobar”,如何显示{{post.content}},使“Foo”以粗体显示 <polymer-element name="feed-element" attributes=""> <template> <template repeat="{{post in posts.feed.entry}}"> <p>

我使用polymer jsonp来执行jsonp请求,但响应有时包含html

例如,假设post.content是
Foobar”
,如何显示{{post.content}},使
“Foo”
以粗体显示

<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>

{{post.content}

聚合物(“进料元件”{ 已创建:函数(){}, 附件:函数(){}, 已分离:函数(){}, attributeChanged:函数(attrName、oldVal、newVal){} });
Polymer将不会通过数据绑定为未替换的HTML添加戳记,因为它会成为XSS攻击的漏洞

目前正在讨论如何在有限的环境下标记HTML,或者允许定制过滤,但这还没有在数据层实现

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

下面是一个显示此技术的示例:


对于那些寻找聚合物1.0的人

<dom-module id="html-echo">
  <style>
    :host {
      display: block;
    }
  </style>
  <template>
  </template>
</dom-module>

<script>
  (function () {
    Polymer({
      is: 'html-echo',
      properties: {
        html: {
          type: String,
          observer: '_htmlChanged'
        }
      },
      _htmlChanged: function (neo) {
        // WARNING: potential XSS vulnerability if `html` comes from an untrusted source
        this.innerHTML = neo;
      }
    });
  })();
</script>

:主持人{
显示:块;
}
(功能(){
聚合物({
是:“html echo”,
特性:{
html:{
类型:字符串,
观察者:“\u htmlChanged”
}
},
_htmlChanged:函数(neo){
//警告:如果“html”来自不受信任的源,则可能存在XSS漏洞
this.innerHTML=neo;
}
});
})();

如果您确定这是您想要做的,只需使用
innerHTML

_renderHtml: function(html) {
  this.$.dynamicHtmlContainer.innerHTML = html;
}
或动态添加阴影dom子级:

_renderHtml: function(html) {
  var div = document.createElement('div');
  div.innerHTML = html;
  Polymer.dom(this).appendChild(div);
}

我认为Polymer 2.0中的
Polymer.dom
正在被删除。

Scott使用Flickr的jsonp API和
的技术示例:多汁的html是否也容易受到XSS攻击?我想答案是肯定的,但只是checking@Scott,您是否可以进一步说明是否需要其他元素才能正常工作?您不需要其他元素。您始终可以执行
this.innerHTML=…
。我们的想法是,我们不能允许您直接将数据绑定到HTML,而不做一些事情来显式地进入危险区域。有些人使用的预处理器试图明确禁止
innerHTML
,而其他团队则需要输入净化程序。有多种解决方案,我们可以在这方面进行改进。请记住,由于绑定是在预呈现时静态计算的,因此您的命令式声明,如
[[foo]]]
{bar}
无法在新html中工作,因为它们是在渲染后添加的。如何在此方法中调用渲染后?
this.innerHTML=postProcess(neo)
这适合你吗?@Ali.MDI认为在Polymer 1.0中,标签应该在标签内,否则我希望我能+1这个答案两次。你救了我这么多心痛;谢谢。你可以做得很简单:>(彼得)