From 94f9146082a2f067efc3fb2a1dbad8728518652c Mon Sep 17 00:00:00 2001 From: Jonas Ohlsson Aden Date: Sat, 30 Jul 2016 12:17:02 +0200 Subject: [PATCH 1/4] handle invalid extra closing brace --- lib/parse/index.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/parse/index.js b/lib/parse/index.js index 053f0596..61a9fa90 100644 --- a/lib/parse/index.js +++ b/lib/parse/index.js @@ -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`. @@ -95,7 +96,9 @@ module.exports = function(css, options){ */ function open() { - return match(/^{\s*/); + var hasopenbrace = match(/^{\s*/); + if (hasopenbrace) nestinglevel++ + return hasopenbrace } /** @@ -103,7 +106,9 @@ module.exports = function(css, options){ */ function close() { - return match(/^}/); + var hasclosebrace = match(/^}/); + if (hasclosebrace) nestinglevel-- + return hasclosebrace } /** @@ -119,6 +124,7 @@ module.exports = function(css, options){ if (node !== false) { rules.push(node); comments(rules); + incorrectclosingbrace(); } } return rules; @@ -188,6 +194,20 @@ 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(); + } + } + /** * Parse selector. */ From 5a6dc1f9bfa8dd66d59e8beb3dd4c34e71e87316 Mon Sep 17 00:00:00 2001 From: Jonas Ohlsson Aden Date: Sat, 30 Jul 2016 12:27:27 +0200 Subject: [PATCH 2/4] test for invalid extra closing brace --- test/parse.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/parse.js b/test/parse.js index b39b6ee2..fbeabbae 100644 --- a/test/parse.js +++ b/test/parse.js @@ -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' + From 555b80d0078700a25b4b5931c973f09cd5723570 Mon Sep 17 00:00:00 2001 From: Jonas Ohlsson Aden Date: Sun, 31 Jul 2016 22:11:57 +0200 Subject: [PATCH 3/4] fixup! handle invalid extra closing brace --- lib/parse/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/parse/index.js b/lib/parse/index.js index 61a9fa90..e40c5243 100644 --- a/lib/parse/index.js +++ b/lib/parse/index.js @@ -203,7 +203,7 @@ module.exports = function(css, options){ error('Extra closing brace') // remove the closing brace so parsing can continue css = css.slice(1); - updatePosition('}') + updatePosition('}'); whitespace(); } } From bd113ce7c8fa7be7d97b412cfdf698c84ddd2f69 Mon Sep 17 00:00:00 2001 From: Jonas Ohlsson Aden Date: Sun, 31 Jul 2016 22:12:28 +0200 Subject: [PATCH 4/4] recursion for handling sequences of incorrect closing braces --- lib/parse/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/parse/index.js b/lib/parse/index.js index e40c5243..d88c23f3 100644 --- a/lib/parse/index.js +++ b/lib/parse/index.js @@ -205,6 +205,8 @@ module.exports = function(css, options){ css = css.slice(1); updatePosition('}'); whitespace(); + // catch multiple incorrect closing braces in a row + incorrectclosingbrace(); } }