Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ Example configuration:
"element-case": "lower",
"eof-newline": true,
"leading-zero": false,
"quotes": "single",
"remove-empty-rulesets": true,
"rule-indent": " ",
"stick-brace": "\n",
Expand Down Expand Up @@ -499,6 +500,20 @@ p { padding: 0.5em }
p { padding: .5em }
```

### quotes

Available values: `{String}` `single` or `double`

Example: `{ "quotes": "single" }`

```css
/* before */
p[href^="https://"]:before { content: "secure" }

/* after */
p[href^='https://']:before { content: 'secure' }
```

### remove-empty-rulesets

Available values: `{Boolean}` `true`
Expand Down
1 change: 1 addition & 0 deletions config/csscomb.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"element-case": "lower",
"eof-newline": true,
"leading-zero": false,
"quotes": "single",
"remove-empty-rulesets": true,
"rule-indent": " ",
"stick-brace": "\n",
Expand Down
1 change: 1 addition & 0 deletions lib/csscomb.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var Comb = function() {
'color-shorthand',
'element-case',
'leading-zero',
'quotes',
'strip-spaces',
'eof-newline',
'stick-brace',
Expand Down
55 changes: 55 additions & 0 deletions lib/options/quotes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module.exports = {

/**
* Sets handler value.
*
* @param {String|Boolean} value Option value
* @returns {Object|undefined}
*/
setValue: function(value) {
if (value === 'single' || value === 'double' ) {
this._value = value;
}

if (!this._value) return;
return this;
},

/**
* Processes tree node.
* @param {String} nodeType
* @param {node} node
*/
process: function(nodeType, node) {
if (nodeType === 'string') {
if (node[0][0] === '"' && this._value === 'single') {
node[0] = node[0]
.replace(/\\"/g, '"') // unescape all escaped double quotes
.replace(/([^\\])'/g, '$1\\\'') // escape all the single quotes
.replace(/^"|"$/g, '\''); // replace the first and the last quote

} else if (node[0][0] === '\'' && this._value === 'double') {
node[0] = node[0]
.replace(/\\'/g, '\'') // unescape all escaped single quotes
.replace(/([^\\])"/g, '$1\\\"') // escape all the double quotes
.replace(/^'|'$/g, '"'); // replace the first and the last quote
}
}
},

/**
* Detects the value of an option at the tree node.
*
* @param {String} nodeType
* @param {node} node
*/
detect: function(nodeType, node) {
if (nodeType === 'string') {
if (node[0][0] === '"') {
return 'double';
} else if (node[0][0] === '\'') {
return 'single';
}
}
}
};
6 changes: 5 additions & 1 deletion test/integral.expect.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
}

/* :after — фон */
.radio-button_theme_normal .radio-button__radio:after
.radio-button_theme_normal .radio-button__radio[class^='radio']:after
{
content: 'it\'s something different';

background: #fff;
background: -webkit-linear-gradient(top, #fff 0,#eee 100%);
background: -moz-linear-gradient(top, #fff 0, #eee 100%);
Expand All @@ -26,6 +28,8 @@
/* _focused_yes */
.radio-button_theme_normal .radio-button__radio_focused_yes:before
{
content: 'hello';

-moz-box-shadow: 0 0 6px 2px rgba(255,204,0,.7), 0 1px 0 rgba(0,0,0,.07);
box-shadow: 0 0 6px 2px rgba(255,204,0,.7), 0 1px 0 rgba(0,0,0,.07);
}
Expand Down
1 change: 1 addition & 0 deletions test/integral.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ vow.all(['origin', 'expect'].map(function(type) {
'color-shorthand': true,
'element-case': 'lower',
'leading-zero': false,
'quotes': 'single',
'strip-spaces': true,
'eof-newline': true,
'stick-brace': '\n',
Expand Down
4 changes: 3 additions & 1 deletion test/integral.origin.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
}

/* :after — фон */
.radio-button_theme_normal .radio-button__radio:after
.radio-button_theme_normal .radio-button__radio[class^="radio"]:after
{
background: #fFf;
background: -webkit-linear-gradient(top, #FffFff 0,#eeeeEe 100%);
background: -moz-linear-gradient(top, #fff 0, #eEe 100%);
background: -o-linear-gradient(top, #fff 0,#eee 100%);
background: linear-gradient(to bottom, #ffffff 0,#eeEeee 100%);
content: "it's something different";
}

/* _focused_yes */
.radio-button_theme_normal .radio-button__radio_focused_yes:before
{
content: "hello";
-moz-box-shadow: 0 0 6px 2px rgba(255,204,0,.7), 0px 1px 0px rgba(0,0,0,.07);
box-shadow: 0 0 6px 2px rgba(255,204,0,.7), 0px 1px 0px rgba(0,0,0,.07);
}
Expand Down
134 changes: 134 additions & 0 deletions test/quotes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
var Comb = require('../lib/csscomb');
var assert = require('assert');

describe('options/quotes', function() {
var comb;
beforeEach(function() {
comb = new Comb();
});

it('Invalid String should not change quotes', function() {
comb.configure({ quotes: 'foobar' });
assert.equal(
comb.processString(
'a { content: "" }' +
'b { content: \'\' }'
),
'a { content: "" }' +
'b { content: \'\' }'
);
});

it('`single` value should set the quotes to single', function() {
comb.configure({ quotes: 'single' });
assert.equal(
comb.processString(
'a { content: "" }' +
'b { content: \'\' }'
),
'a { content: \'\' }' +
'b { content: \'\' }'
);
});

it('`double` value should set the quotes to double', function() {
comb.configure({ quotes: 'double' });
assert.equal(
comb.processString(
'a { content: "" }' +
'b { content: \'\' }'
),
'a { content: "" }' +
'b { content: "" }'
);
});

it('`double` value should set the quotes to double in attrs and urls', function() {
comb.configure({ quotes: 'double' });
assert.equal(
comb.processString(
'a[class^=\'foo\'] { background: url(\'foo.png\') }'
),
'a[class^="foo"] { background: url("foo.png") }'
);
});

it('`double` value should escape the unescaped double quotes on change', function() {
comb.configure({ quotes: 'double' });
assert.equal(
comb.processString(
'a { content: "\\"" }' +
'b { content: \'"\' }'
),
'a { content: "\\"" }' +
'b { content: "\\"" }'
);
});


it('`single` value should unescape the escaped double quotes on change', function() {
comb.configure({ quotes: 'single' });
assert.equal(
comb.processString(
'a { content: "\\"" }'
),
'a { content: \'"\' }'
);
});

// Helper to check the detection
function should_detect(options, a, b) {
assert.equal(
JSON.stringify(comb.detectInString(a, options)),
JSON.stringify(b)
);
}

it('Should not detect quotes when there are none', function() {
should_detect(
['quotes'],
'a { color:red }',
{}
);
});

it('Should detect double quotes', function() {
should_detect(
['quotes'],
'a { content: "foo" }',
{
quotes: 'double'
}
);
});

it('Should detect single quotes', function() {
should_detect(
['quotes'],
'a { content: \'foo\' }',
{
quotes: 'single'
}
);
});

it('Should detect single quotes in attribute', function() {
should_detect(
['quotes'],
'a[class^=\'foo\'] { color: red }',
{
quotes: 'single'
}
);
});

it('Should detect double quotes in url', function() {
should_detect(
['quotes'],
'a { background: url("foo.png") }',
{
quotes: 'double'
}
);
});
});