Skip to content

Instantly share code, notes, and snippets.

@siddMahen
Created December 26, 2011 16:30
Show Gist options
  • Save siddMahen/1521540 to your computer and use it in GitHub Desktop.
Save siddMahen/1521540 to your computer and use it in GitHub Desktop.
Child Filters Impl
// Child filters - TBD
var filters = {
"nth-child": function(ctx, val){
var found = [],
val = val-1;
for(var j = 0; j < ctx.length; j++){
if(ctx[j][val])
found.push(ctx[j][val]);
}
return found;
},
"first-child": function(ctx){
return filters["nth-child"](ctx, 1);
},
"last-child": function(ctx){
var found = [];
for(var j = 0; j < ctx.length; j++){
if(ctx[j][ctx.j.length-1])
found.push(ctx[j][ctx.j.length-1]);
}
return found;
},
"only-child": function(ctx){
var found = [];
for(var j = 0; j < ctx.length; j++){
for(var p = 0; p < ctx[j].length; p++){
var node = ctx[j][p];
if(node.children){
if(node.children.length === 0)
found.push(node.children[0]);
}
}
}
return found;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment