11import { FROM_CSS } from './sections-util' ;
2+ import { RX_META } from './util' ;
23/* global parserlib */
34
45/**
@@ -13,6 +14,7 @@ import {FROM_CSS} from './sections-util';
1314 * @property {?number } lastStyleId
1415 */
1516export default function extractSections ( { code, styleId, fast = true } ) {
17+ const commentsAtEnd = / ( \/ \* (?: [ ^ * ] + | \* (? ! \/ ) ) * \* \/ \s * ) * $ / ;
1618 const hasSingleEscapes = / ( [ ^ \\ ] | ^ ) \\ ( [ ^ \\ ] | $ ) / ;
1719 const parser = new parserlib . css . Parser ( {
1820 noValidation : true ,
@@ -31,7 +33,7 @@ export default function extractSections({code, styleId, fast = true}) {
3133 parser . addListener ( 'startdocument' , e => {
3234 const lastSection = sectionStack [ sectionStack . length - 1 ] ;
3335 let outerText = mozStyle . slice ( lastSection . start , e . offset ) ;
34- const lastCmt = getLastComment ( outerText ) ;
36+ const lastCmt = outerText . match ( commentsAtEnd ) [ 0 ] ;
3537 const section = {
3638 code : '' ,
3739 start : e . brace . offset + 1 ,
@@ -90,7 +92,7 @@ export default function extractSections({code, styleId, fast = true}) {
9092 } ) ;
9193
9294 try {
93- parser . parse ( code , {
95+ parser . parse ( code . replace ( RX_META , '' ) , {
9496 reuseCache : ! extractSections . lastStyleId || styleId === extractSections . lastStyleId ,
9597 } ) ;
9698 } catch ( e ) {
@@ -124,27 +126,4 @@ export default function extractSections({code, styleId, fast = true}) {
124126 }
125127 sections . push ( Object . assign ( { } , section ) ) ;
126128 }
127-
128- function getLastComment ( text ) {
129- let open = text . length ;
130- let close ;
131- while ( open ) {
132- // at this point we're guaranteed to be outside of a comment
133- close = text . lastIndexOf ( '*/' , open - 2 ) ;
134- if ( close < 0 ) {
135- break ;
136- }
137- // stop if a non-whitespace precedes and return what we currently have
138- const tailEmpty = ! text . substring ( close + 2 , open ) . trim ( ) ;
139- if ( ! tailEmpty ) {
140- break ;
141- }
142- // find a closed preceding comment
143- const prevClose = text . lastIndexOf ( '*/' , close - 2 ) ;
144- // then find the real start of current comment
145- // e.g. /* preceding */ /* current /* current /* current */
146- open = text . indexOf ( '/*' , prevClose < 0 ? 0 : prevClose + 2 ) ;
147- }
148- return open ? text . slice ( open ) : text ;
149- }
150129}
0 commit comments