IE9 Guidelines Treewalker Filter Error

Background Information

There is an error in IE9 in which it considers that the NodeFilter property of the NodeFilter method is a callback function instead of an object containing a callback function.

In such a call:

document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT, filter, false);

filter defined as "an object containing the acceptNode method" in Webkit and Gecko; however, in IE9 there is no mention of acceptNode at all - it expects a "callback method" without binding the object.

Actual question

So what is the best way to get around this problem without explicitly detecting the browser? In some cases, I need filter be a method, and in others, I need an object containing this method. Is there a clean way to do this? All of these browsers claim to support DOM 2.0, so I cannot verify this ...

Documents - Proof of Error

Here is a comparison of the documentation for each:

+4
source share
2 answers

Ok, I came up with one that works. Open to the best alternatives:

 var filter = { acceptNode: function() { //do filtering... } }; // Hackzilla. A true W3C-compliant nodeFilter object isn't passed, and instead a "safe" one _based_ off of the real one. var safeFilter = filter.acceptNode; safeFilter.acceptNode = filter.acceptNode; document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT, safeFilter, false); 

This works because good browsers will call .acceptNode in the filter object, where bad ones will try to execute it immediately.

Alternatives?

+6
source

In fact, IE 9 performs following the specifications. Read the ECMAScript binding section in the DOM specification :

NodeFilter Object

This is a link to the ECMAScript function. This method returns Number. The parameter is a Node object.

Therefore, the corresponding browsers (including the current versions of all the major ones) will accept the function as a filter parameter.

+5
source

All Articles