@@ -146,54 +146,65 @@ module.exports = function (bannerWidth, libraryPaths) {
146146 function reworkPlugin ( stylesheet ) {
147147
148148 // visit each node (selector) in the stylesheet recursively using the official utility method
149- visit ( stylesheet , function ( declarations ) {
150-
151- // each node may have multiple declarations
152- declarations . forEach ( function ( declaration ) {
153-
154- // reverse the original source-map to find the original sass file
155- var cssStart = declaration . position . start ;
156- var sassStart = sourceMapConsumer . originalPositionFor ( {
157- line : cssStart . line ,
158- column : cssStart . column
159- } ) ;
160- if ( ! sassStart . source ) {
161- throw new Error ( 'failed to decode node-sass source map' ) ; // this can occur with regressions in libsass
149+ // each node may have multiple declarations
150+ visit ( stylesheet , function visitor ( declarations ) {
151+ declarations
152+ . forEach ( eachDeclaration ) ;
153+ } ) ;
154+
155+ /**
156+ * Process a declaration from the syntax tree.
157+ * @param declaration
158+ */
159+ function eachDeclaration ( declaration ) {
160+ var URL_STATEMENT_REGEX = / ( u r l \s * \( ) \s * (?: ( [ ' " ] ) ( (?: (? ! \2) .) * ) ( \2) | ( [ ^ ' " ] (?: (? ! \) ) .) * [ ^ ' " ] ) ) \s * ( \) ) / g;
161+
162+ // reverse the original source-map to find the original sass file
163+ var cssStart = declaration . position . start ;
164+ var sassStart = sourceMapConsumer . originalPositionFor ( {
165+ line : cssStart . line ,
166+ column : cssStart . column
167+ } ) ;
168+ if ( ! sassStart . source ) {
169+ throw new Error ( 'failed to decode node-sass source map' ) ; // this can occur with regressions in libsass
170+ }
171+ var sassDir = path . dirname ( sassStart . source ) ;
172+
173+ // allow multiple url() values in the declaration
174+ // split by url statements and process the content
175+ // additional capture groups are needed to match quotations correctly
176+ // escaped quotations are not considered
177+ declaration . value = declaration . value
178+ . split ( URL_STATEMENT_REGEX )
179+ . map ( eachSplitOrGroup )
180+ . join ( '' ) ;
181+
182+ /**
183+ * Encode the content portion of <code>url()</code> statements.
184+ * There are 4 capture groups in the split making every 5th unmatched.
185+ * @param {string } token A single split item
186+ * @param i The index of the item in the split
187+ * @returns {string } Every 3 or 5 items is an encoded url everything else is as is
188+ */
189+ function eachSplitOrGroup ( token , i ) {
190+
191+ // we can get groups as undefined under certain match circumstances
192+ var initialised = token || '' ;
193+
194+ // the content of the url() statement is either in group 3 or group 5
195+ var mod = i % 7 ;
196+ if ( ( mod === 3 ) || ( mod === 5 ) ) {
197+
198+ // remove query string or hash suffix
199+ var uri = initialised . split ( / [ ? # ] / g) . shift ( ) ;
200+ return uri && encodeRelativeURL ( sassDir , uri ) || initialised ;
162201 }
163- var sassDir = path . dirname ( sassStart . source ) ;
164-
165- // allow multiple url() values in the declaration
166- // split by url statements and process the content
167- // additional capture groups are needed to match quotations correctly
168- // escaped quotations are not considered
169- declaration . value = declaration . value
170- . split ( / ( u r l \s * \( \s * ) ( [ ' " ] ? ) ( (?: (? ! \2| \? | # ] ) .) * (?: (? ! \2) .) * ) ( \2\s * \) ) / g)
171- . map ( eachSplitOrGroup )
172- . join ( '' ) ;
173-
174- /**
175- * Encode the content portion of <code>url()</code> statements.
176- * There are 4 capture groups in the split making every 5th unmatched.
177- * @param {string } token A single split item
178- * @param i The index of the item in the split
179- * @returns {string } Every 3 or 5 items is an encoded url everything else is as is
180- */
181- function eachSplitOrGroup ( token , i ) {
182-
183- // the quoted or unquoted content of the url() statement
184- if ( i % 5 === 3 ) {
185-
186- // remove query string or hash suffix
187- var uri = token . split ( / [ ? # ] / g) . shift ( ) ;
188- return encodeRelativeURL ( sassDir , uri ) || token ;
189- }
190- // everything else, including parentheses and quotation (where present) and media statements
191- else {
192- return token ;
193- }
202+ // everything else, including parentheses and quotation (where present) and media statements
203+ else {
204+ return initialised ;
194205 }
195- } ) ;
196- } ) ;
206+ }
207+ }
197208 }
198209
199210 /**
0 commit comments