Skip to content

Commit e68d85c

Browse files
committed
fix #84
import callumlocke/css-url-rewriter#7 utils.js
1 parent 3cdba66 commit e68d85c

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

lib/plugins/url-rewriter.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
var resolve = require('url').resolve;
2-
var rewriteCSSURLs = require('css-url-rewriter');
3-
42
var utils = require('../utils');
53

64
module.exports = function (prefix, options) {
@@ -23,7 +21,7 @@ module.exports = function (prefix, options) {
2321
var branch = file.branch;
2422

2523
// rewrite URLs
26-
file.string = rewriteCSSURLs(string, function (uri) {
24+
file.string = utils.rewriteCSSURLs(string, function (uri) {
2725
if (isData(uri)) return uri;
2826
if (isAbsolute(uri)) return uri;
2927
if (isFragment(uri)) return uri;

lib/utils.js

+54
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,57 @@ exports.isGeneratorFunction = function (obj) {
7676
&& obj.constructor
7777
&& 'GeneratorFunction' === obj.constructor.name;
7878
}
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 = /@import[^;]*|[;\s]?\*?[a-zA-Z\-]+\s*\:\#?[^;}]*url\(\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 = /url\(\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+
};

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"component-manifest": "^1.0.0",
1818
"component-require2": "^1.0.1",
1919
"cp": "~0.1.1",
20-
"css-url-rewriter": "^0.1.0",
2120
"debug": "*",
2221
"generator-supported": "~0.0.1",
2322
"graceful-fs": "^2.0.1",

0 commit comments

Comments
 (0)