Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle invalid extra closing brace #99

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 24 additions & 2 deletions lib/parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = function(css, options){

var lineno = 1;
var column = 1;
var nestinglevel = 0

/**
* Update lineno and column based on `str`.
Expand Down Expand Up @@ -95,15 +96,19 @@ module.exports = function(css, options){
*/

function open() {
return match(/^{\s*/);
var hasopenbrace = match(/^{\s*/);
if (hasopenbrace) nestinglevel++
return hasopenbrace
}

/**
* Closing brace.
*/

function close() {
return match(/^}/);
var hasclosebrace = match(/^}/);
if (hasclosebrace) nestinglevel--
return hasclosebrace
}

/**
Expand All @@ -119,6 +124,7 @@ module.exports = function(css, options){
if (node !== false) {
rules.push(node);
comments(rules);
incorrectclosingbrace();
}
}
return rules;
Expand Down Expand Up @@ -188,6 +194,22 @@ module.exports = function(css, options){
});
}

/**
* Parse incorrect closing brace.
*/

function incorrectclosingbrace() {
if (css.length && nestinglevel === 0 && css.charAt(0) == '}') {
error('Extra closing brace')
// remove the closing brace so parsing can continue
css = css.slice(1);
updatePosition('}');
whitespace();
// catch multiple incorrect closing braces in a row
incorrectclosingbrace();
}
}

/**
* Parse selector.
*/
Expand Down
16 changes: 16 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ describe('parse(str)', function() {

});

it('should handle invalid extra closing brace', function() {
var result = parse('foo { color: red; }} bar { color: blue; }', {
silent: true,
source: 'foo.css'
});

var rules = result.stylesheet.rules;
rules.length.should.equal(2);

var errors = result.stylesheet.parsingErrors;
errors.length.should.equal(1);

errors[0].line.should.equal(1);
errors[0].column.should.equal(20);
});

it('should set parent property', function() {
var result = parse(
'thing { test: value; }\n' +
Expand Down