Skip to content

Commit

Permalink
don't merge rulesets if between them a ruleset with same specificity (f…
Browse files Browse the repository at this point in the history
…ixes #264)
  • Loading branch information
lahmatiy committed Jan 22, 2016
1 parent af24398 commit 379ee44
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/compressor/restructure/rejoinRuleset.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = function rejoinRuleset(node, parent, array, i) {
return null;
}

var compareMarkers = {};
compareMarkers[selector[0].info.compareMarker] = true;
var nodeCompareMarker = selector[0].info.compareMarker;
var skippedCompareMarkers = {};

for (i = i + 1; i < array.length; i++) {
var next = array[i];
Expand All @@ -24,6 +24,12 @@ module.exports = function rejoinRuleset(node, parent, array, i) {

var nextFirstSelector = next.selector.selectors[0];
var nextBlock = next.block.declarations;
var nextCompareMarker = nextFirstSelector.info.compareMarker;

// if next ruleset has same marked as one of skipped then stop joining
if (nextCompareMarker in skippedCompareMarkers) {
return;
}

// try to join by selectors
if (selector.length === 1) {
Expand All @@ -41,10 +47,9 @@ module.exports = function rejoinRuleset(node, parent, array, i) {
}

// try to join by properties
var nextCompareMarker = nextFirstSelector.info.compareMarker;
var equalBlocks = true;

if (block.length === nextBlock.length) {
var equalBlocks = true;

for (var j = 0; j < block.length; j++) {
if (block[j].info.s !== nextBlock[j].info.s) {
equalBlocks = false;
Expand All @@ -62,17 +67,18 @@ module.exports = function rejoinRuleset(node, parent, array, i) {
}
}

compareMarkers[nextCompareMarker] = true;
array.splice(i, 1);
i--;

continue;
}
}

// go to next ruleset if simpleselectors has no equal specifity and element selector
if (nextCompareMarker in compareMarkers) {
// go to next ruleset if current one can be skipped (has no equal specificity nor element selector)
if (nextCompareMarker === nodeCompareMarker) {
return;
}

skippedCompareMarkers[nextCompareMarker] = true;
}
};
15 changes: 15 additions & 0 deletions test/fixture/compress/restructure.merge/7.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
issue #264 don't merge rulesets if between there is a ruleset with same specificity
*/

.notification-banner.drop {
top: -50px
}

.notification-banner.drop.in {
top: 0
}

.notification-banner.drop.out {
top: -50px
}
1 change: 1 addition & 0 deletions test/fixture/compress/restructure.merge/7.min.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.notification-banner.drop{top:-50px}.notification-banner.drop.in{top:0}.notification-banner.drop.out{top:-50px}

1 comment on commit 379ee44

@davidmfoley
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rad; thanks.

Please sign in to comment.