postcss-url
Advanced tools
Comparing version 5.0.1 to 5.0.2
@@ -0,1 +1,7 @@ | ||
# 5.0.2 - 2015-10-12 | ||
- Fixed: now rebase url in old Internet Explorer filter | ||
`progid:DXImageTransform.Microsoft.AlphaImageLoader()` | ||
([#55](https://github.com/postcss/postcss-url/pull/55)) | ||
# 5.0.1 - 2015-10-04 | ||
@@ -2,0 +8,0 @@ |
259
index.js
@@ -11,6 +11,20 @@ /** | ||
var SvgEncoder = require("directory-encoder/lib/svg-uri-encoder.js") | ||
var reduceFunctionCall = require("reduce-function-call") | ||
var mkdirp = require("mkdirp") | ||
var crypto = require("crypto") | ||
var pathIsAbsolute = require("path-is-absolute") | ||
/** | ||
* @typedef UrlRegExp | ||
* @name UrlRegExp | ||
* @desc A regex for match url with parentheses: | ||
* (before url)(the url)(after url). | ||
* (the url) will be replace with new url, and before and after will remain | ||
* @type RegExp | ||
*/ | ||
/** | ||
* @type {UrlRegExp[]} | ||
*/ | ||
var UrlsPatterns = [ | ||
/(url\(\s*['"]?)([^"')]+)(["']?\s*\))/g, | ||
/(AlphaImageLoader\(\s*src=['"]?)([^"')]+)(["'])/g, | ||
] | ||
@@ -28,2 +42,4 @@ /** | ||
var mode = options.url !== undefined ? options.url : "rebase" | ||
var isCustom = typeof mode === "function" | ||
var callback = isCustom ? getCustomProcessor(mode) : getUrlProcessor(mode) | ||
@@ -38,7 +54,5 @@ return function(styles, result) { | ||
styles.walkDecls(function(decl) { | ||
if (decl.value && decl.value.indexOf("url(") > -1) { | ||
processDecl(result, decl, from, to, mode, options) | ||
} | ||
}) | ||
var cb = getDeclProcessor(result, from, to, callback, options, isCustom) | ||
styles.walkDecls(cb) | ||
} | ||
@@ -49,115 +63,94 @@ } | ||
/** | ||
* return quote type | ||
* | ||
* @param {String} string quoted (or not) value | ||
* @return {String} quote if any, or empty string | ||
* @callback PostcssUrl~UrlProcessor | ||
* @param {String} from from | ||
* @param {String} dirname to dirname | ||
* @param {String} oldUrl url | ||
* @param {String} to destination | ||
* @param {Object} options plugin options | ||
* @param {Object} decl postcss declaration | ||
* @return {String|undefined} new url or undefined if url is old | ||
*/ | ||
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), | ||
/** | ||
* @param {String} mode | ||
* @returns {PostcssUrl~UrlProcessor} | ||
*/ | ||
function getUrlProcessor(mode) { | ||
switch (mode) { | ||
case "rebase": | ||
return processRebase | ||
case "inline": | ||
return processInline | ||
case "copy": | ||
return processCopy | ||
default: | ||
throw new Error("Unknow mode for postcss-url: " + mode) | ||
} | ||
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() | ||
* @callback PostcssUrl~DeclProcessor | ||
* @param {Object} decl declaration | ||
*/ | ||
function createUrl(urlMeta, newPath) { | ||
return "url(" + | ||
urlMeta.before + | ||
urlMeta.quote + | ||
(newPath || urlMeta.value) + | ||
urlMeta.quote + | ||
urlMeta.after + | ||
")" | ||
} | ||
/** | ||
* Processes one declaration | ||
* | ||
* @param {Object} decl postcss declaration | ||
* @param {String} from source | ||
* @param {Object} result | ||
* @param {String} from from | ||
* @param {String} to destination | ||
* @param {String|Function} mode plugin mode | ||
* @param {Object} options plugin options | ||
* @return {void} | ||
* @param {PostcssUrl~UrlProcessor} callback | ||
* @param {Object} options | ||
* @param {Boolean} [isCustom] | ||
* @returns {PostcssUrl~DeclProcessor} | ||
*/ | ||
function processDecl(result, decl, from, to, mode, options) { | ||
var dirname = decl.source && decl.source.input | ||
? path.dirname(decl.source.input.file) | ||
: process.cwd() | ||
decl.value = reduceFunctionCall(decl.value, "url", function(value) { | ||
var urlMeta = getUrlMetaData(value) | ||
function getDeclProcessor(result, from, to, cb, options, isCustom) { | ||
var valueCallback = function(decl, oldUrl) { | ||
var dirname = decl.source && decl.source.input | ||
? path.dirname(decl.source.input.file) | ||
: process.cwd() | ||
if (typeof mode === "function") { | ||
return processCustom( | ||
result, | ||
mode, | ||
from, | ||
dirname, | ||
urlMeta, | ||
to, | ||
options, | ||
decl | ||
) | ||
} | ||
var newUrl | ||
// ignore absolute urls, hasshes or data uris | ||
if (urlMeta.value.indexOf("/") === 0 || | ||
urlMeta.value.indexOf("data:") === 0 || | ||
urlMeta.value.indexOf("#") === 0 || | ||
/^[a-z]+:\/\//.test(urlMeta.value) | ||
) { | ||
return createUrl(urlMeta) | ||
if (isCustom || ! isUrlShouldBeIgnored(oldUrl)) { | ||
newUrl = cb(result, from, dirname, oldUrl, to, options, decl) | ||
} | ||
switch (mode) { | ||
case "rebase": | ||
return processRebase(result, from, dirname, urlMeta, to) | ||
case "inline": | ||
return processInline(result, from, dirname, urlMeta, to, options, decl) | ||
case "copy": | ||
return processCopy(result, from, dirname, urlMeta, to, options, decl) | ||
default: | ||
throw new Error("Unknow mode for postcss-url: " + mode) | ||
} | ||
}) | ||
return newUrl || oldUrl | ||
} | ||
return function(decl) { | ||
UrlsPatterns.some(function(pattern) { | ||
if (pattern.test(decl.value)) { | ||
decl.value = decl.value | ||
.replace(pattern, function(_, beforeUrl, oldUrl, afterUrl) { | ||
return beforeUrl + valueCallback(decl, oldUrl) + afterUrl | ||
}) | ||
return true | ||
} | ||
}) | ||
} | ||
} | ||
/** | ||
* Check if url is absolute, hash or data-uri | ||
* @param {String} url | ||
* @returns {boolean} | ||
*/ | ||
function isUrlShouldBeIgnored(url) { | ||
return url[0] === "/" || | ||
url[0] === "#" || | ||
url.indexOf("data:") === 0 || | ||
/^[a-z]+:\/\//.test(url) | ||
} | ||
/** | ||
* Transform url() based on a custom callback | ||
* | ||
* @param {Function} cb callback function | ||
* @param {String} from from | ||
* @param {String} dirname to dirname | ||
* @param {String} urlMeta url meta data | ||
* @param {String} to destination | ||
* @param {Object} options plugin options | ||
* @param {Object} decl postcss declaration | ||
* @return {void} | ||
* @return {PostcssUrl~UrlProcessor} | ||
*/ | ||
function processCustom(result, cb, from, dirname, urlMeta, to, options, decl) { | ||
var newValue = cb(urlMeta.value, decl, from, dirname, to, options, result) | ||
return createUrl(urlMeta, newValue) | ||
function getCustomProcessor(cb) { | ||
return function(result, from, dirname, oldUrl, to, options, decl) { | ||
return cb(oldUrl, decl, from, dirname, to, options, result) | ||
} | ||
} | ||
@@ -168,10 +161,6 @@ | ||
* | ||
* @param {String} from from | ||
* @param {String} dirname to dirname | ||
* @param {String} urlMeta url meta datayy | ||
* @param {String} to destination | ||
* @return {String} new url | ||
* @type {PostcssUrl~UrlProcessor} | ||
*/ | ||
function processRebase(result, from, dirname, urlMeta, to) { | ||
var newPath = urlMeta.value | ||
function processRebase(result, from, dirname, oldUrl, to) { | ||
var newPath = oldUrl | ||
if (dirname !== from) { | ||
@@ -185,3 +174,3 @@ newPath = path.relative(from, dirname + path.sep + newPath) | ||
} | ||
return createUrl(urlMeta, newPath) | ||
return newPath | ||
} | ||
@@ -192,11 +181,5 @@ | ||
* | ||
* @param {String} from from | ||
* @param {String} dirname to dirname | ||
* @param {String} urlMeta url meta data | ||
* @param {String} to destination | ||
* @param {Object} options plugin options | ||
* @param {Object} decl postcss declaration | ||
* @return {String} new url | ||
* @type {PostcssUrl~UrlProcessor} | ||
*/ | ||
function processInline(result, from, dirname, urlMeta, to, options, decl) { | ||
function processInline(result, from, dirname, oldUrl, to, options, decl) { | ||
var maxSize = options.maxSize === undefined ? 14 : options.maxSize | ||
@@ -211,18 +194,10 @@ var fallback = options.fallback | ||
if (typeof fallback === "function") { | ||
return processCustom( | ||
result, | ||
fallback, | ||
from, | ||
dirname, | ||
urlMeta, | ||
to, | ||
options, | ||
decl | ||
) | ||
return getCustomProcessor(fallback) | ||
(result, from, dirname, oldUrl, to, options, decl) | ||
} | ||
switch (fallback) { | ||
case "copy": | ||
return processCopy(result, from, dirname, urlMeta, to, options, decl) | ||
return processCopy(result, from, dirname, oldUrl, to, options, decl) | ||
default: | ||
return createUrl(urlMeta) | ||
return | ||
} | ||
@@ -232,3 +207,3 @@ } | ||
// ignore URLs with hashes/fragments, they can't be inlined | ||
var link = url.parse(urlMeta.value) | ||
var link = url.parse(oldUrl) | ||
if (link.hash) { | ||
@@ -250,3 +225,3 @@ return processFallback() | ||
result.warn("Can't read file '" + file + "', ignoring", {node: decl}) | ||
return createUrl(urlMeta) | ||
return | ||
} | ||
@@ -264,3 +239,3 @@ | ||
result.warn("Unable to find asset mime-type for " + file, {node: decl}) | ||
return createUrl(urlMeta) | ||
return | ||
} | ||
@@ -270,3 +245,3 @@ | ||
var svg = new SvgEncoder(file) | ||
return createUrl(urlMeta, svg.encode()) | ||
return svg.encode() | ||
} | ||
@@ -276,6 +251,3 @@ | ||
file = fs.readFileSync(file) | ||
return createUrl( | ||
urlMeta, | ||
"data:" + mimeType + ";base64," + file.toString("base64") | ||
) | ||
return "data:" + mimeType + ";base64," + file.toString("base64") | ||
} | ||
@@ -290,13 +262,8 @@ | ||
* | ||
* @param {String} from from | ||
* @param {String} dirname to dirname | ||
* @param {String} urlMeta url meta data | ||
* @param {String} to destination | ||
* @param {Object} options plugin options | ||
* @return {String} new url | ||
* @type {PostcssUrl~UrlProcessor} | ||
*/ | ||
function processCopy(result, from, dirname, urlMeta, to, options, decl) { | ||
function processCopy(result, from, dirname, oldUrl, to, options, decl) { | ||
if (from === to) { | ||
result.warn("Option `to` of postcss is required, ignoring", {node: decl}) | ||
return createUrl(urlMeta) | ||
return | ||
} | ||
@@ -308,3 +275,3 @@ var relativeAssetsPath = (options && options.assetsPath) | ||
var filePathUrl = path.resolve(dirname, urlMeta.value) | ||
var filePathUrl = path.resolve(dirname, oldUrl) | ||
var nameUrl = path.basename(filePathUrl) | ||
@@ -314,3 +281,3 @@ | ||
// e.g., url('glyphicons-halflings-regular.eot?#iefix') | ||
var fileLink = url.parse(urlMeta.value) | ||
var fileLink = url.parse(oldUrl) | ||
var filePath = path.resolve(dirname, fileLink.pathname) | ||
@@ -326,3 +293,3 @@ var name = path.basename(filePath) | ||
result.warn("Can't read file '" + filePath + "', ignoring", {node: decl}) | ||
return createUrl(urlMeta) | ||
return | ||
} | ||
@@ -352,3 +319,3 @@ | ||
+ "[\/]\?"), ""), | ||
path.dirname(urlMeta.value) | ||
path.dirname(oldUrl) | ||
) | ||
@@ -375,3 +342,3 @@ absoluteAssetsPath = path.resolve(to, relativeAssetsPath) | ||
} | ||
return createUrl(urlMeta, assetPath) | ||
return assetPath | ||
} |
{ | ||
"name": "postcss-url", | ||
"version": "5.0.1", | ||
"version": "5.0.2", | ||
"description": "PostCSS plugin to rebase or inline on url().", | ||
@@ -30,4 +30,3 @@ "keywords": [ | ||
"path-is-absolute": "^1.0.0", | ||
"postcss": "^5.0.0", | ||
"reduce-function-call": "^1.0.1" | ||
"postcss": "^5.0.0" | ||
}, | ||
@@ -34,0 +33,0 @@ "devDependencies": { |
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
6
16982
284
- Removedreduce-function-call@^1.0.1
- Removedreduce-function-call@1.0.3(transitive)