From 3e6e5e53042987ffa3cd16397f820211b3f4fdd0 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 27 Feb 2017 16:16:19 -0500 Subject: [PATCH] normalise CSS parse errors --- src/parse/read/style.js | 18 ++++++++++++++---- test/parser/error-css/error.json | 8 ++++++++ test/parser/error-css/input.html | 3 +++ 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 test/parser/error-css/error.json create mode 100644 test/parser/error-css/input.html diff --git a/src/parse/read/style.js b/src/parse/read/style.js index 68e05da4ca..d367aba627 100644 --- a/src/parse/read/style.js +++ b/src/parse/read/style.js @@ -6,10 +6,20 @@ export default function readStyle ( parser, start, attributes ) { const styles = parser.readUntil( /<\/style>/ ); const contentEnd = parser.index; - const ast = parse( styles, { - positions: true, - offset: contentStart - }); + let ast; + + try { + ast = parse( styles, { + positions: true, + offset: contentStart + }); + } catch ( err ) { + if ( err.name === 'CssSyntaxError' ) { + parser.error( err.message, err.offset ); + } else { + throw err; + } + } // tidy up AST walk.all( ast, node => { diff --git a/test/parser/error-css/error.json b/test/parser/error-css/error.json new file mode 100644 index 0000000000..177f6d977f --- /dev/null +++ b/test/parser/error-css/error.json @@ -0,0 +1,8 @@ +{ + "message": "LeftCurlyBracket is expected", + "loc": { + "line": 2, + "column": 16 + }, + "pos": 24 +} diff --git a/test/parser/error-css/input.html b/test/parser/error-css/input.html new file mode 100644 index 0000000000..d073ec6258 --- /dev/null +++ b/test/parser/error-css/input.html @@ -0,0 +1,3 @@ + \ No newline at end of file