44 */
55var loaderUtils = require ( 'loader-utils' ) ;
66
7+ var REGEX_STYLE = / < s t y l e [ \s \S ] * ?> [ \s \S ] * ?< \/ s t y l e > / i
8+ var REGEX_DECLARATION = / ^ \s * < \? x m l [ ^ > ] * > \s * / i
9+
10+ var REGEX_DOUBLE_QUOTE = / " / g
11+ var REGEX_MULTIPLE_SPACES = / \s + / g
12+ var REGEX_UNSAFE_CHARS = / [ { } \| \\ \^ ~ \[ \] ` " < > # % ] / g
13+
714module . exports = function ( content ) {
815this . cacheable && this . cacheable ( ) ;
916
@@ -15,10 +22,10 @@ module.exports = function(content) {
1522if ( limit <= 0 || content . length < limit ) {
1623var newContent = content . toString ( 'utf8' ) ;
1724
18- var hasStyleElement = / < s t y l e [ \s \S ] * ? > [ \s \S ] * ? < \/ s t y l e > / i . test ( newContent )
25+ var hasStyleElement = REGEX_STYLE . test ( newContent )
1926
2027if ( query . stripdeclarations ) {
21- newContent = newContent . replace ( / ^ \s * < \? x m l [ ^ > ] * > \s * / i , "" ) ;
28+ newContent = newContent . replace ( REGEX_DECLARATION , "" ) ;
2229}
2330
2431var data ;
@@ -28,9 +35,9 @@ module.exports = function(content) {
2835}
2936data = "data:image/svg+xml;base64," + newContent . toString ( "base64" ) ;
3037} else {
31- newContent = newContent . replace ( / " / g , "'" ) ;
32- newContent = newContent . replace ( / \s + / g , " " ) ;
33- newContent = newContent . replace ( / [ { } \| \\ \^ ~ \[ \] ` " < > # % ] / g , function ( match ) {
38+ newContent = newContent . replace ( REGEX_DOUBLE_QUOTE , "'" ) ;
39+ newContent = newContent . replace ( REGEX_MULTIPLE_SPACES , " " ) ;
40+ newContent = newContent . replace ( REGEX_UNSAFE_CHARS , function ( match ) {
3441return '%' + match [ 0 ] . charCodeAt ( 0 ) . toString ( 16 ) . toUpperCase ( ) ;
3542} ) ;
3643
0 commit comments