[Jifty-commit] r1314 - in jifty: trunk/share/web/static/js/cssquery

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Sun Jun 18 15:32:16 EDT 2006


Author: nelhage
Date: Sun Jun 18 15:32:13 2006
New Revision: 1314

Modified:
   jifty/   (props changed)
   jifty/trunk/share/web/static/js/cssquery/cssQuery.js

Log:
 r12712 at phanatique:  nelhage | 2006-06-16 12:57:11 -0400
 Optimize for the case of a #id selector with one root node, e.g. whenever we do an ajax DOM update


Modified: jifty/trunk/share/web/static/js/cssquery/cssQuery.js
==============================================================================
--- jifty/trunk/share/web/static/js/cssquery/cssQuery.js	(original)
+++ jifty/trunk/share/web/static/js/cssquery/cssQuery.js	Sun Jun 18 15:32:13 2006
@@ -16,21 +16,28 @@
 var $COMMA = /\s*,\s*/;
 var cssQuery = function($selector, $$from) {
 try {
+	var $match = [];
+	var $useCache = arguments.callee.caching && !$$from;
+	var $base = ($$from) ? ($$from.constructor == Array) ? $$from : [$$from] : [document];
 	//Optimization -- check for selectors beginning with '#id'
-	if(!$$from) {
+	if($base.length == 1) {
 	    var $$bits = $selector.match($ID_ONLY);
 	    if($$bits) {
-		var $match = document.getElementById($$bits[1]);
-		if(!$match || !$$bits[2].length) {
+		var $match;
+		if($base[0] == document) {
+		    $match = document.getElementById($$bits[1]);
+		} else {
+		    $match = getChildById($base[0], $$bits[1]);
+		}
+		if(!$match) {
+		    return [];
+		} else if(!$$bits[2].length) {
 		    return [$match];
 		} else {
 		    return cssQuery($$bits[2], $match);
 		}
 	    }
 	}
-	var $match = [];
-	var $useCache = arguments.callee.caching && !$$from;
-	var $base = ($$from) ? ($$from.constructor == Array) ? $$from : [$$from] : [document];
 	// process comma separated selectors
 	var $$selectors = parseSelector($selector).split($COMMA), i;
 	for (i = 0; i < $$selectors.length; i++) {
@@ -274,6 +281,22 @@
 	return $results;
 };
 
+function getChildById($from, $id) {
+    if(isMSIE) {
+	return _msie_selectById([], [$from], $id);
+    } else {
+	var $node = document.getElementById($id);
+	var $elt = $node;
+	while($elt) {
+	    if($elt == $from) {
+		return $node;
+	    }
+	    $elt = $elt.parentNode;
+	}
+	return null;
+    }
+}
+
 // for IE5.0
 if (![].push) Array.prototype.push = function() {
 	for (var i = 0; i < arguments.length; i++) {


More information about the Jifty-commit mailing list