Skip to content

Commit

Permalink
Added leftovers token to sort-order option, fixes csscomb#160.
Browse files Browse the repository at this point in the history
  • Loading branch information
kizu authored and tonyganch committed Jun 9, 2014
1 parent bb759a8 commit 09223a0
Show file tree
Hide file tree
Showing 14 changed files with 298 additions and 2 deletions.
22 changes: 22 additions & 0 deletions doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,28 @@ p {
}
```

### Leftovers

When there are properties that are not mentioned in the `sort-order` option, they are inserted after all the sorted properties in the new group in the same order they were in the source stylesheet.

You can override this by using a “leftovers” token: `...` — just place it either in its own group, or near other properties in any other group and CSSComb would place all the properties that were not sorted where the `...` was in `sort-order`.

So, with this value:

``` json
{
"sort-order": [
["$variable"],
["position"],
["...", "border"],
["$include"],
["font"]
]
}
```

everything would go into five groups: variables, then group with `position`, then group containing all the leftovers plus the `border`, then group with all includes and then the `font`.

## space-after-colon

Set space after `:` in declarations.
Expand Down
9 changes: 7 additions & 2 deletions lib/options/sort-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ module.exports = {
// (e.g. `color`), or @-rule's special keyword (e.g. `$import`):
var propertyName;

// Index to place the nodes that shouldn't be sorted
var lastGroupIndex = order['...'] ? order['...'].group : Infinity;
var lastPropertyIndex = order['...'] ? order['...'].prop : Infinity;

// Counters for loops:
var i;
var l;
Expand Down Expand Up @@ -173,10 +177,11 @@ module.exports = {
// If the declaration's property is in order's list, save its
// group and property indices. Otherwise set them to 10000, so
// declaration appears at the bottom of a sorted list:

extendedNode.groupIndex = orderProperty && orderProperty.group > -1 ?
orderProperty.group : 10000;
orderProperty.group : lastGroupIndex;
extendedNode.propertyIndex = orderProperty && orderProperty.prop > -1 ?
orderProperty.prop : 10000;
orderProperty.prop : lastPropertyIndex;

// Mark current node to remove it later from parent node:
deleted.push(i);
Expand Down
13 changes: 13 additions & 0 deletions test/options/sort-order-scss.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,17 @@ describe('options/sort-order (scss)', function() {
] });
this.shouldBeEqual('condition.scss', 'condition.expected.scss');
});

it('Should sort complex case with leftovers', function() {
this.comb.configure({
"sort-order": [
["$variable"],
["position"],
["...", "border"],
["$include"],
["font"]
]
});
this.shouldBeEqual('leftovers.scss', 'leftovers.expected.scss');
});
});
36 changes: 36 additions & 0 deletions test/options/sort-order-scss/leftovers.expected.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
a {
$red: tomato;

position: $tomato;

leftover: yay;
border: 1px solid;

@include .nani;

font: 20px/16px Arial, sans-serif;
}
b {
$red: tomato;

position: $tomato;

leftover: yay;
border: 1px solid;

@include .nani;

font: 20px/16px Arial, sans-serif;
}
c {
$red: tomato;

position: $tomato;

leftover: yay;
border: 1px solid;

@include .nani;

font: 20px/16px Arial, sans-serif;
}
24 changes: 24 additions & 0 deletions test/options/sort-order-scss/leftovers.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
a {
leftover: yay;
border: 1px solid;
@include .nani;
font: 20px/16px Arial, sans-serif;
position: $tomato;
$red: tomato;
}
b {
border: 1px solid;
@include .nani;
position: $tomato;
leftover: yay;
font: 20px/16px Arial, sans-serif;
$red: tomato;
}
c {
border: 1px solid;
@include .nani;
font: 20px/16px Arial, sans-serif;
position: $tomato;
$red: tomato;
leftover: yay;
}
30 changes: 30 additions & 0 deletions test/options/sort-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,34 @@ describe('options/sort-order', function() {
this.comb.configure(config);
this.shouldBeEqual('issue-94-3.css', 'issue-94-3.expected.css');
});

it('Should place the leftovers in the end', function() {
var config = this.Comb.getConfig('csscomb');
this.comb.configure(config);
this.shouldBeEqual('leftovers-1.css', 'leftovers-1.expected.css');
});

it('Should place the leftovers in the beginning', function() {
var config = this.Comb.getConfig('csscomb');
config['sort-order'][0].unshift(['...']);
this.comb.configure(config);
this.shouldBeEqual('leftovers-2.css', 'leftovers-2.expected.css');
config['sort-order'][0].shift();
});

it('Should place the leftovers in the beginning of its group', function() {
var config = this.Comb.getConfig('csscomb');
config['sort-order'][1].unshift('...');
this.comb.configure(config);
this.shouldBeEqual('leftovers-3.css', 'leftovers-3.expected.css');
config['sort-order'][1].shift();
});

it('Should place the leftovers in the middle of its group', function() {
var config = this.Comb.getConfig('csscomb');
config['sort-order'][1].splice(1, 0, '...');
this.comb.configure(config);
this.shouldBeEqual('leftovers-4.css', 'leftovers-4.expected.css');
config['sort-order'][1].splice(1, 1);
});
});
20 changes: 20 additions & 0 deletions test/options/sort-order/leftovers-1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
a
{
leftover: yay;
z-index: 1;
position: absolute;
}

b
{
z-index: 2;
leftover: yay;
position: absolute;
}

c
{
z-index: 3;
position: absolute;
leftover: yay;
}
23 changes: 23 additions & 0 deletions test/options/sort-order/leftovers-1.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
a
{
position: absolute;
z-index: 1;

leftover: yay;
}

b
{
position: absolute;
z-index: 2;

leftover: yay;
}

c
{
position: absolute;
z-index: 3;

leftover: yay;
}
20 changes: 20 additions & 0 deletions test/options/sort-order/leftovers-2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
a
{
leftover: yay;
z-index: 1;
position: absolute;
}

b
{
z-index: 2;
leftover: yay;
position: absolute;
}

c
{
z-index: 3;
position: absolute;
leftover: yay;
}
23 changes: 23 additions & 0 deletions test/options/sort-order/leftovers-2.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
a
{
leftover: yay;

position: absolute;
z-index: 1;
}

b
{
leftover: yay;

position: absolute;
z-index: 2;
}

c
{
leftover: yay;

position: absolute;
z-index: 3;
}
20 changes: 20 additions & 0 deletions test/options/sort-order/leftovers-3.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
a
{
leftover: yay;
z-index: 1;
position: absolute;
}

b
{
z-index: 2;
leftover: yay;
position: absolute;
}

c
{
z-index: 3;
position: absolute;
leftover: yay;
}
20 changes: 20 additions & 0 deletions test/options/sort-order/leftovers-3.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
a
{
leftover: yay;
position: absolute;
z-index: 1;
}

b
{
leftover: yay;
position: absolute;
z-index: 2;
}

c
{
leftover: yay;
position: absolute;
z-index: 3;
}
20 changes: 20 additions & 0 deletions test/options/sort-order/leftovers-4.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
a
{
leftover: yay;
z-index: 1;
position: absolute;
}

b
{
z-index: 2;
leftover: yay;
position: absolute;
}

c
{
z-index: 3;
position: absolute;
leftover: yay;
}
20 changes: 20 additions & 0 deletions test/options/sort-order/leftovers-4.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
a
{
position: absolute;
leftover: yay;
z-index: 1;
}

b
{
position: absolute;
leftover: yay;
z-index: 2;
}

c
{
position: absolute;
leftover: yay;
z-index: 3;
}

0 comments on commit 09223a0

Please sign in to comment.