@@ -303,26 +303,46 @@ function parseNestedCSS(node, options) {
303303 // `postcss@8` parse custom properties as `css-decl`
304304 if ( options . parser === "css" && node . type === "css-decl" ) {
305305 const { prop, value } = node ;
306- if (
307- prop . startsWith ( "--" ) &&
308- value . startsWith ( "{" ) &&
309- value . endsWith ( "}" )
310- ) {
311- const textBefore = options . originalText . slice (
312- 0 ,
313- node . source . start . offset
314- ) ;
315- const nodeText =
316- "a" . repeat ( prop . length ) +
317- options . originalText . slice (
318- node . source . start . offset + prop . length ,
319- node . source . end . offset + 1
306+ if ( prop . startsWith ( "--" ) && value . startsWith ( "{" ) ) {
307+ let rules ;
308+ if ( value . endsWith ( "}" ) ) {
309+ const textBefore = options . originalText . slice (
310+ 0 ,
311+ node . source . start . offset
320312 ) ;
321- const fakeContent = textBefore . replace ( / [ ^ \n ] / g, " " ) + nodeText ;
322- node . value = {
323- type : "css-rule" ,
324- nodes : parseCss ( fakeContent , [ ] , { ...options } ) . nodes [ 0 ] . nodes ,
325- } ;
313+ const nodeText =
314+ "a" . repeat ( prop . length ) +
315+ options . originalText . slice (
316+ node . source . start . offset + prop . length ,
317+ node . source . end . offset + 1
318+ ) ;
319+ const fakeContent = textBefore . replace ( / [ ^ \n ] / g, " " ) + nodeText ;
320+ let ast ;
321+ try {
322+ ast = parseCss ( fakeContent , [ ] , { ...options } ) ;
323+ } catch ( _ ) {
324+ // noop
325+ }
326+ if (
327+ ast &&
328+ ast . nodes &&
329+ ast . nodes . length === 1 &&
330+ ast . nodes [ 0 ] . type === "css-rule"
331+ ) {
332+ rules = ast . nodes [ 0 ] . nodes ;
333+ }
334+ }
335+ if ( rules ) {
336+ node . value = {
337+ type : "css-rule" ,
338+ nodes : rules ,
339+ } ;
340+ } else {
341+ node . value = {
342+ type : "value-unknown" ,
343+ value : node . raws . value . raw
344+ }
345+ }
326346 return node ;
327347 }
328348 }
0 commit comments