appendHtml()不会附加完整的HTML-Dart

T.J*_*ika 2 html javascript dart

以下代码在DartPad中按预期工作,如下所示:

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);
}
Run Code Online (Sandbox Code Playgroud)

飞镖盘

但是,当我使用dart2js(dart2js -c -o app.js app.dart)编译相同的代码并在同一页面上运行该代码时,创建<td>的完全被删除,最终得到:

<table>
  <tr>hello, world 23958</tr>
</table>
Run Code Online (Sandbox Code Playgroud)

当实际的.dart文件<script src="...">与Dartium(v39.0.2171.0 x64)一起使用时,会发生相同的问题。我正在使用Dart v1.11.1。


一些测试:

..appendHtml('''
  <td></td>
  <td>hi</td>
''');
Run Code Online (Sandbox Code Playgroud)

产量:

<table>
  <tr>hi</tr>
</table>
Run Code Online (Sandbox Code Playgroud)

这与上面相同:

..appendHtml('<td>hi</td>');
Run Code Online (Sandbox Code Playgroud)

我可以得到它给我想要的东西的唯一方法是:

..append(new Element.td()..text = someValue)
..append(new Element.td()..text = anotherValue.toString());
Run Code Online (Sandbox Code Playgroud)

daf*_*iel 5

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

为避免这种情况,您可以传递一个不执行任何操作的消毒剂。

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

document.body.appendHtml('<td>fish</td>', treeSanitizer: new MySanitizer());
Run Code Online (Sandbox Code Playgroud)