Skip to content

Commit

Permalink
Combinator space: Change acceptable values
Browse files Browse the repository at this point in the history
`combinator-space` option now accepts only `{Array}` of these kinds of values:
    - `{Number}` of spaces;
    - `{String}` of whitespaces and tabs. If there is any other character in the
      string, the value will not be set.
The first element of array is spaces before combinator, and second one is spaces
after combinator.
  • Loading branch information
tonyganch committed Dec 3, 2013
1 parent 7cbbe70 commit a261945
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .csscomb.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"colon-space": ["", " "],
"color-case": "lower",
"color-shorthand": true,
"combinator-space": true,
"combinator-space": [" ", " "],
"element-case": "lower",
"eof-newline": true,
"leading-zero": false,
Expand Down
32 changes: 13 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,42 +429,36 @@ b { color: #fc0 }
### combinator-space
Available values:
* `{Boolean}`: `true` sets one space, `false` removes the spaces.
* `{String}`: any combination of whitespaces.
* `{Array}` with two `{String}` values: for setting left and right whitespace.
Example: `{ "combinator-space": true }`
```css
/* before */
a>b { color: red }
Acceptable value is `{Array}` with 2 elements of following types:
* `{Number}` of spaces;
* `{String}` of whitespaces, tabs or new lines. If there is any other
character in the string, the value will not be set.
/* after */
a > b { color: red }
```
The first element of the array sets spaces before combinator, and the second
one sets spaces after combinator.
Example: `{ "combinator-space": "" }`
Example: `{ "combinator-space": [" ", "\n"] }`
```css
/* before */
a > b { color: red }
a>b { color: red }
/* after */
a>b { color: red }
a >
b { color: red }
```
Example: `{ "combinator-space": [" ", "\n"] }`
Example: `{ "combinator-space": [1, 1] }`
```css
/* before */
a>b { color: red }
/* after */
a >
b { color: red }
a > b { color: red }
```
### element-case
Available values: `{String}` `lower` or `upper`
Expand Down
32 changes: 23 additions & 9 deletions lib/options/combinator-space.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,33 @@ module.exports = {
/**
* Sets handler value.
*
* @param {String|Boolean|Array} value Option value
* @returns {Object}
* @param {Array} value Option value
* @returns {Object|undefined}
*/
setValue: function(value) {
this._value = false;
if (value === true) value = ' ';
if (value === false) value = '';
if (typeof value === 'string' && value.match(/^[ \t\n]*$/)) {
this._value = [value, value];
delete this._value;

if (value.constructor !== Array) return;

if (typeof value[0] === 'number' &&
value[0] === Math.abs(Math.round(value[0]))) {
value[0] = new Array(value[0] + 1).join(' ');
} else if (typeof value[0] !== 'string' ||
!value[0].match(/^[ \t\n]*$/)) {
return;
}
if (value.constructor === Array) this._value = value;
if (!this._value) return;

if (typeof value[1] === 'number' &&
value[1] === Math.abs(Math.round(value[1]))) {
value[1] = new Array(value[1] + 1).join(' ');
} else if (typeof value[1] !== 'string' ||
!value[1].match(/^[ \t\n]*$/)) {
return;
}

this._value = value;
return this;

},

/**
Expand Down
155 changes: 41 additions & 114 deletions test/combinator-space.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,105 +3,31 @@ var assert = require('assert');

describe('options/combinator-space', function() {
var comb;

beforeEach(function() {
comb = new Comb();
});
it('Invalid String should not change space around combinator', function() {

it('Number value should not change space around combinator', function() {
var input = 'a >b { color: red }';
comb.configure({ 'combinator-space': 2 });
assert.equal(comb.processString(input), input);
});

it('String value should not change space around combinator', function() {
var input = 'a >b { color: red }';
comb.configure({ 'combinator-space': 'foobar' });
assert.equal(
comb.processString(
'a >b { color: red }' +
'a ~b { color: red }' +
'a +b { color: red }'
),
'a >b { color: red }' +
'a ~b { color: red }' +
'a +b { color: red }'
);
assert.equal(comb.processString(input), input);
});
it('True Boolean value should set space around combinator to one space', function() {

it('Boolean value should not change space around combinator', function() {
var input = 'a >b { color: red }';
comb.configure({ 'combinator-space': true });
assert.equal(
comb.processString(
'a>b { color: red }' +
'a> b { color: red }' +
'a >b { color: red }' +
'a+b { color: red }' +
'a+ b { color: red }' +
'a +b { color: red }' +
'a~b { color: red }' +
'a~ b { color: red }' +
'a ~b { color: red }' +
'a ~b+ c>d { color: red }'
),
'a > b { color: red }' +
'a > b { color: red }' +
'a > b { color: red }' +
'a + b { color: red }' +
'a + b { color: red }' +
'a + b { color: red }' +
'a ~ b { color: red }' +
'a ~ b { color: red }' +
'a ~ b { color: red }' +
'a ~ b + c > d { color: red }'
);
});
it('False Boolean value should remove spaces around combinator', function() {
comb.configure({ 'combinator-space': false });
assert.equal(
comb.processString(
'a>b { color: red }' +
'a> b { color: red }' +
'a >b { color: red }' +
'a+b { color: red }' +
'a+ b { color: red }' +
'a +b { color: red }' +
'a~b { color: red }' +
'a~ b { color: red }' +
'a ~b { color: red }' +
'a ~b+ c>d { color: red }'
),
'a>b { color: red }' +
'a>b { color: red }' +
'a>b { color: red }' +
'a+b { color: red }' +
'a+b { color: red }' +
'a+b { color: red }' +
'a~b { color: red }' +
'a~b { color: red }' +
'a~b { color: red }' +
'a~b+c>d { color: red }'
);
assert.equal(comb.processString(input), input);
});
it('String `` value should remove spaces around combinator', function() {
comb.configure({ 'combinator-space': '' });
assert.equal(
comb.processString(
'a>b { color: red }' +
'a> b { color: red }' +
'a >b { color: red }' +
'a+b { color: red }' +
'a+ b { color: red }' +
'a +b { color: red }' +
'a~b { color: red }' +
'a~ b { color: red }' +
'a ~b { color: red }' +
'a ~b+ c>d { color: red }'
),
'a>b { color: red }' +
'a>b { color: red }' +
'a>b { color: red }' +
'a+b { color: red }' +
'a+b { color: red }' +
'a+b { color: red }' +
'a~b { color: red }' +
'a~b { color: red }' +
'a~b { color: red }' +
'a~b+c>d { color: red }'
);
});
it('String ` ` value should set two spaces around combinator', function() {
comb.configure({ 'combinator-space': ' ' });

it('Array of strings should set proper spaces around combinator', function() {
comb.configure({ 'combinator-space': [' ', '\n'] });
assert.equal(
comb.processString(
'a>b { color: red }' +
Expand All @@ -115,20 +41,21 @@ describe('options/combinator-space', function() {
'a ~b { color: red }' +
'a ~b+ c>d { color: red }'
),
'a > b { color: red }' +
'a > b { color: red }' +
'a > b { color: red }' +
'a + b { color: red }' +
'a + b { color: red }' +
'a + b { color: red }' +
'a ~ b { color: red }' +
'a ~ b { color: red }' +
'a ~ b { color: red }' +
'a ~ b + c > d { color: red }'
'a >\nb { color: red }' +
'a >\nb { color: red }' +
'a >\nb { color: red }' +
'a +\nb { color: red }' +
'a +\nb { color: red }' +
'a +\nb { color: red }' +
'a ~\nb { color: red }' +
'a ~\nb { color: red }' +
'a ~\nb { color: red }' +
'a ~\nb +\nc >\nd { color: red }'
);
});
it('Array value should set different spaces around combinator', function() {
comb.configure({ 'combinator-space': [' ', '\n'] });

it('Array of numbers should set proper spaces around combinator', function() {
comb.configure({ 'combinator-space': [0, 1] });
assert.equal(
comb.processString(
'a>b { color: red }' +
Expand All @@ -142,16 +69,16 @@ describe('options/combinator-space', function() {
'a ~b { color: red }' +
'a ~b+ c>d { color: red }'
),
'a >\nb { color: red }' +
'a >\nb { color: red }' +
'a >\nb { color: red }' +
'a +\nb { color: red }' +
'a +\nb { color: red }' +
'a +\nb { color: red }' +
'a ~\nb { color: red }' +
'a ~\nb { color: red }' +
'a ~\nb { color: red }' +
'a ~\nb +\nc >\nd { color: red }'
'a> b { color: red }' +
'a> b { color: red }' +
'a> b { color: red }' +
'a+ b { color: red }' +
'a+ b { color: red }' +
'a+ b { color: red }' +
'a~ b { color: red }' +
'a~ b { color: red }' +
'a~ b { color: red }' +
'a~ b+ c> d { color: red }'
);
});

Expand Down

0 comments on commit a261945

Please sign in to comment.