Skip to content

Commit

Permalink
Added: related postcss declaration object has been added as a 2nd par…
Browse files Browse the repository at this point in the history
…ameter to the url callback for custom processing

Ref MoOx/cssnext-loader#4
  • Loading branch information
MoOx committed Mar 12, 2015
1 parent f815c46 commit 272e37d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.1.0 - 2015-03-12

- Added: related postcss declaration object has been added as a 2nd parameter to the url callback for custom processing

# 2.0.2 - 2015-01-31

- Fixed: url that are just hashes are ignored completely ([#25](https://github.com/postcss/postcss-url/issues/25))
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Allow you to inline assets using base64 syntax. Can use postcss `from` option to

##### `url: {Function}`

Custom transform function. Takes one argument (original url) and should return the transformed url.
Custom transform function. Takes two arguments (original url, related postcss declaration object) and should return the transformed url.
You can use this option to adjust urls for CDN.

#### `maxSize: "size in kbytes"`
Expand Down
21 changes: 11 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
var fs = require("fs")
var path = require("path")
var mime = require("mime")
var url = require("url");
var url = require("url")
var SvgEncoder = require("directory-encoder/lib/svg-uri-encoder.js")
var reduceFunctionCall = require("reduce-function-call");
var reduceFunctionCall = require("reduce-function-call")

/**
* Fix url() according to source (`from`) or destination (`to`)
Expand Down Expand Up @@ -50,7 +50,7 @@ function processDecl(decl, from, to, mode, options) {
value = unquote(value, quote)

if (typeof mode === "function") {
return processCustom(quote, value, mode);
return processCustom(quote, value, mode, decl)
}

// ignore absolute urls, hasshes or data uris
Expand Down Expand Up @@ -85,9 +85,10 @@ function processDecl(decl, from, to, mode, options) {
* @param {String} quote
* @param {String} value
* @param {Function} cb
* @param {Object} decl
*/
function processCustom(quote, value, cb) {
var newValue = cb(value)
function processCustom(quote, value, cb, decl) {
var newValue = cb(value, decl)
return createUrl(quote, newValue)
}

Expand All @@ -108,9 +109,9 @@ function processRebase(from, dirname, newPath, quote, to) {
newPath = path.resolve(from, newPath)
newPath = path.relative(to, newPath)
if (path.sep == "\\") {
newPath = newPath.replace(/\\/g, "\/");
newPath = newPath.replace(/\\/g, "\/")
}
return createUrl(quote, newPath);
return createUrl(quote, newPath)
}

/**
Expand All @@ -130,9 +131,9 @@ function processInline(from, dirname, newPath, quote, value, options) {
maxSize *= 1024;

// ignore URLs with hashes/fragments, they can't be inlined
var link = url.parse(value);
var link = url.parse(value)
if (link.hash) {
return createUrl(quote, value);
return createUrl(quote, value)
}

if (basePath) {
Expand All @@ -147,7 +148,7 @@ function processInline(from, dirname, newPath, quote, value, options) {
}
else {
var mimeType = mime.lookup(file)
var stats = fs.statSync(file);
var stats = fs.statSync(file)
if (stats.size >= maxSize) {
return createUrl(quote, newPath)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-url",
"version": "2.0.2",
"version": "2.1.0",
"description": "PostCSS plugin to rebase or inline on url().",
"keywords": [
"css",
Expand Down
11 changes: 10 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,16 @@ test("inline", function(t) {
})

test("custom", function(t) {
var opts = {url: function(url) { return url.toUpperCase(); }}
var declOk = false
var opts = {
url: function(url, decl) {
if (!declOk) {
t.ok(decl, "should offer postcss decl as a 2nd parameter")
declOk = true
}
return url.toUpperCase();
}
}
compareFixtures(t, "custom", "should transform url through custom callback", opts)

t.end()
Expand Down

0 comments on commit 272e37d

Please sign in to comment.