appendHtml()不附加完整的HTML - Dart [英] appendHtml() doesn't append the full HTML - Dart

查看:149
本文介绍了appendHtml()不附加完整的HTML - Dart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在DartPad中正常工作,如下所示:

The following code works as expected in DartPad, demonstrated below:

void main() {
  Element e = querySelector('table');

  String someValue = 'hello, world';
  int anotherValue = 23958;

  Element row = new Element.tr()
    ..appendHtml('''
      <td>$someValue</td>
      <td>$anotherValue</td>
    ''');

  e.append(row);
}

DartPad

但是,当我使用dart2js( dart2js -c -o app .js app.dart )并在同一页面上运行它,创建的< td> 被完全删除,与:

However, when I compile the same code using dart2js (dart2js -c -o app.js app.dart) and run it on the same page, the created <td>'s are completely removed, and I end up with:

<table>
  <tr>hello, world 23958</tr>
</table>

同样的问题发生时,实际的.dart文件在脚本src =...> 与Dartium(v39.0.2171.0 x64)。我在Dart v1.11.1上。

The same issue occurs when the actual .dart file is used in <script src="..."> with Dartium (v39.0.2171.0 x64). I'm on Dart v1.11.1.

一些测试: b
$ b

Some testing:

..appendHtml('''
  <td></td>
  <td>hi</td>
''');

产生:

<table>
  <tr>hi</tr>
</table>

这与上述相同:

..appendHtml('<td>hi</td>');

我可以得到的只有我想要的是:

The only way I could get it to give me what I want is:

..append(new Element.td()..text = someValue)
..append(new Element.td()..text = anotherValue.toString());


推荐答案

Dart 1.11更改了appendHTML以清除HTML输入。

Dart 1.11 made a change to appendHTML to sanitize the HTML input.

为了避免这种情况,你可以通过一个不做任何事情的消毒剂。

To avoid this, you can pass a sanitizer that does nothing.

class MySanitizer implements NodeTreeSanitizer {
  void sanitizeTree(Node node) {}
}

document.body.appendHtml('<td>fish</td>', treeSanitizer: new MySanitizer());

这篇关于appendHtml()不附加完整的HTML - Dart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆