postcss-normalize-url
Advanced tools
Comparing version
@@ -0,1 +1,6 @@ | ||
# 1.2.0 | ||
* Fixes an issue where options could not be passed through. | ||
* Support for normalising URLs in `@namespace` rules. | ||
# 1.1.0 | ||
@@ -2,0 +7,0 @@ |
49
index.js
@@ -5,2 +5,3 @@ 'use strict'; | ||
var postcss = require('postcss'); | ||
var list = postcss.list; | ||
var shorter = require('./lib/shorter'); | ||
@@ -10,2 +11,3 @@ var normalize = require('normalize-url'); | ||
var path = require('path'); | ||
var assign = require('object-assign'); | ||
@@ -18,16 +20,32 @@ var multiline = /\\[\r\n]/; | ||
function convert (url, options) { | ||
if (isAbsolute(url)) { | ||
return normalize(url, options); | ||
} | ||
return path.normalize(url); | ||
} | ||
function namespaceOptimiser (options) { | ||
return function (rule) { | ||
rule.params = list.space(rule.params).map(function (param) { | ||
if (/^url/.test(param)) { | ||
param = param.replace(/^url\((.*)\)$/, '$1'); | ||
} | ||
return param.replace(/^("|')(.*)\1$/, function (_, quo, body) { | ||
return quo + convert(body.trim(), options) + quo; | ||
}); | ||
}).join(' '); | ||
} | ||
} | ||
module.exports = postcss.plugin('postcss-normalize-url', function (options) { | ||
options = { normalizeProtocol: false, stripFragment: false } || {}; | ||
options = assign({ | ||
normalizeProtocol: false, | ||
stripFragment: false | ||
}, options); | ||
var convert = function (url) { | ||
if (isAbsolute(url)) { | ||
return normalize(url, options); | ||
} | ||
return path.normalize(url); | ||
}; | ||
return function (css) { | ||
css.eachDecl(function (declaration) { | ||
declaration.value = declaration.value.replace(multiline, ''); | ||
eachFunction(declaration, 'url', function (substring) { | ||
css.eachDecl(function (decl) { | ||
decl.value = decl.value.replace(multiline, ''); | ||
decl.value = eachFunction(decl.value, 'url', function (substring) { | ||
// Don't mangle embedded base64 or svg images | ||
@@ -38,7 +56,7 @@ if (~substring.indexOf('data:image/')) { | ||
var url = substring.replace(extractUrl, '$1').trim(); | ||
url = url.replace(unquote, function (m, quote, body) { | ||
return quote + convert(body.trim()) + quote; | ||
url = url.replace(unquote, function (_, quote, body) { | ||
return quote + convert(body.trim(), options) + quote; | ||
}); | ||
var trimmed = url.replace(unquote, '$2').trim(); | ||
var optimised = convert(trimmed); | ||
var optimised = convert(trimmed, options); | ||
if (escapeChars.test(trimmed)) { | ||
@@ -48,3 +66,3 @@ var isEscaped = trimmed.replace(replaceEscapeChars, '\\$1'); | ||
} | ||
return substring.replace(extractUrl, function (m, pre, post) { | ||
return substring.replace(extractUrl, function (_, pre, post) { | ||
if (post) { | ||
@@ -57,3 +75,4 @@ return 'url(' + optimised + ')' + post; | ||
}); | ||
css.eachAtRule(namespaceOptimiser(options)); | ||
}; | ||
}); |
@@ -5,12 +5,13 @@ 'use strict'; | ||
module.exports = function eachFunction (decl, type, callback) { | ||
if (~decl.value.indexOf(type + '(')) { | ||
var locs = indexesOf(decl.value, type); | ||
locs.push(decl.value.length); | ||
module.exports = function eachFunction (value, type, callback) { | ||
if (~value.indexOf(type + '(')) { | ||
var locs = indexesOf(value, type); | ||
locs.push(value.length); | ||
while (locs.length > 1) { | ||
var sub = decl.value.substring(locs[0], locs[1]); | ||
decl.value = decl.value.replace(sub, callback.call(this, sub)); | ||
var sub = value.substring(locs[0], locs[1]); | ||
value = value.replace(sub, callback.call(this, sub)); | ||
locs.shift(); | ||
} | ||
} | ||
return value; | ||
}; |
{ | ||
"name": "postcss-normalize-url", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Normalize URLs with PostCSS", | ||
@@ -24,2 +24,3 @@ "main": "index.js", | ||
"normalize-url": "^1.2.0", | ||
"object-assign": "^2.0.0", | ||
"postcss": "^4.1.2" | ||
@@ -39,6 +40,3 @@ }, | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/ben-eb/postcss-normalize-url.git" | ||
} | ||
"repository": "ben-eb/postcss-normalize-url" | ||
} |
Sorry, the diff of this file is not supported yet
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
7079
10.99%84
29.23%5
25%1
Infinity%+ Added
+ Added