博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DOM TreeWalker - Tales of a Developer Advocate
阅读量:6447 次
发布时间:2019-06-23

本文共 3014 字,大约阅读时间需要 10 分钟。

DOM TreeWalker

Aug 8th, 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 REre.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, "@") }

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

Aug 8th, 2010

转载地址:http://noowo.baihongyu.com/

你可能感兴趣的文章
Nginx应用笔记(二)Nginx配置文件说明
查看>>
在Linux(Ubuntu)+Nginx安装配置AjaXplorer
查看>>
(八十九)txt文档的输入和输出
查看>>
openssl vulnerability affect PostgreSQLs
查看>>
使用ant编译发布web项目
查看>>
[经典面试题][百度]在由N个正整数的集合S中,找出最大元素C,满足C=A + B
查看>>
【SICP练习】152 练习4.8
查看>>
PostgreSQL 百亿数据 秒级响应 正则及模糊查询
查看>>
【JSP开发】获取web应用的初始化参数
查看>>
iOS开发网络篇—HTTP协议
查看>>
Zabbix 监控 Nginx
查看>>
【C++注意事项】4 指针 Pointers
查看>>
js 深拷贝,浅拷贝
查看>>
LeetCode刷题: 整数反转
查看>>
#学习笔记# 记录一次java父类转子类的方法
查看>>
Vue源码分析系列四:Virtual DOM
查看>>
Git 版本回退
查看>>
Python:使用pypdf2合并、分割、加密pdf文件。
查看>>
rabbitmq java 应用实例
查看>>
Flutter Mac下环境配置
查看>>