-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Description
Further discussion here: http://stackoverflow.com/questions/34580013/jquery-closest-default-context
The documentation for closest states:
If no context is passed in then the context of the jQuery set will be used instead.
However, it seems that this is often not the case. You can see this illustrated here:
https://jsfiddle.net/pjbg4398/
When the context is passed explicitly, closest works as expected and only finds an element if it is within the context element. However, if the jquery set has a context and closest is called without explicitly passing a context, no context is used (contradicting the documentation). However, if the selector passed to closest contains non-css selectors (gt, eq, etc.), then the set's context is used.
I believe the fix should be to change the loop condition as follows:
for (cur = this[i]; cur && cur !== context; cur = cur.parentNode) {
should become:
for (cur = this[i]; cur && cur !== (context || this.context); cur = cur.parentNode) {
which would be in line with the non-css code which does a similar thing with context:
pos = rneedsContext.test(selectors) || typeof selectors !== "string" ? jQuery(selectors, context || this.context) : 0;