@@ -76,3 +76,57 @@ exports.isGeneratorFunction = function (obj) {
76
76
&& obj . constructor
77
77
&& 'GeneratorFunction' === obj . constructor . name ;
78
78
}
79
+
80
+
81
+ /*
82
+ * css-url-rewriter
83
+ * https://github.com/callumlocke/css-url-rewriter
84
+ *
85
+ * Copyright (c) 2014 Callum Locke
86
+ * Licensed under the MIT license.
87
+ */
88
+
89
+ // Regex to find CSS properties that contain URLs
90
+ // Fiddle: http://refiddle.com/refiddles/css-url-matcher
91
+ // Railroad: http://goo.gl/LXpk52
92
+ var cssPropertyMatcher = / @ i m p o r t [ ^ ; ] * | [ ; \s ] ? \* ? [ a - z A - Z \- ] + \s * \: \# ? [ ^ ; } ] * u r l \( \s * [ ' " ] ? [ ^ ' " \) \s ] + [ ' " ] ? \s * \) [ ^ ; } ] * / g;
93
+
94
+ // Regex to find the URLs within a CSS property value
95
+ // Fiddle: http://refiddle.com/refiddles/match-multiple-urls-within-a-css-property-value
96
+ // Railroad: http://goo.gl/vQzMcg
97
+ var urlMatcher = / u r l \( \s * [ ' " ] ? ( [ ^ ) ' " ] + ) [ ' " ] ? \s * \) / g;
98
+
99
+ var defaults = {
100
+ excludeProperties : [ 'behavior' , '*behavior' ]
101
+ } ;
102
+
103
+ exports . rewriteCSSURLs = function rewriteCSSURLs ( css , settings , rewriterFn ) {
104
+ // Normalise arguments and settings
105
+ if ( typeof settings === 'function' ) {
106
+ rewriterFn = settings ;
107
+ settings = defaults ;
108
+ }
109
+
110
+ // Return the modified CSS
111
+ var result = css . toString ( ) . replace ( cssPropertyMatcher , function ( property ) {
112
+ // This function deals with an individual CSS property.
113
+
114
+ // If this property is excluded, return it unchanged
115
+ if ( settings . excludeProperties . length ) {
116
+ var propertyName = property . split ( ':' ) [ 0 ] . replace ( / ^ \s + | \s + $ / g, '' ) ;
117
+
118
+ for ( var i = settings . excludeProperties . length - 1 ; i >= 0 ; i -- ) {
119
+ if ( propertyName . indexOf ( settings . excludeProperties [ i ] ) === 0 ) {
120
+ return property ;
121
+ }
122
+ }
123
+ }
124
+
125
+ // Return the property with the URL rewritten
126
+ return property . replace ( urlMatcher , function ( urlFunc , justURL ) {
127
+ return urlFunc . replace ( justURL , rewriterFn ( justURL ) ) ;
128
+ } ) ;
129
+ } ) ;
130
+
131
+ return result ;
132
+ } ;
0 commit comments