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

ignore error css code gracefully #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
39 changes: 31 additions & 8 deletions lib/parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,27 @@ module.exports = function(css, options){
* Parse declarations.
*/

function handleClosingMissing(subject){
var missingCloseCount = 1;
while(missingCloseCount > 0){
// find the next closing bracket
var match = /}/.exec(css);
if(!match)
return error(subject + "missing '}'");
// update position and css
var str = css.substr(0,match.index + match[0].length);
updatePosition(str);
css = css.substr(match.index + match[0].length);
// clean comments
str = str.replace(/\/\*(.|\n|\r)*?\*\//g,'');
// search for opening or closing brackets
var opened = (str.match(/{/g) || []).length;
var closed = (str.match(/}/g) || []).length;
// update bracket closing counter
missingCloseCount += opened - closed;
}
}

function declarations() {
var decls = [];

Expand All @@ -257,7 +278,8 @@ module.exports = function(css, options){
}
}

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

Expand Down Expand Up @@ -309,7 +331,8 @@ module.exports = function(css, options){
frames = frames.concat(comments());
}

if (!close()) return error("@keyframes missing '}'");
if (!close())
return handleClosingMissing('@keyframes');

return pos({
type: 'keyframes',
Expand All @@ -334,7 +357,7 @@ module.exports = function(css, options){

var style = comments().concat(rules());

if (!close()) return error("@supports missing '}'");
if (!close())return handleClosingMissing('@supports');

return pos({
type: 'supports',
Expand All @@ -357,7 +380,7 @@ module.exports = function(css, options){

var style = comments().concat(rules());

if (!close()) return error("@host missing '}'");
if (!close())return handleClosingMissing('@host');

return pos({
type: 'host',
Expand All @@ -380,7 +403,7 @@ module.exports = function(css, options){

var style = comments().concat(rules());

if (!close()) return error("@media missing '}'");
if (!close()) return handleClosingMissing("@media");

return pos({
type: 'media',
Expand Down Expand Up @@ -427,7 +450,7 @@ module.exports = function(css, options){
decls = decls.concat(comments());
}

if (!close()) return error("@page missing '}'");
if (!close()) return handleClosingMissing("@page");

return pos({
type: 'page',
Expand All @@ -452,7 +475,7 @@ module.exports = function(css, options){

var style = comments().concat(rules());

if (!close()) return error("@document missing '}'");
if (!close()) return handleClosingMissing("@document");

return pos({
type: 'document',
Expand Down Expand Up @@ -481,7 +504,7 @@ module.exports = function(css, options){
decls = decls.concat(comments());
}

if (!close()) return error("@font-face missing '}'");
if (!close()) return handleClosingMissing("@font-face");

return pos({
type: 'font-face',
Expand Down