createFragment 方法

DocumentFragment createFragment(
  1. String? html,
  2. {NodeValidator? validator,
  3. NodeTreeSanitizer? treeSanitizer}
)
override

从 HTML 片段创建一个 DocumentFragment 并确保其遵循由 validator 或 treeSanitizer 指定的 sanitization 规则。

如果默认的验证行为过于严格,则应创建一个新的 NodeValidator,无论是扩展还是包装默认验证器并覆盖验证 API。

treeSanitizer 用于遍历生成的节点树并对其进行清理。也可以提供自定义的 treeSanitizer 来执行特殊的验证规则,但由于 API 实现较为复杂,因此不推荐这样做。

返回的树保证只包含由提供的 validator 容许的节点和属性。

另请参阅

实现

DocumentFragment createFragment(String? html,
    {NodeValidator? validator, NodeTreeSanitizer? treeSanitizer}) {
  if (Range.supportsCreateContextualFragment) {
    return super.createFragment(html,
        validator: validator, treeSanitizer: treeSanitizer);
  }
  // IE9 workaround which does not support innerHTML on Table elements.
  var fragment = new DocumentFragment();
  var section = new TableElement()
      .createFragment(html,
          validator: validator, treeSanitizer: treeSanitizer)
      .nodes
      .single;
  var row = section.nodes.single;
  fragment.nodes.addAll(row.nodes);
  return fragment;
}