DOM TreeWalker - Tales of a Developer Advoca

系统 1728 0

DOM TreeWalker - Tales of a Developer Advocate

DOM TreeWalker

Aug 8 th , 2010

I really wanted to get a reference to Walker Texas Ranger in to the title, but I really couldn’t think of anything that cool. If you can think of a great Chuck Norris reference leave a comment, I am all chins!

It always amazes me that there is so much to HTML that is still not being exploited by developers.

One pattern I see regularly is recursive descent through the DOM to find particular TEXT nodes that contain a given string so that the container element can be manipulated.

It is not that recursion is slow, if your DOM is complex enough you could hit stack overflow errors (although it is pretty unlikely), it is that there are a lot of edge cases when parsing the DOM that you need to code in.

A little known DOM function is available that makes developing applications that need to scan the DOM easy. It is called Tree Walker, created through the createTreeWalker function on the document.

You can create a tree walker very quickly using the following Javascript:

            document.createTreeWalker(document.body, NODE_FILTER.SHOW_TEXT, 
            
              function
            
            (node) { 
            
              return
            
             NodeFilter.FILTER_ACCEPT; }, 
            
              false
            
            );


            
              while
            
            (treeWalker.nextNode()) console.log(treeWalker.currentNode);
          

The above code is given a root node of document.body, a filter of what to show (only Text Nodes in our case), and a function that returns if the node should be returned (essentially a filter).

An interesting point to note is that the Filter function is only called when iterating over the treeWalker.

This is actually a really cool feature, the currentNode property of the Tree Walker contains DOM objects, so you can start to do some really advanced processing, you could highlight the current node, replace its text or remove it – really anything you want. This is significantly simpler than managing the recursion yourself.

As a more concrete example, lets use this to find all twitter user names on a page and then automatically make these a twitter link. It could be done using recursion pretty simply, but I need something fun to show you.

            
              var
            
             re = 
            
              new
            
             RegExp(); 
            
              // This isn't accurate RE
            
            
re.compile(
            
              
                "
              
              
                @([A-Za-z0-9_]*)
              
              
                "
              
            
            );

            
              var
            
             walker = document.createTreeWalker(
  document.body,
  NodeFilter.SHOW_TEXT,
  
            
              function
            
            (node) {
    
            
              var
            
             matches = node.textContent.match(re);

    
            
              if
            
            (matches) { 
      
            
              return
            
             NodeFilter.FILTER_ACCEPT;
    } 
            
              else
            
             {
      
            
              return
            
             NodeFilter.FILTER_SKIP;
    }
  },
  
            
              false
            
            );


            
              var
            
             nodes = [];


            
              while
            
            (walker.nextNode()) {
  nodes.push(walker.currentNode);
}


            
              for
            
            (
            
              var
            
             i = 
            
              0
            
            ; node=nodes[i] ; i++) {
  node.parentNode.innerHTML = node.parentNode.innerHTML.replace(re, 
            
              
                "
              
              
                @
                
                  $1
                
              
              
                "
              
            
            ) }
          

A live example is on my sample site

The theory is, that User-Agents can optimize the access to the DOM better than you can recursively descend through the DOM. So, where would I use this? The first thing that springs to mind is that it is ideal for Chrome extensions. Many Chrome extensions traverse the DOM looking for pieces of text, or particular patterns inside nodes that aren’t available via CSS Selectors.

More information can be found on Mozilla’s Developer site

Aug 8 th , 2010

DOM TreeWalker - Tales of a Developer Advocate


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论