postcss-normalize-url
Advanced tools
Comparing version 2.1.1 to 2.1.2
@@ -0,1 +1,7 @@ | ||
# 2.1.2 | ||
* postcss-normalize-url now uses postcss-value-parser to iterate | ||
url values (thanks to @TrySound). | ||
* Fixed `@namespace` URL reduction behaviour. (thanks to @TrySound). | ||
# 2.1.1 | ||
@@ -2,0 +8,0 @@ |
102
index.js
'use strict'; | ||
var postcss = require('postcss'); | ||
var shorter = require('./lib/shorter'); | ||
var parser = require('postcss-value-parser'); | ||
var stringify = parser.stringify; | ||
var normalize = require('normalize-url'); | ||
@@ -10,6 +11,3 @@ var isAbsolute = require('is-absolute-url'); | ||
var cssList = require('css-list'); | ||
var multiline = /\\[\r\n]/; | ||
var unquote = /^("|')(.*)\1$/; | ||
var escapeChars = /([\s\(\)"'])/g; | ||
@@ -24,37 +22,76 @@ | ||
function trimSpaceNodes(nodes) { | ||
var first = nodes[0]; | ||
if (first && first.type === 'space') { | ||
nodes.shift(); | ||
} | ||
var last = nodes[nodes.length - 1]; | ||
if (last && last.type === 'space') { | ||
nodes.pop(); | ||
} | ||
} | ||
function namespaceOptimiser (options) { | ||
return function (rule) { | ||
rule.params = cssList.map(rule.params, function (param) { | ||
if (/^url/.test(param)) { | ||
param = param.replace(/^url\((.*)\)$/, '$1'); | ||
rule.params = parser(rule.params).walk(function (node, i, parentNodes) { | ||
var nodes = node.nodes; | ||
if (node.type === 'function' && node.value === 'url') { | ||
trimSpaceNodes(node.nodes); | ||
if (nodes.length === 1 && nodes[0].type === 'string' && nodes[0].quote) { | ||
node = nodes[0]; | ||
} else { | ||
node = { type: 'string', quote: '"', value: stringify(nodes) }; | ||
} | ||
parentNodes[i] = node; | ||
} | ||
return param.replace(/^("|')(.*)\1$/, function (_, quo, body) { | ||
return quo + convert(body.trim(), options) + quo; | ||
}); | ||
}); | ||
if (node.type === 'string') { | ||
node.value = convert(node.value.trim(), options); | ||
} | ||
return false; | ||
}).toString(); | ||
}; | ||
} | ||
function eachValue (val, options) { | ||
return cssList.map(val, function (value, type) { | ||
if ( | ||
type !== 'func' || | ||
value.indexOf('url') !== 0 || | ||
~value.indexOf('data:image/') || | ||
~value.indexOf('data:application/') | ||
) { | ||
return value; | ||
function transformDecl(decl, opts) { | ||
decl.value = decl.value.replace(multiline, ''); | ||
decl.value = parser(decl.value).walk('url', function (node) { | ||
var nodes = node.nodes; | ||
var url; | ||
var escaped; | ||
if (node.type !== 'function') { | ||
return; | ||
} | ||
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); | ||
trimSpaceNodes(nodes); | ||
if (nodes.length === 1 && nodes[0].type === 'string' && nodes[0].quote) { | ||
url = nodes[0]; | ||
} else { | ||
url = { type: 'word', value: stringify(nodes) }; | ||
} | ||
return 'url(' + optimised + ')'; | ||
}); | ||
node.nodes = [url]; | ||
if (~url.value.indexOf('data:image/') || ~url.value.indexOf('data:application/')) { | ||
return false; | ||
} | ||
url.value = url.value.trim() | ||
url.value = convert(url.value, opts); | ||
if (escapeChars.test(url.value)) { | ||
escaped = url.value.replace(escapeChars, '\\$1'); | ||
if (escaped.length < url.value.length + (url.type === 'string' ? 2 : 0)) { | ||
url.value = escaped; | ||
url.type = 'word'; | ||
} | ||
} else { | ||
url.type = 'word'; | ||
} | ||
return false; | ||
}).toString(); | ||
} | ||
@@ -72,4 +109,3 @@ | ||
if (node.type === 'decl') { | ||
node.value = eachValue(node.value.replace(multiline, ''), opts); | ||
return; | ||
return transformDecl(node, opts); | ||
} | ||
@@ -76,0 +112,0 @@ if (node.type === 'atrule' && node.name === 'namespace') { |
{ | ||
"name": "postcss-normalize-url", | ||
"version": "2.1.1", | ||
"version": "2.1.2", | ||
"description": "Normalize URLs with PostCSS", | ||
"main": "index.js", | ||
"files": [ | ||
"index.js", | ||
"LICENSE-MIT" | ||
], | ||
"scripts": { | ||
@@ -21,3 +25,3 @@ "lint": "jshint index.js lib/*.js --reporter node_modules/jshint-stylish/stylish.js", | ||
"dependencies": { | ||
"css-list": "^0.1.2", | ||
"postcss-value-parser": "^1.2.2", | ||
"is-absolute-url": "^2.0.0", | ||
@@ -24,0 +28,0 @@ "normalize-url": "^1.3.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8220
96
5
+ Addedpostcss-value-parser@^1.2.2
+ Addedpostcss-value-parser@1.4.2(transitive)
- Removedcss-list@^0.1.2
- Removedcss-list@0.1.3(transitive)