Skip to content

Commit

Permalink
fixed $ to always return the same instance when coming from .search
Browse files Browse the repository at this point in the history
- when a new elements collection is passed to $, it is assumed that is a clean collection, and only the instance and empty checks will be made.
- removed sorting from $. Sorting is only forced for a multi context search.
  • Loading branch information
kamicane committed Dec 11, 2012
1 parent e3f1dde commit 5e5b4c0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
51 changes: 28 additions & 23 deletions lib/elements.js
Expand Up @@ -19,40 +19,45 @@ var instances = {}
var $ = prime({constructor: function $(n, context){

if (n == null) return (this && this.constructor === $) ? new elements : null
if (n.constructor === elements) return n

var self = new elements, uid
var self = n

if (typeof n === "string"){
if (!self.search) return null
self[self.length++] = context || document
return self.search(n)
}
if (n.constructor !== elements){

self = new elements

if (n.nodeType || n === global){
var uid

if (typeof n === "string"){
if (!self.search) return null
self[self.length++] = context || document
return self.search(n)
}

self[self.length++] = n
if (n.nodeType || n === global){

} else if (n.length){
self[self.length++] = n

// this could be an array, or any object with a length attribute,
// including another instance of elements from another interface.
} else if (n.length){

var uniques = {}
// this could be an array, or any object with a length attribute,
// including another instance of elements from another interface.

for (var i = 0, l = n.length; i < l; i++){ // perform elements flattening
var nodes = $(n[i], context)
if (nodes && nodes.length) for (var j = 0, k = nodes.length; j < k; j++){
var node = nodes[j]
uid = uniqueID(node)
if (!uniques[uid]){
self[self.length++] = node
uniques[uid] = true
var uniques = {}

for (var i = 0, l = n.length; i < l; i++){ // perform elements flattening
var nodes = $(n[i], context)
if (nodes && nodes.length) for (var j = 0, k = nodes.length; j < k; j++){
var node = nodes[j]
uid = uniqueID(node)
if (!uniques[uid]){
self[self.length++] = node
uniques[uid] = true
}
}
}
}

if (self.sort && self.length > 1) self.sort()
}

}

Expand Down
7 changes: 2 additions & 5 deletions lib/traversal.js
Expand Up @@ -23,14 +23,11 @@ var walk = function(combinator, method){
$.implement({

search: function(expression){
if (this.length === 1){
var nodes = slick.search(expression, this[0], new $)
return nodes.length ? nodes : null
}
if (this.length === 1) return $(slick.search(expression, this[0], new $))

var buffer = []
for (var i = 0, node; node = this[i]; i++) buffer.push.apply(buffer, slick.search(expression, node))
return $(buffer)
return $(buffer).sort()
},

find: function(expression){
Expand Down

0 comments on commit 5e5b4c0

Please sign in to comment.