Skip to content

Commit

Permalink
remove redundant ID parts of selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
ndp committed Sep 28, 2010
1 parent 464f7fc commit 636317b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
14 changes: 14 additions & 0 deletions spec/core_spec.js
Expand Up @@ -128,6 +128,20 @@ describe("Csster", function() {
})).toEqual([{sel:"div.cls", props:"height: 235px;\r"}]);
});

it('should not remove redundant ids', function() {
Csster.shortCircuitIds = false;
expect(Csster.formatRules({
'#a #b #c': {width: 235}
})).toEqual([{sel:"#a #b #c", props:"width: 235px;\r"}]);
});

it('should remove redundant ids', function() {
Csster.shortCircuitIds = true;
expect(Csster.formatRules({
'#a #b #c': {width: 235}
})).toEqual([{sel:"#c", props:"width: 235px;\r"}]);
});


function roundedCorners(radius) {
return {
Expand Down
26 changes: 22 additions & 4 deletions src/core.js
@@ -1,5 +1,11 @@
if (!Csster) {
var Csster = {}
var Csster = {
/**
* Remove redundant parents from selectors that include more than one ID
* selector. eg. #page #top => "#top"
*/
shortCircuitIds: true
}
}

Csster.propertyNames = ['accelerator',
Expand Down Expand Up @@ -341,7 +347,7 @@ Csster.formatSelectorAndProperties = function(selector, properties) {
}

var subs = p.split(',');
for (var s = 0; s<subs.length; s++) {
for (var s = 0; s < subs.length; s++) {
subs[s] = selector + (subs[s][0] == '&' ? subs[s].substr(1) : ' ' + subs[s]);
}
rules.push(Csster.formatSelectorAndProperties(subs.join(','), properties[p]));
Expand All @@ -352,12 +358,20 @@ Csster.formatSelectorAndProperties = function(selector, properties) {

Csster.insertStylesheet = function (rules) {
var ss = document.styleSheets[document.styleSheets.length - 1];
for (var i = 0; i < rules.length; i++ ) {
for (var i = 0; i < rules.length; i++) {
ss.insertRule(rules[i].sel + "{" + rules[i].props + "}", ss.cssRules.length);
}
};


Csster.compressSelectors = function(rules) {
for (var i = 0; i < rules.length; i++) {
while (rules[i].sel.match(/.*#.*#.*/)) {
rules[i].sel = rules[i].sel.replace(/^.*#.*#/, '#');
}
}
};

Csster.formatRules = function(rs) {

// @param cssRule { selector: { prop1: value, prop2: value, subselector: { prop3: value}}
Expand All @@ -374,7 +388,11 @@ Csster.formatRules = function(rs) {
[rs].flatten().each(function(r) {
result.push(resolveRuleHash(r));
});
return result.flatten();
result = result.flatten();
if (Csster.shortCircuitIds) {
Csster.compressSelectors(result);
}
return result;
};

Csster.style = function(cssRules) {
Expand Down

0 comments on commit 636317b

Please sign in to comment.