2014-09-30 56 views
1

在自定义元素中,我尝试用换行符和链接的HTML替换项目的正文文本。当我刚刚处理换行符(nl2br())时,它正在工作,但不再处理链接(linkify())时。安全地将HTML注入到聚合物模板中

get formattedBody { 
    if (item.isEmpty) return 'Loading...'; 
    return "${InputFormatter.nl2br(InputFormatter.linkify(item['body']))}"; 
    } 

    itemChanged() { 
    // Trick to respect line breaks. 
    HtmlElement body = $['body']; 
    body.innerHtml = formattedBody; 
    } 

我得到一个温暖和安全保障信息例如为:

Removing disallowed attribute <A href="http://miamiherald.typepad.com/the-starting-gate/2014/09/news-.html"> 

我也试过setInnerHtml(),无济于事。

想法?谢谢!

回答

2

我有一个<safe-html>元素工作。在Chrome上进行验证,甚至在Safari上进行后dart2js验证。如果你想让我把它变成pub.dartlang.org上的一个lib,请将它解决。

用法:

<safe-html validator="{{nodeValidator}}">{{someHtml}}</safe-html> 

(在你自己的传递validator是可选如果没有它,我们将使用默认的。)

safe_html.html:

<link rel="import" href="../../../../../../../packages/polymer/polymer.html"> 
<polymer-element name="safe-html"> 
    <template> 
    <div id="container"></div> 
    </template> 

    <script type="application/dart" src='safe_html.dart'></script> 
</polymer-element> 

safe_html.dart:

library safe_html; 

import 'dart:async'; 
import "dart:html"; 

import "package:polymer/polymer.dart"; 

@CustomTag("safe-html") 
class SafeHtml extends PolymerElement { 
    @published NodeValidator validator = new NodeValidatorBuilder() 
    ..allowHtml5(uriPolicy: new DefaultUriPolicy()); 

    SafeHtml.created() : super.created(); 

    addFragment() { 
    DivElement container = $['container']; 
    String fragment = this.text; 
    container.setInnerHtml(fragment, // Set the fragment in a safe way. 
     validator: validator); 
    this.text = ""; // Clear the original fragment passed to the element. 
    } 

    attached() { 
    addFragment(); 
    } 
} 

class DefaultUriPolicy implements UriPolicy { 
    DefaultUriPolicy(); 

    // Allow all external, absolute URLs. 
    RegExp regex = new RegExp(r'(?:http://|https://|//)?.*'); 

    bool allowsUri(String uri) { 
    return regex.hasMatch(uri); 
    } 
} 

如果选择通过自己的NodeValidator,做到在使用<safe-html>父元素指定一个getter:

NodeValidator get nodeValidator => new NodeValidatorBuilder() 
..allowHtml5(uriPolicy: new ItemUrlPolicy()); 

正如你可以看到我引用UriPolicy我保持像uri_policy一个单独的文件。dart:

import 'dart:html'; 

class ItemUrlPolicy implements UriPolicy { 
    ItemUrlPolicy(); 

    RegExp regex = new RegExp(r'(?:http://|https://|//)?.*'); 

    bool allowsUri(String uri) { 
    return regex.hasMatch(uri); 
    } 
} 

在GünterZöchbauer和SO上其他有用帖子的作者的许多帮助下。

+0

不应该足以确保'html'不是'null'吗? 'content.setInnerHtml(html,...'之前的if(html == null)html =''' – 2014-10-01 04:13:48

+0

不完全是因为'html'不是'null',它是'this.text = '';'''''''''''''''或者类似的尝试https://github.com/woven/dart-communities/blob/3450a02141e5c748efde178adb7b26dbdcf37573/lib/src/client/components/widgets/safe_html/safe_html.dart这就像它写了一个'null'右里面''之前其他的东西打印 - 只在Safari发布后的Safari浏览器。 – 2014-10-01 04:20:54

+0

我没有Safari来测试,但是你在'this.text ='';'之后分配'html'什么是'this.text'为什么你要用''?''不应该有内容。''只是聚合物的一个占位符,知道在哪里投射聚合物元素的孩子。 – 2014-10-01 04:26:07

3

您需要通过一个配置为允许特定标签和属性的NodeValidator

此问题HTML Tags Within Internationalized Strings In Polymer.dart包含允许使用聚合物表达式传递HTML的自定义元素的示例。该实施演示如何使用NodeValidator

而不是..allowTextElements()您可以允许其他元素/属性或自定义元素/属性的集合。自动完成应显示可能的选项。

参见
- Dart, why does using innerHtml to set shadow root content work but appendHtml doesn't?
- Dart Removing disallowed attribute after editor upgraded
- Mustache Template usage in Dart
- in dart parse HTML string to DOM

我得到了它与

NodeValidator nodeValidator = new NodeValidatorBuilder() 
    ..allowNavigation(new MiamiHeraldUrlPolicy()); 

aContainer.setInnerHtml('<a href="http://miamiherald.typepad.com/the-starting-gate/2014/09/news-.html">bla</a>', 
    validator: nodeValidator); 
class MiamiHeraldUrlPolicy implements UriPolicy { 
    MiamiHeraldUrlPolicy(); 

    RegExp regex = new RegExp(r'(?:http://|https://|//)?miamiherald.typepad.com/.*'); 

    bool allowsUri(String uri) { 
    return regex.hasMatch(uri); 
    } 
} 
工作
+0

你会得到什么输出?当标签/属性被删除时,它会记录到控制台。 – 2014-09-30 06:36:46

+0

即使是更自由的'allowHtml5()'我也可以:删除不允许的属性 + ItemActivities 删除不允许的属性 2014-09-30 06:37:55

+0

FWIW:https://github.com/woven/ dart-communities/commit/fe9fe3f6b937aad34bad9c4efade7ddfa9a73dd9关闭睡觉,很快返回。 – 2014-09-30 06:51:46