postcss-url
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -0,1 +1,5 @@ | ||
# 2.1.1 - 2015-03-31 | ||
- Fixed: whitespace before and after url() value are now supported and preserved | ||
# 2.1.0 - 2015-03-12 | ||
@@ -2,0 +6,0 @@ |
216
index.js
@@ -14,3 +14,4 @@ /** | ||
* | ||
* @param {Object} options | ||
* @param {Object} options plugin options | ||
* @return {void} | ||
*/ | ||
@@ -26,7 +27,3 @@ module.exports = function fixUrl(options) { | ||
styles.eachDecl(function(decl) { | ||
if (!decl.value) { | ||
return | ||
} | ||
if (decl.value.indexOf("url(") > -1) { | ||
if (decl.value && decl.value.indexOf("url(") > -1) { | ||
processDecl(decl, from, to, mode, options) | ||
@@ -39,9 +36,52 @@ } | ||
/** | ||
* return quote type | ||
* | ||
* @param {String} string quoted (or not) value | ||
* @return {String} quote if any, or empty string | ||
*/ | ||
function getUrlMetaData(string) { | ||
var quote = "" | ||
var quotes = ["\"", "'"] | ||
var trimedString = string.trim() | ||
quotes.forEach(function(q) { | ||
if (trimedString.charAt(0) === q && trimedString.charAt(trimedString.length - 1) === q) { | ||
quote = q | ||
} | ||
}) | ||
var urlMeta = { | ||
before: string.slice(0, string.indexOf(quote)), | ||
quote: quote, | ||
value: quote ? trimedString.substr(1, trimedString.length - 2) : trimedString, | ||
after: string.slice(string.lastIndexOf(quote) + 1), | ||
} | ||
return urlMeta | ||
} | ||
/** | ||
* Create an css url() from a path and a quote style | ||
* | ||
* @param {String} urlMeta url meta data | ||
* @param {String} newPath url path | ||
* @return {String} new url() | ||
*/ | ||
function createUrl(urlMeta, newPath) { | ||
return "url(" + | ||
urlMeta.before + | ||
urlMeta.quote + | ||
(newPath || urlMeta.value) + | ||
urlMeta.quote + | ||
urlMeta.after + | ||
")" | ||
} | ||
/** | ||
* Processes one declaration | ||
* | ||
* @param {Object} decl | ||
* @param {String} from | ||
* @param {String} to | ||
* @param {String|Function} mode | ||
* @param {Object} options | ||
* @param {Object} decl postcss declaration | ||
* @param {String} from source | ||
* @param {String} to destination | ||
* @param {String|Function} mode plugin mode | ||
* @param {Object} options plugin options | ||
* @return {void} | ||
*/ | ||
@@ -51,31 +91,24 @@ function processDecl(decl, from, to, mode, options) { | ||
decl.value = reduceFunctionCall(decl.value, "url", function(value) { | ||
// save quote style | ||
var quote = getQuote(value) | ||
value = unquote(value, quote) | ||
var urlMeta = getUrlMetaData(value) | ||
if (typeof mode === "function") { | ||
return processCustom(quote, value, mode, decl) | ||
return processCustom(urlMeta, mode, decl) | ||
} | ||
// ignore absolute urls, hasshes or data uris | ||
if (value.indexOf("/") === 0 || | ||
value.indexOf("data:") === 0 || | ||
value.indexOf("#") === 0 || | ||
/^[a-z]+:\/\//.test(value) | ||
if (urlMeta.value.indexOf("/") === 0 || | ||
urlMeta.value.indexOf("data:") === 0 || | ||
urlMeta.value.indexOf("#") === 0 || | ||
/^[a-z]+:\/\//.test(urlMeta.value) | ||
) { | ||
return createUrl(quote, value) | ||
return createUrl(urlMeta) | ||
} | ||
var newPath = value | ||
switch (mode) { | ||
case "rebase": | ||
return processRebase(from, dirname, newPath, quote, to) | ||
break | ||
return processRebase(from, dirname, urlMeta, to) | ||
case "inline": | ||
return processInline(from, dirname, newPath, quote, value, options) | ||
break | ||
return processInline(from, dirname, urlMeta, options) | ||
default: | ||
throw new Error("Unknow mode for postcss-url: " + mode) | ||
break | ||
} | ||
@@ -85,14 +118,13 @@ }) | ||
/** | ||
* Transform url() based on a custom callback | ||
* | ||
* @param {String} quote | ||
* @param {String} value | ||
* @param {Function} cb | ||
* @param {Object} decl | ||
* @param {String} urlMeta url meta datayy | ||
* @param {Function} cb callback to execute | ||
* @param {Object} decl postcss declaration | ||
* @return {void} | ||
*/ | ||
function processCustom(quote, value, cb, decl) { | ||
var newValue = cb(value, decl) | ||
return createUrl(quote, newValue) | ||
function processCustom(urlMeta, cb, decl) { | ||
var newValue = cb(urlMeta.value, decl) | ||
return createUrl(urlMeta, newValue) | ||
} | ||
@@ -104,9 +136,10 @@ | ||
* | ||
* @param {String} from | ||
* @param {String} dirname | ||
* @param {String} newPath | ||
* @param {String} quote | ||
* @param {String} to | ||
* @param {String} from from | ||
* @param {String} dirname to dirname | ||
* @param {String} urlMeta url meta datayy | ||
* @param {String} to destination | ||
* @return {String} new url | ||
*/ | ||
function processRebase(from, dirname, newPath, quote, to) { | ||
function processRebase(from, dirname, urlMeta, to) { | ||
var newPath = urlMeta.value | ||
if (dirname !== from) { | ||
@@ -117,6 +150,6 @@ newPath = path.relative(from, dirname + path.sep + newPath) | ||
newPath = path.relative(to, newPath) | ||
if (path.sep == "\\") { | ||
if (path.sep === "\\") { | ||
newPath = newPath.replace(/\\/g, "\/") | ||
} | ||
return createUrl(quote, newPath) | ||
return createUrl(urlMeta, newPath) | ||
} | ||
@@ -127,87 +160,54 @@ | ||
* | ||
* @param {String} from | ||
* @param {String} dirname | ||
* @param {String} newPath | ||
* @param {String} quote | ||
* @param {String} value | ||
* @param {Object} options | ||
* @param {String} from from | ||
* @param {String} dirname to dirname | ||
* @param {String} urlMeta url meta data | ||
* @param {Object} options plugin options | ||
* @return {String} new url | ||
*/ | ||
function processInline(from, dirname, newPath, quote, value, options) { | ||
var maxSize = typeof(options.maxSize) == "undefined" ? 14 : options.maxSize | ||
var basePath = options.basePath; | ||
var fullFilePath; | ||
maxSize *= 1024; | ||
function processInline(from, dirname, urlMeta, options) { | ||
var maxSize = options.maxSize === undefined ? 14 : options.maxSize | ||
var basePath = options.basePath | ||
var fullFilePath | ||
maxSize *= 1024 | ||
// ignore URLs with hashes/fragments, they can't be inlined | ||
var link = url.parse(value) | ||
var link = url.parse(urlMeta.value) | ||
if (link.hash) { | ||
return createUrl(quote, value) | ||
return createUrl(urlMeta, urlMeta.value) | ||
} | ||
if (basePath) { | ||
fullFilePath = path.join(basePath, value) | ||
fullFilePath = path.join(basePath, urlMeta.value) | ||
} | ||
else { | ||
fullFilePath = dirname !== from ? dirname + path.sep + value : value | ||
fullFilePath = dirname !== from ? dirname + path.sep + urlMeta.value : urlMeta.value | ||
} | ||
var file = path.resolve(from, fullFilePath) | ||
if (!fs.existsSync(file)) { | ||
console.warn("Can't read file '" + file + "', ignoring") | ||
return createUrl(urlMeta) | ||
} | ||
else { | ||
var mimeType = mime.lookup(file) | ||
var stats = fs.statSync(file) | ||
if (stats.size >= maxSize) { | ||
return createUrl(quote, newPath) | ||
} | ||
if (!mimeType) { | ||
console.warn("Unable to find asset mime-type for " + file) | ||
} | ||
else if (mimeType === "image/svg+xml") { | ||
var svg = new SvgEncoder(file) | ||
newPath = svg.encode() | ||
} | ||
else { | ||
file = fs.readFileSync(file) | ||
newPath = "data:" + mimeType + ";base64," + file.toString("base64") | ||
} | ||
var mimeType = mime.lookup(file) | ||
var stats = fs.statSync(file) | ||
if (stats.size >= maxSize) { | ||
return createUrl(urlMeta) | ||
} | ||
return createUrl(quote, newPath) | ||
} | ||
function createUrl(quote, newPath) { | ||
return "url(" + quote + newPath + quote + ")" | ||
} | ||
if (!mimeType) { | ||
console.warn("Unable to find asset mime-type for " + file) | ||
return createUrl(urlMeta) | ||
} | ||
/** | ||
* remove quote around a string | ||
* | ||
* @param {String} string | ||
* @param {String} quote | ||
* @return {String} unquoted string | ||
*/ | ||
function unquote(string, quote) { | ||
if (quote) { | ||
return string.substr(1, string.length - 2) | ||
if (mimeType === "image/svg+xml") { | ||
var svg = new SvgEncoder(file) | ||
return createUrl(urlMeta, svg.encode()) | ||
} | ||
return string | ||
// else | ||
file = fs.readFileSync(file) | ||
return createUrl(urlMeta, "data:" + mimeType + ";base64," + file.toString("base64")) | ||
} | ||
/** | ||
* return quote type | ||
* | ||
* @param {String} string quoted (or not) value | ||
* @return {String} quote if any, or empty string | ||
*/ | ||
function getQuote(string) { | ||
var quote = "" | ||
Array("\"", "'").forEach(function(q) { | ||
if (string.charAt(0) === q && string.charAt(string.length - 1) === q) { | ||
quote = q | ||
} | ||
}) | ||
return quote | ||
} |
{ | ||
"name": "postcss-url", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "PostCSS plugin to rebase or inline on url().", | ||
@@ -33,4 +33,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"jscs": "^1.6.2", | ||
"jshint": "^2.5.6", | ||
"eslint": "^0.18.0", | ||
"postcss": "^4.0.2", | ||
@@ -41,6 +40,4 @@ "postcss-import": "^5.0.0", | ||
"scripts": { | ||
"jscs": "jscs *.js **/*.js", | ||
"jshint": "jshint . --exclude-path .gitignore", | ||
"test": "npm run jscs && npm run jshint && tape test" | ||
"test": "eslint . && tape test" | ||
} | ||
} |
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
11547
4
182