Skip to content

Commit 72f4293

Browse files
MadeByMikenecolas
authored andcommitted
Add a 'silent' option
Do not throw on errors.
1 parent 02235f9 commit 72f4293

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

Readme.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ console.log(JSON.stringify(output_obj, null, 2));
3030

3131
`options`:
3232

33+
- `silent` - silently fail on parse errrors.
3334
- `source` - recommended for debugging.
3435
- `position` - `true` by default.
3536

@@ -153,9 +154,9 @@ also pass in `source: 'path/to/original.css'`, that will be set on
153154
Parsed 15,000 lines of CSS (2mb) in 40ms on my macbook air.
154155

155156
## Related
156-
157-
[css-stringify](https://github.com/visionmedia/css-stringify "CSS-Stringify")
158-
[css-value](https://github.com/visionmedia/css-value "CSS-Value")
157+
158+
[css-stringify](https://github.com/visionmedia/css-stringify "CSS-Stringify")
159+
[css-value](https://github.com/visionmedia/css-value "CSS-Value")
159160

160161
## License
161162

index.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ module.exports = function(css, options){
6969
*/
7070

7171
function error(msg) {
72+
if (options.silent === true) {
73+
return false;
74+
}
75+
7276
var err = new Error(msg + ' near line ' + lineno + ':' + column);
7377
err.filename = options.source;
7478
err.line = lineno;
@@ -116,8 +120,10 @@ module.exports = function(css, options){
116120
whitespace();
117121
comments(rules);
118122
while (css.length && css.charAt(0) != '}' && (node = atrule() || rule())) {
119-
rules.push(node);
120-
comments(rules);
123+
if (node !== false) {
124+
rules.push(node);
125+
comments(rules);
126+
}
121127
}
122128
return rules;
123129
}
@@ -150,7 +156,11 @@ module.exports = function(css, options){
150156
function comments(rules) {
151157
var c;
152158
rules = rules || [];
153-
while (c = comment()) rules.push(c);
159+
while (c = comment()) {
160+
if (c !== false) {
161+
rules.push(c);
162+
}
163+
}
154164
return rules;
155165
}
156166

@@ -245,8 +255,10 @@ module.exports = function(css, options){
245255
// declarations
246256
var decl;
247257
while (decl = declaration()) {
248-
decls.push(decl);
249-
comments(decls);
258+
if (decl !== false) {
259+
decls.push(decl);
260+
comments(decls);
261+
}
250262
}
251263

252264
if (!close()) return error("missing '}'");

test/css-parse.js

+7
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,11 @@ describe('parse(str)', function(){
6666
parse('p { color:; }');
6767
});
6868
});
69+
70+
it('should not throw with silent option', function () {
71+
assert.doesNotThrow(function(){
72+
parse('thing { color: red; } /* b { color: blue; }',{ silent: true });
73+
});
74+
});
75+
6976
});

0 commit comments

Comments
 (0)