Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Mar 1, 2012
1 parent 7c800e8 commit a7a28c5
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions lib/zest.js
Expand Up @@ -18,8 +18,8 @@
var window = this var window = this
, document = this.document , document = this.document
, old = this.zest , old = this.zest
, subject , context = document
, context; , subject;


/** /**
* Helpers * Helpers
Expand Down Expand Up @@ -393,15 +393,15 @@ var selectors = {
':hover': function(el) { ':hover': function(el) {
throw new Error(':hover is not supported.'); throw new Error(':hover is not supported.');
}, },
':active': function(el) {
throw new Error(':active is not supported.');
},
':link': function(el) { ':link': function(el) {
throw new Error(':link is not supported.'); throw new Error(':link is not supported.');
}, },
':visited': function(el) { ':visited': function(el) {
throw new Error(':visited is not supported.'); throw new Error(':visited is not supported.');
}, },
':active': function(el) {
throw new Error(':active is not supported.');
},
':column': function() { ':column': function() {
throw new Error(':column is not supported.'); throw new Error(':column is not supported.');
}, },
Expand Down Expand Up @@ -646,7 +646,6 @@ var compile = function(sel) {
} }


filter.push(comb(makeSimple(buff))); filter.push(comb(makeSimple(buff)));

buff = []; buff = [];
} }


Expand Down Expand Up @@ -701,7 +700,7 @@ var compileGroup = function(sel) {
* Selection * Selection
*/ */


var select = function(sel) { var find = function(sel) {
var results = [] var results = []
, test = compile(sel) , test = compile(sel)
, scope = context.getElementsByTagName(test.qname) , scope = context.getElementsByTagName(test.qname)
Expand All @@ -711,7 +710,7 @@ var select = function(sel) {
, l , l
, last; , last;


if (subject) { if (subject === true) {
while (el = scope[i++]) { while (el = scope[i++]) {
if (test(el) && subject !== last) { if (test(el) && subject !== last) {
results.push(subject); results.push(subject);
Expand All @@ -727,7 +726,7 @@ var select = function(sel) {
subject = null; subject = null;


if (test.sel) { if (test.sel) {
res = select(test.sel); res = find(test.sel);
l = res.length; l = res.length;
i = 0; i = 0;


Expand All @@ -745,13 +744,7 @@ var select = function(sel) {
* Compatibility * Compatibility
*/ */


select = (function() { var select = (function() {
var _select = select;

if (window.ZEST_DEBUG) {
return _select;
}

var slice = (function() { var slice = (function() {
try { try {
Array.prototype.slice.call(document.getElementsByTagName('*')); Array.prototype.slice.call(document.getElementsByTagName('*'));
Expand All @@ -771,7 +764,7 @@ select = (function() {
try { try {
return slice.call(context.querySelectorAll(sel)); return slice.call(context.querySelectorAll(sel));
} catch(e) { } catch(e) {
return _select(sel); return find(sel);
} }
}; };
} }
Expand All @@ -791,7 +784,7 @@ select = (function() {
} catch(e) { } catch(e) {
; ;
} }
return _select(sel); return find(sel);
}; };
})(); })();


Expand All @@ -809,8 +802,8 @@ if (function() {
* Zest * Zest
*/ */


var zest = function(sel, context_) { var zest = function(sel, con) {
context = context_ || document; context = con || document;


try { try {
sel = select(sel); sel = select(sel);
Expand All @@ -821,7 +814,7 @@ var zest = function(sel, context_) {
sel = []; sel = [];
} }


context = null; context = document;


return sel; return sel;
}; };
Expand All @@ -840,7 +833,7 @@ zest.matches = function(el, sel) {
}; };


zest.cache = function() { zest.cache = function() {
if (zest._cache) return; if (compile.raw) return;


var raw = compile var raw = compile
, cache = {}; , cache = {};
Expand All @@ -854,14 +847,26 @@ zest.cache = function() {
zest._cache = cache; zest._cache = cache;
}; };


zest.noCache = function() {
if (!compile.raw) return;
compile = compile.raw;
delete zest._cache;
};

zest.noConflict = function() { zest.noConflict = function() {
window.zest = old; window.zest = old;
return zest; return zest;
}; };


zest.noNative = function() {
select = find;
};

window.zest = zest; window.zest = zest;


if (!window.ZEST_DEBUG) { if (window.ZEST_DEBUG) {
zest.noNative();
} else {
zest.cache(); zest.cache();
} }


Expand Down

0 comments on commit a7a28c5

Please sign in to comment.