如何实现NodeList? [英] How is the NodeList implemented?

查看:64
本文介绍了如何实现NodeList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DOM NodeList(例如由element.getElementsByTagName返回)是一个有趣的对象,因为它不是快照,但是反映了在创建NodeList之后对文档所做的更改。



我想知道如何实现这样的集合:完全懒惰的评估必须非常缓慢,但是保持缓存版本的一致性需要内部进行大量记账。 / p>

我尝试通过Google搜索有关该主题的博客文章,还尝试找到Mozilla的相关源代码文件,但无法立即找到任何内容(当我找不到时)



那么Firefox,Safari,Internet Explorer(以及其他非浏览器DOM实现)如何处理NodeList?

解决方案

对于.NET的XML库,XmlNodeList有3个内部子类,它们具有不同的策略。对于XmlNode.ChildNodes属性,XmlChildNodes集合使用基于对其包含元素的引用的简单惰性评估。 XmlElementList在DOM更改时使用事件侦听器。第三个是XPathNodelList,用于XPath查询(例如XmlNode.SelectNodes()),并在每次访问其索引,读取其Count属性或对其进行迭代时评估XPath。


The DOM NodeList (as returned e.g. by element.getElementsByTagName) is an interesting object, in that it is not a snapshot, but reflects changes to the document made after you have created the NodeList.

I was wondering how one would implement such a collection: Completely lazy evaluation must be horribly slow, but keeping a cached version consistent requires a lot of book-keeping internally.

I tried to google for blog articles on the subject, and also tried to find the relevant source code files for Mozilla, but could not find anything immediately (and when I cannot find something immediately, I come here...).

So how do Firefox, Safari, Internet Explorer (and other non-browser DOM implementations) handle NodeLists?

解决方案

For .NET's XML library, there are 3 internal subclasses of XmlNodeList with different strategies. The XmlChildNodes collection, for the XmlNode.ChildNodes property, uses simple lazy evaluation based on a reference to its containing element. The XmlElementList uses event listeners for when the DOM changes. The third, XPathNodelList, is for XPath queries (e.g., XmlNode.SelectNodes()) and evaluates the XPath each time its index is accessed, its Count property is read, or it is iterated.

这篇关于如何实现NodeList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆