获取 html 注释节点

2013-05-03 by Dron

Tracker 需要移除页面的 HTML 注释节点,鉴于注释的复杂性,为了靠谱点,不使用正则了,顺手写了个获取 HTML 注释的工具函数:

var getHtmlCommentNodes = function(){
  var cn, push;

  cn = document.COMMENT_NODE;
  push = [].push;

  return function f( node ){
    var result, c, l, i;

    result = [];

    if( node.nodeType == cn )
      result.push( node );
    else if( c = node.childNodes, l = c.length )
      for( i = 0; i < l; i ++ )
        push.apply( result, f( c[ i ] ) );

    return result;
  }
}();

用法:

getHtmlCommentNodes( document.documentElement );