Bug with .focus and text nodes in 3.4


There’s a new breaking change in 3.4 when you attempt to call .focus(fn) on a selector containing a text node.

Consider the following:

$('<span>foo</span>hi<span>bar</span>').focus(function() {})

in v3.3.1 this is all fine, but in v3.4.0 and v3.4.1 you get
Uncaught RangeError: Maximum call stack size exceeded

Of note, no one would do this on purpose, as it doesn’t make sense, and even this example is a bit contrived (actual case I encountered at least had form elements), but given various libraries out of my control, it managed to happen for me.

This is the case in question: gitana/alpaca#705

This is clearly outside the boundaries of normal usage, however, is it possible for the .focus(fn) could have some checks to not fail quite so critically?

According to the jQuery function docs it sounds like text nodes should generally be tolerated?

When passing HTML to jQuery(), note that text nodes are not treated as DOM elements. With the exception of a few methods (such as .content()), they are generally ignored or removed. E.g:

Link to test case

In this jsfiddle with v3.4.1, it produces an error

but this jsfiddle with v3.3.1 it doesn’t

Author: Fantashit

1 thought on “Bug with .focus and text nodes in 3.4

Comments are closed.