createFragment 方法

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

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

如果默认验证行为过于严格,则应创建一个新的 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;
  fragment.nodes.addAll(section.nodes);
  return fragment;
}