Skip to content

Commit

Permalink
ensure Array's native filter() is always used
Browse files Browse the repository at this point in the history
Fix conflict with Prototype framework.  Thanks, John, Ted, and Ryan.

Closes madrobby#549
  • Loading branch information
vmarta authored and mislav committed Sep 28, 2012
1 parent 18ba1f0 commit 86fb735
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/zepto.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Zepto.js may be freely distributed under the MIT license.

var Zepto = (function() {
var undefined, key, $, classList, emptyArray = [], slice = emptyArray.slice,
var undefined, key, $, classList, emptyArray = [], slice = emptyArray.slice, filter = emptyArray.filter,
document = window.document,
elementDisplay = {}, classCache = {},
getComputedStyle = document.defaultView.getComputedStyle,
Expand Down Expand Up @@ -53,7 +53,7 @@ var Zepto = (function() {
function isArray(value) { return value instanceof Array }
function likeArray(obj) { return typeof obj.length == 'number' }

function compact(array) { return array.filter(function(item){ return item !== undefined && item !== null }) }
function compact(array) { return filter.call(array, function(item){ return item !== undefined && item !== null }) }
function flatten(array) { return array.length > 0 ? $.fn.concat.apply([], array) : array }
camelize = function(str){ return str.replace(/-+(.)?/g, function(match, chr){ return chr ? chr.toUpperCase() : '' }) }
function dasherize(str) {
Expand All @@ -63,7 +63,7 @@ var Zepto = (function() {
.replace(/_/g, '-')
.toLowerCase()
}
uniq = function(array){ return array.filter(function(item, idx){ return array.indexOf(item) == idx }) }
uniq = function(array){ return filter.call(array, function(item, idx){ return array.indexOf(item) == idx }) }

function classRE(name) {
return name in classCache ?
Expand Down Expand Up @@ -319,7 +319,7 @@ var Zepto = (function() {
},
filter: function(selector){
if (isFunction(selector)) return this.not(this.not(selector))
return $([].filter.call(this, function(element){
return $(filter.call(this, function(element){
return zepto.matches(element, selector)
}))
},
Expand Down Expand Up @@ -389,7 +389,7 @@ var Zepto = (function() {
},
siblings: function(selector){
return filtered(this.map(function(i, el){
return children(el.parentNode).filter(function(child){ return child!==el })
return filter.call(children(el.parentNode), function(child){ return child!==el })
}), selector)
},
empty: function(){
Expand Down
11 changes: 11 additions & 0 deletions test/zepto.html
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,17 @@ <h1>Zepto DOM unit tests</h1>
t.assertEqualCollection([0,2], indexes)
},

testFilterWithNonNativeArrayFilter: function(t){
var nativeFilter = Array.prototype.filter
try {
// apply broken filter
Array.prototype.filter = function(){ return [] }
t.assertLength(2, $('div').filter('.filtertest'))
} finally {
Array.prototype.filter = nativeFilter
}
},

testAdd: function(t){
var lis=$("li"),spans=$("span"),
together=lis.add("span"),
Expand Down

0 comments on commit 86fb735

Please sign in to comment.