Skip to content

Instantly share code, notes, and snippets.

@arestov
Created January 28, 2012 08:31
Show Gist options
  • Save arestov/1693509 to your computer and use it in GitHub Desktop.
Save arestov/1693509 to your computer and use it in GitHub Desktop.
var getAllParents = function(el, include_itself, excl){
var ps = [];
if (include_itself){
ps.push({
node: el,
exception: include_itself.exception
});
}
while (el && ((el = el.parentNode) && ((!excl && el != el.ownerDocument)|| (el != excl )))) {
ps.push({
node: el
});
}
return ps;
};
(function(){
var checkParent = function(parents, filter){
if (parents.length){
var p =parents.shift();
var el = p.node;
if (p.exception){
if (p.exception.classes){
for (var i=0; i < p.exception.classes.length; i++) {
filter = filter.replace('.' + p.exception.classes[i], '');
}
}
}
if (!filter || $(el).is(filter)){
return true;
} else{
//console.log(filter);
}
}
};
var checkParents = function(parents, filter){
while ( parents.length ) {
if (checkParent(parents, filter)){
return true;
}
}
console.log('vava');
};
var checkRelative = function(rule, filter, parents){
if (!rule){
return !!checkParents(parents, filter);
} else if (rule == '>'){
return !!checkParent(parents, filter);
}
};
checkRelativesBySteps = function(el, steps, parents){
var rule,
p = parents || getAllParents(el, false, el.ownerDocument);
for (var i = steps.length - 1; i >= 0; i--){
if (typeof rule != 'undefined'){
if (!checkRelative(rule, steps[i].t, p)){
console.log('failed');
console.log(rule);
console.log(steps[i].t);
console.log(p);
return false;
} else{
}
}
rule = steps[i].r;
}
return true;
};
})();
(function(){
var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g;
var relative = {
'+': true,
'>': true,
'~': true
};
parseCSSSelector = function(selector){
var m, cur,
parts = [],
steps = [],
soFar = selector;
do {
chunker.exec( "" );
m = chunker.exec( soFar );
if ( m ) {
soFar = m[3];
parts.push( m[1] );
if ( m[2] ) {
extra = m[3];
break;
}
}
} while ( m );
while ( parts.length ) {
cur = parts.shift();
pop = cur;
if ( !relative[cur]) {
cur = "";
} else {
pop = parts.shift();
}
if ( pop == null ) {
pop = context;
}
steps.push({t: pop, r: cur});
//Expr.relative[ cur ]( checkSet, pop, contextXML );
}
return steps;
};
})();
var findCSSRules = function(selector_part){
selector_part=selector_part.toLowerCase();
var target_rules = [];
if (document.styleSheets) {
for (var i=0; i < document.styleSheets.length; i++) {
var styleSheet = document.styleSheets[i];
var c_rules = styleSheet.cssRules || styleSheet.rules;
var ct_rules = $filter(c_rules, 'selectorText', function(v){
if (v){
if (bN(v.toLowerCase().indexOf(selector_part))){
return true;
}
}
});
if (ct_rules.length){
target_rules = target_rules.concat.apply(target_rules, ct_rules);
}
}
return target_rules.length && target_rules;
}
return false;
};
/*
var killCSSRule = function (selector_part) {
return getCSSRule(selector_part,'delete');
}
var addCSSRule = function (selector_part) {
if (document.styleSheets) {
if (!getCSSRule(selector_part)) {
if (document.styleSheets[0].addRule) {
document.styleSheets[0].addRule(selector_part, null,0);
} else {
document.styleSheets[0].insertRule(selector_part+' { }', 0);
}
}
}
return getCSSRule(selector_part);
}
*/
//var AnimateToCSSClass=
var checkNodeParticipation = function(node, god_father, oldold_parents, steps){
var r=[];
for (var i=0; i < node.length; i++) {
var n = node[i];
var node_parents = getAllParents(n, false, god_father).concat(oldold_parents);
if (checkRelativesBySteps(n, steps, node_parents)){
r.push(n);
}
}
return r.length && r;
};
var getECParticipials = function(el, class_name){
var classes = class_name instanceof Array ? class_name : [class_name];
var tnodes = [],
parents = getAllParents(el, {exception: {classes : classes}});
for (var ii=0; ii < classes.length; ii++) {
var c_class_name = classes[ii];
var rules = findCSSRules("." + c_class_name);
for (var i=0; i < rules.length; i++) {
var steps = parseCSSSelector(rules[i].selectorText);
if (steps.length > 1){
var last_step = steps[steps.length-1].t;
if (last_step.indexOf("." +c_class_name) == -1){
var node = $(el).find(last_step);
var ns = checkNodeParticipation(node, el, parents, steps);
if (ns){
tnodes = tnodes.concat(ns);
}
}
}
}
}
return tnodes.length && collapseAll(tnodes);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment