Skip to main content

Bug Tracker

Side navigation

#9750 closed bug (duplicate)

Opened July 05, 2011 09:58PM UTC

Closed July 12, 2011 02:46PM UTC

Last modified July 12, 2011 02:46PM UTC

jQuery.map( arguments, fn ) can fail in Safari

Reported by: spicyj Owned by:
Priority: low Milestone: 1.next
Component: core Version: 1.6.2
Keywords: Cc:
Blocked by: Blocking:
Description

The "isArray" heuristic looks at elems[ 0 ] and elems[ elems.length - 1 ]; if they are both truthy; it assumes that elems is an array. In the case that one of the two elements is falsey, map will treat it as an object instead of an array.

Normally, that's pretty much okay, but in Safari, for ( key in arguments ) seems to yield no values. This page demonstrates the bug:

http://jsfiddle.net/qeSxC/

By testing the two end values for undefined instead of truthiness, everything is fixed. Patch and a test on GitHub:

https://github.com/spicyj/jquery/commit/204844c8d7acd97d20b3add013156b1a2cc58c3c

Attachments (0)
Change History (8)

Changed July 05, 2011 10:15PM UTC by spicyj comment:1

Changed July 05, 2011 10:44PM UTC by rwaldron comment:2

component: unfiledcore
priority: undecidedlow
resolution: → wontfix
status: newclosed

Without arguing the semantics of jQuery.map's first argument, you're using it like "each" which is incorrect - jQuery.map should not be used for side-effects. jQuery.map is expected to return a mutated copy of the original array or object argument. I've reduced the test case further to show the correct usage: http://jsfiddle.net/rwaldron/FU7Fv/

Changed July 05, 2011 10:48PM UTC by spicyj comment:3

My bug stands. Screenshot from your fiddle:

http://uploads.hipchat.com/6574/26709/u3mhjt5gjugeigk/upload.png

(In my original script, I was using jQuery.map properly but changed it to run console.log for that demo.)

Changed July 05, 2011 11:22PM UTC by spicyj comment:4

If you prefer to relying on jQuery.map's each-like functionality, you can use this patch instead which uses map "properly" in the test case:

https://github.com/spicyj/jquery/commit/81e0e8cd42da0dc93d2d69cd8bed6915105d1d0b

Changed July 06, 2011 03:50AM UTC by rwaldron comment:5

resolution: wontfix
status: closedreopened

Reopening for further discussion

Changed July 11, 2011 11:12PM UTC by rkatic comment:6

This would be a duplicate of #9025 (that is wrongly declared as a duplicate) that already has a patch.

Changed July 12, 2011 02:46PM UTC by addyosmani comment:7

resolution: → duplicate
status: reopenedclosed

Changed July 12, 2011 02:46PM UTC by addyosmani comment:8

Duplicate of #9025.