Skip to content
Browse files

* Updating Slick to the official 1.0 version

  • Loading branch information...
1 parent 4fd71d8 commit 062e3e604ced52039a566a75d7fbdefdd6de50ef @cpojer cpojer committed Jan 11, 2011
Showing with 59 additions and 40 deletions.
  1. +58 −38 Source/Slick/Slick.Finder.js
  2. +1 −2 Source/Slick/Slick.Parser.js
View
96 Source/Slick/Slick.Finder.js
@@ -18,15 +18,15 @@ local.isNativeCode = function(fn){
};
local.isXML = function(document){
- return (!!document.xmlVersion) || (!!document.xml) || (Object.prototype.toString.call(document) === '[object XMLDocument]') ||
- (document.nodeType === 9 && document.documentElement.nodeName !== 'HTML');
+ return (!!document.xmlVersion) || (!!document.xml) || (Object.prototype.toString.call(document) == '[object XMLDocument]') ||
+ (document.nodeType == 9 && document.documentElement.nodeName != 'HTML');
};
local.setDocument = function(document){
// convert elements / window arguments to document. if document cannot be extrapolated, the function returns.
- if (document.nodeType === 9); // document
+ if (document.nodeType == 9); // document
else if (document.ownerDocument) document = document.ownerDocument; // node
else if (document.navigator) document = document.document; // window
else return;
@@ -64,9 +64,9 @@ local.setDocument = function(document){
} catch(e){};
if (this.isHTMLDocument){
-
+
testNode.style.display = 'none';
-
+
// IE returns comment nodes for getElementsByTagName('*') for some documents
testNode.appendChild(document.createComment(''));
starSelectsComments = (testNode.getElementsByTagName('*').length > 0);
@@ -114,19 +114,19 @@ local.setDocument = function(document){
} catch(e){};
this.brokenGEBCN = cachedGetElementsByClassName || brokenSecondClassNameGEBCN;
-
+
// Webkit dont return selected options on querySelectorAll
try {
testNode.innerHTML = '<select><option selected="selected">a</option></select>';
this.brokenCheckedQSA = (testNode.querySelectorAll(':checked').length == 0);
} catch(e){};
-
+
// IE returns incorrect results for attr[*^$]="" selectors on querySelectorAll
try {
testNode.innerHTML = '<a class=""></a>';
this.brokenEmptyAttributeQSA = (testNode.querySelectorAll('[class*=""]').length != 0);
} catch(e){};
-
+
}
root.removeChild(testNode);
@@ -396,13 +396,19 @@ local.matchNode = function(node, selector){
if (!parsed) return true;
// simple (single) selectors
- if(parsed.length == 1 && parsed.expressions[0].length == 1){
- var exp = parsed.expressions[0][0];
- return this.matchSelector(node, (this.isXMLDocument) ? exp.tag : exp.tag.toUpperCase(), exp.id, exp.classes, exp.attributes, exp.pseudos);
+ var expressions = parsed.expressions, reversedExpressions, simpleExpCounter = 0, i;
+ for (i = 0; (currentExpression = expressions[i]); i++){
+ if (currentExpression.length == 1){
+ var exp = currentExpression[0];
+ if (this.matchSelector(node, (this.isXMLDocument) ? exp.tag : exp.tag.toUpperCase(), exp.id, exp.classes, exp.attributes, exp.pseudos)) return true;
+ simpleExpCounter++;
+ }
}
- var nodes = this.search(this.document, parsed);
- for (var i = 0, item; item = nodes[i++];){
+ if (simpleExpCounter == parsed.length) return false;
+
+ var nodes = this.search(this.document, parsed), item;
+ for (i = 0; item = nodes[i++];){
if (item === node) return true;
}
return false;
@@ -460,7 +466,7 @@ var combinators = {
for (i = 0; item = children[i++];) if (item.getAttributeNode('id').nodeValue == id){
this.push(item, tag, null, classes, attributes, pseudos);
break;
- }
+ }
return;
}
if (!item){
@@ -488,12 +494,12 @@ var combinators = {
'>': function(node, tag, id, classes, attributes, pseudos){ // direct children
if ((node = node.firstChild)) do {
- if (node.nodeType === 1) this.push(node, tag, id, classes, attributes, pseudos);
+ if (node.nodeType == 1) this.push(node, tag, id, classes, attributes, pseudos);
} while ((node = node.nextSibling));
},
'+': function(node, tag, id, classes, attributes, pseudos){ // next sibling
- while ((node = node.nextSibling)) if (node.nodeType === 1){
+ while ((node = node.nextSibling)) if (node.nodeType == 1){
this.push(node, tag, id, classes, attributes, pseudos);
break;
}
@@ -502,14 +508,14 @@ var combinators = {
'^': function(node, tag, id, classes, attributes, pseudos){ // first child
node = node.firstChild;
if (node){
- if (node.nodeType === 1) this.push(node, tag, id, classes, attributes, pseudos);
+ if (node.nodeType == 1) this.push(node, tag, id, classes, attributes, pseudos);
else this['combinator:+'](node, tag, id, classes, attributes, pseudos);
}
},
'~': function(node, tag, id, classes, attributes, pseudos){ // next siblings
while ((node = node.nextSibling)){
- if (node.nodeType !== 1) continue;
+ if (node.nodeType != 1) continue;
var uid = this.getUID(node);
if (this.bitUniques[uid]) break;
this.bitUniques[uid] = true;
@@ -537,7 +543,7 @@ var combinators = {
},
'!+': function(node, tag, id, classes, attributes, pseudos){ // previous sibling
- while ((node = node.previousSibling)) if (node.nodeType === 1){
+ while ((node = node.previousSibling)) if (node.nodeType == 1){
this.push(node, tag, id, classes, attributes, pseudos);
break;
}
@@ -546,14 +552,14 @@ var combinators = {
'!^': function(node, tag, id, classes, attributes, pseudos){ // last child
node = node.lastChild;
if (node){
- if (node.nodeType === 1) this.push(node, tag, id, classes, attributes, pseudos);
+ if (node.nodeType == 1) this.push(node, tag, id, classes, attributes, pseudos);
else this['combinator:!+'](node, tag, id, classes, attributes, pseudos);
}
},
'!~': function(node, tag, id, classes, attributes, pseudos){ // previous siblings
while ((node = node.previousSibling)){
- if (node.nodeType !== 1) continue;
+ if (node.nodeType != 1) continue;
var uid = this.getUID(node);
if (this.bitUniques[uid]) break;
this.bitUniques[uid] = true;
@@ -583,20 +589,20 @@ var pseudos = {
},
'first-child': function(node){
- while ((node = node.previousSibling)) if (node.nodeType === 1) return false;
+ while ((node = node.previousSibling)) if (node.nodeType == 1) return false;
return true;
},
'last-child': function(node){
- while ((node = node.nextSibling)) if (node.nodeType === 1) return false;
+ while ((node = node.nextSibling)) if (node.nodeType == 1) return false;
return true;
},
'only-child': function(node){
var prev = node;
- while ((prev = prev.previousSibling)) if (prev.nodeType === 1) return false;
+ while ((prev = prev.previousSibling)) if (prev.nodeType == 1) return false;
var next = node;
- while ((next = next.nextSibling)) if (next.nodeType === 1) return false;
+ while ((next = next.nextSibling)) if (next.nodeType == 1) return false;
return true;
},
@@ -628,21 +634,21 @@ var pseudos = {
'first-of-type': function(node){
var nodeName = node.nodeName;
- while ((node = node.previousSibling)) if (node.nodeName === nodeName) return false;
+ while ((node = node.previousSibling)) if (node.nodeName == nodeName) return false;
return true;
},
'last-of-type': function(node){
var nodeName = node.nodeName;
- while ((node = node.nextSibling)) if (node.nodeName === nodeName) return false;
+ while ((node = node.nextSibling)) if (node.nodeName == nodeName) return false;
return true;
},
'only-of-type': function(node){
var prev = node, nodeName = node.nodeName;
- while ((prev = prev.previousSibling)) if (prev.nodeName === nodeName) return false;
+ while ((prev = prev.previousSibling)) if (prev.nodeName == nodeName) return false;
var next = node;
- while ((next = next.nextSibling)) if (next.nodeName === nodeName) return false;
+ while ((next = next.nextSibling)) if (next.nodeName == nodeName) return false;
return true;
},
@@ -651,11 +657,11 @@ var pseudos = {
// custom pseudos
'enabled': function(node){
- return (node.disabled === false);
+ return !node.disabled;
},
'disabled': function(node){
- return (node.disabled === true);
+ return node.disabled;
},
'checked': function(node){
@@ -669,7 +675,7 @@ var pseudos = {
'root': function(node){
return (node === this.root);
},
-
+
'selected': function(node){
return node.selected;
}
@@ -722,23 +728,37 @@ local.override = function(regexp, method){
/*<query-selector-override>*/
-var reEmptyAttribute = /\[.*[*$^]=(?:["']{2})?\]/;
+var reEmptyAttribute = /\[.+[*$^]=(?:""|'')?\]/, qsaFailExpCache = {};
local.override(/./, function(expression, found, first){ //querySelectorAll override
- if (!this.querySelectorAll || this.nodeType != 9 || !local.isHTMLDocument || local.brokenMixedCaseQSA ||
+ if (!this.querySelectorAll || !local.isHTMLDocument || local.brokenMixedCaseQSA || qsaFailExpCache[expression] ||
(local.brokenCheckedQSA && expression.indexOf(':checked') > -1) ||
(local.brokenEmptyAttributeQSA && reEmptyAttribute.test(expression)) || Slick.disableQSA) return false;
- var nodes, node;
+ var nodes, isDocument = (this.nodeType == 9), cacheKey = expression;
+ if (!isDocument){
+ // non-document rooted QSA
+ // credits to Andrew Dupont
+ var currentId = this.getAttribute('id'), id = 'slick:id';
+ this.setAttribute('id', id);
+ expression = '#' + id + ' ' + expression;
+ }
+
try {
if (first) return this.querySelector(expression) || null;
else nodes = this.querySelectorAll(expression);
- } catch(error){
+ } catch(e) {
+ qsaFailExpCache[cacheKey] = 1;
return false;
+ } finally {
+ if (!isDocument){
+ if (currentId) this.setAttribute('id', currentId);
+ else this.removeAttribute('id');
+ }
}
- var i, hasOthers = !!(found.length);
+ var i, node, hasOthers = !!(found.length);
if (local.starSelectsClosedQSA) for (i = 0; node = nodes[i++];){
if (node.nodeName > '@' && (!hasOthers || !local.uniques[local.getUIDHTML(node)])) found.push(node);
@@ -829,7 +849,7 @@ if (typeof document != 'undefined') local.setDocument(document);
var Slick = local.Slick = (this.Slick || {});
-Slick.version = '0.9dev';
+Slick.version = '1.0';
// Slick finder
View
3 Source/Slick/Slick.Parser.js
@@ -27,10 +27,9 @@ var parse = function(expression, isReversed){
return parse(this.raw, true);
}};
separatorIndex = -1;
- var cacheKey = expression;
while (expression != (expression = expression.replace(regexp, parser)));
parsed.length = parsed.expressions.length;
- return currentCache[cacheKey] = (reversed) ? reverse(parsed) : parsed;
+ return currentCache[parsed.raw] = (reversed) ? reverse(parsed) : parsed;
};
var reverseCombinator = function(combinator){

0 comments on commit 062e3e6

Please sign in to comment.
Something went wrong with that request. Please try again.