javascript - Recommendations for Working Around IE9 Treewalker Filter Bug - Stack Overflow

Background InfoA bug exists currently in IE9 where it thinks that the NodeFilter property of the create

Background Info

A bug exists currently in IE9 where it thinks that the NodeFilter property of the createTreeWalker method is a callback function instead of an object containing a callback function.

In a call like this:

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

filter is defined as "an object that contains a method acceptNode," in Webkit and Gecko; however, in IE9, there's no mention of acceptNode at all--it expects a "callback method," without that object wrapping.

Actual Question

So, what's the best way to work around this issue without doing explicit browser detection? In some instances I need filter to be a method, and in others I need it to be an object containing the method. Is there a clean way to acplish this? All of these browsers claim to support DOM 2.0, so I can't test on that...

Documents - Proof of Bug

Here's a parison of the documentation for each:

  1. W3C Spec
  2. Gecko
  3. Webkit
  4. Microsoft ("The NodeFilter is a callback function..." - WRONG)

Background Info

A bug exists currently in IE9 where it thinks that the NodeFilter property of the createTreeWalker method is a callback function instead of an object containing a callback function.

In a call like this:

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

filter is defined as "an object that contains a method acceptNode," in Webkit and Gecko; however, in IE9, there's no mention of acceptNode at all--it expects a "callback method," without that object wrapping.

Actual Question

So, what's the best way to work around this issue without doing explicit browser detection? In some instances I need filter to be a method, and in others I need it to be an object containing the method. Is there a clean way to acplish this? All of these browsers claim to support DOM 2.0, so I can't test on that...

Documents - Proof of Bug

Here's a parison of the documentation for each:

  1. W3C Spec
  2. Gecko
  3. Webkit
  4. Microsoft ("The NodeFilter is a callback function..." - WRONG)
Share Improve this question edited May 12, 2011 at 18:48 Adam Terlson asked May 12, 2011 at 18:29 Adam TerlsonAdam Terlson 12.7k4 gold badges46 silver badges63 bronze badges 3
  • 1 Gecko has accepted a callback function as well as an object that contains an acceptNode method for almost 10 years. See bugzilla.mozilla/show_bug.cgi?id=113008 – Neil Commented May 12, 2011 at 18:55
  • That's no doubt the way to go. The standard, however, states that it's an object that contains a callback. This ment doesn't really work toward solving my issue in any way as at the end of the day, IE9 doesn't support the standard. So I guess... what's your point? – Adam Terlson Commented May 12, 2011 at 19:15
  • This was a ment, not an answer. I just wanted to point out that a callback works in at least one other browser in case it was helpful. – Neil Commented May 12, 2011 at 19:19
Add a ment  | 

2 Answers 2

Reset to default 6

Well, I came up with one thing that works. Open to better alternatives:

var filter = { acceptNode: function() {
     //do filtering...
} };

// Hackzilla.  A true W3C-pliant 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 as nice browsers will call .acceptNode on the filter object, where bad ones will try and execute it immediately.

Alternatives?

Actually IE 9 does follow the spec. Read the ECMAScript bindings section of the DOM spec:

Object NodeFilter

This is an ECMAScript function reference. This method returns a Number. The parameter is a Node object.

Therefore conforming browsers (which includes current versions of all the major ones) will all accept a function as the filter parameter.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745065793a4609247.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信