Skip to content

Instantly share code, notes, and snippets.

@il-katta
Created May 28, 2013 00:16
Show Gist options
  • Save il-katta/5659715 to your computer and use it in GitHub Desktop.
Save il-katta/5659715 to your computer and use it in GitHub Desktop.
nth-child for IE browser
/* thanks to:
* http://stackoverflow.com/questions/3326494/parsing-css-in-javascript-jquery
* for this parser
*/
function CSS(css){
this.parseCSS=function(css) {
var rules = {};
css = this.removeComments(css);
var blocks = css.split('}');
blocks.pop();
var len = blocks.length;
for (var i = 0; i < len; i++)
{
var pair = blocks[i].split('{');
rules[$.trim(pair[0])] = this.parseCSSBlock(pair[1]);
}
return rules;
}
this.parseCSSBlock= function(css) {
var rule = {};
var declarations = css.split(';');
declarations.pop();
var len = declarations.length;
for (var i = 0; i < len; i++)
{
var loc = declarations[i].indexOf(':');
var property = $.trim(declarations[i].substring(0, loc));
var value = $.trim(declarations[i].substring(loc + 1));
if (property != "" && value != "")
rule[property] = value;
}
return rule;
}
this.removeComments= function(css) {
return css.replace(/\/\*(\r|\n|.)*\*\//g,"");
}
this.rules=this.parseCSS(css);
}
$('link[type="text/css"]').each(function(i,v){
$.get($(v).attr("href")).done(
function(data){
$.getScript("js/libs/css-utils.js").done(function(){
var css_parsed=(new CSS(data)).rules;
$.each(css_parsed,function(i,v){
if(i.search(/\:nth-(last-of-type|of-type|lasth-child|child)/i)>=0){
$(i).css(v);
}
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment