postcss-normalize-url
Advanced tools
Comparing version
@@ -0,1 +1,6 @@ | ||
# 2.0.0 | ||
* Improved URL detection when using two or more in the same declaration. | ||
* node 0.10 is no longer supported. | ||
# 1.2.1 | ||
@@ -2,0 +7,0 @@ |
54
index.js
'use strict'; | ||
var eachFunction = require('./lib/eachFunction'); | ||
var postcss = require('postcss'); | ||
var list = postcss.list; | ||
var space = postcss.list.space; | ||
var shorter = require('./lib/shorter'); | ||
@@ -12,7 +11,7 @@ var normalize = require('normalize-url'); | ||
var cssList = require('css-list'); | ||
var multiline = /\\[\r\n]/; | ||
var extractUrl = /^url\(([\s\S]*)\)(.*)?$/; | ||
var unquote = /^("|')(.*)\1$/; | ||
var escapeChars = /[\s\(\)'"]/; | ||
var replaceEscapeChars = /([\s\(\)"'])/g; | ||
var escapeChars = /([\s\(\)"'])/g; | ||
@@ -28,3 +27,3 @@ function convert (url, options) { | ||
return function (rule) { | ||
rule.params = list.space(rule.params).map(function (param) { | ||
rule.params = space(rule.params).map(function (param) { | ||
if (/^url/.test(param)) { | ||
@@ -40,2 +39,21 @@ param = param.replace(/^url\((.*)\)$/, '$1'); | ||
function eachValue (val, options) { | ||
return cssList.map(val, [' ', '\n', '\t', ',', '/'], function (value, type) { | ||
if (type !== 'func' || value.indexOf('url') !== 0 || ~value.indexOf('data:image/')) { | ||
return value; | ||
} | ||
var url = value.substring(4, value.length - 1).trim(); | ||
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, options); | ||
if (escapeChars.test(trimmed)) { | ||
var isEscaped = trimmed.replace(escapeChars, '\\$1'); | ||
optimised = shorter(isEscaped, url); | ||
} | ||
return 'url(' + optimised + ')'; | ||
}); | ||
} | ||
module.exports = postcss.plugin('postcss-normalize-url', function (options) { | ||
@@ -49,25 +67,3 @@ options = assign({ | ||
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 | ||
if (~substring.indexOf('data:image/')) { | ||
return substring; | ||
} | ||
var url = substring.replace(extractUrl, '$1').trim(); | ||
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, options); | ||
if (escapeChars.test(trimmed)) { | ||
var isEscaped = trimmed.replace(replaceEscapeChars, '\\$1'); | ||
optimised = shorter(isEscaped, url); | ||
} | ||
return substring.replace(extractUrl, function (_, pre, post) { | ||
if (post) { | ||
return 'url(' + optimised + ')' + post; | ||
} | ||
return 'url(' + optimised + ')'; | ||
}); | ||
}); | ||
decl.value = eachValue(decl.value.replace(multiline, ''), options); | ||
}); | ||
@@ -74,0 +70,0 @@ css.eachAtRule('namespace', namespaceOptimiser(options)); |
{ | ||
"name": "postcss-normalize-url", | ||
"version": "1.2.1", | ||
"version": "2.0.0", | ||
"description": "Normalize URLs with PostCSS", | ||
@@ -21,7 +21,7 @@ "main": "index.js", | ||
"dependencies": { | ||
"indexes-of": "^1.0.1", | ||
"css-list": "0.0.3", | ||
"is-absolute-url": "^1.0.0", | ||
"normalize-url": "^1.2.0", | ||
"object-assign": "^2.0.0", | ||
"postcss": "^4.1.2" | ||
"postcss": "^4.1.9" | ||
}, | ||
@@ -28,0 +28,0 @@ "devDependencies": { |
6428
-10.32%7
-12.5%64
-23.81%+ Added
+ Added
- Removed
- Removed
Updated