[KSS-checkins] r44770 - kukit/kukit.js/trunk/kukit
jvloothuis at codespeak.net
jvloothuis at codespeak.net
Fri Jul 6 12:57:36 CEST 2007
Author: jvloothuis
Date: Fri Jul 6 12:57:36 2007
New Revision: 44770
Modified:
kukit/kukit.js/trunk/kukit/selectorreg.js
Log:
Added an extra option to the parentnode selector so that it now
accepts selectors in the form of selector1 % selector2. This can be
used to select surrounding nodes.
Modified: kukit/kukit.js/trunk/kukit/selectorreg.js
==============================================================================
--- kukit/kukit.js/trunk/kukit/selectorreg.js (original)
+++ kukit/kukit.js/trunk/kukit/selectorreg.js Fri Jul 6 12:57:36 2007
@@ -149,28 +149,64 @@
return nodes;
});
-// Return a list of all nodes that match the css expression in the parent chain
+// Return a list of all nodes that match the css expression in the
+// parent chain This selector can also return surrounding nodes by
+// passing in an expression with a "%" in it.
kukit.selectorTypesGlobalRegistry.register('parentnode', function(expr, node) {
- var selectednodes = kukit.dom.cssQuery(expr);
- var parentnodes = [];
- var parentnode = node.parentNode;
- while(parentnode.parentNode) {
- parentnodes.push(parentnode);
- parentnode = parentnode.parentNode;
+ var expressions = expr.split('%');
+
+ ;;; if (expressions.length > 2) {
+ ;;; throw 'Only one "%" allowed in parentnode selector: "' + expr + '"';
+ ;;; }
+
+ function parentnodes(node){
+ var nodes = [];
+ var parentnode = node;
+ while(parentnode.parentNode) {
+ nodes.push(parentnode);
+ parentnode = parentnode.parentNode;
+ }
+ return nodes;
+ }
+
+ function filternode(allowed, node){
+ for(var i=0; i<allowed.length; i++){
+ if(allowed[i] == node){
+ return node;
+ }
+ }
+ }
+
+ function filternodes(allowed, nodes){
+ var results = [];
+ for(var i=0; i<nodes.length; i++){
+ var node = filternode(allowed, nodes[i]);
+ if(node != null){
+ results.push(node);
+ }
+ }
+ return results;
}
+ var selectednodes = kukit.dom.cssQuery(expressions[0]);
+ var parentchain = parentnodes(node.parentNode);
+
// Filter the nodes so that only the ones in the parent chain remain
+ parentchain = filternodes(parentchain, selectednodes);
+
+ // If there is only a single expression just return the results
+ if(expressions.length == 1){
+ return parentchain;
+ }
+
+ // When searching for surrounding nodes use the second expression
+ var surroundingnodes = kukit.dom.cssQuery(expressions[1]);
var results = [];
- for(var i=0; i<selectednodes.length; i++){
- var inchain = false;
- for(var j=0; j<parentnodes.length; j++){
- if(selectednodes[i] === parentnodes[j]){
- inchain = true;
- }
- }
- if(inchain){
- results.push(selectednodes[i]);
- }
+ for(var i=0; i<surroundingnodes.length; i++){
+ var node = surroundingnodes[i];
+ if(filternodes(parentchain, parentnodes(node)) != null){
+ results.push(node);
+ }
}
return results;
});
More information about the Kukit-checkins
mailing list