Skip to content

Commit 272e37d

Browse files
committed
Added: related postcss declaration object has been added as a 2nd parameter to the url callback for custom processing
Ref MoOx/cssnext-loader#4
1 parent f815c46 commit 272e37d

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 2.1.0 - 2015-03-12
2+
3+
- Added: related postcss declaration object has been added as a 2nd parameter to the url callback for custom processing
4+
15
# 2.0.2 - 2015-01-31
26

37
- Fixed: url that are just hashes are ignored completely ([#25](https://github.com/postcss/postcss-url/issues/25))

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Allow you to inline assets using base64 syntax. Can use postcss `from` option to
4949

5050
##### `url: {Function}`
5151

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

5555
#### `maxSize: "size in kbytes"`

index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
var fs = require("fs")
55
var path = require("path")
66
var mime = require("mime")
7-
var url = require("url");
7+
var url = require("url")
88
var SvgEncoder = require("directory-encoder/lib/svg-uri-encoder.js")
9-
var reduceFunctionCall = require("reduce-function-call");
9+
var reduceFunctionCall = require("reduce-function-call")
1010

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

5252
if (typeof mode === "function") {
53-
return processCustom(quote, value, mode);
53+
return processCustom(quote, value, mode, decl)
5454
}
5555

5656
// ignore absolute urls, hasshes or data uris
@@ -85,9 +85,10 @@ function processDecl(decl, from, to, mode, options) {
8585
* @param {String} quote
8686
* @param {String} value
8787
* @param {Function} cb
88+
* @param {Object} decl
8889
*/
89-
function processCustom(quote, value, cb) {
90-
var newValue = cb(value)
90+
function processCustom(quote, value, cb, decl) {
91+
var newValue = cb(value, decl)
9192
return createUrl(quote, newValue)
9293
}
9394

@@ -108,9 +109,9 @@ function processRebase(from, dirname, newPath, quote, to) {
108109
newPath = path.resolve(from, newPath)
109110
newPath = path.relative(to, newPath)
110111
if (path.sep == "\\") {
111-
newPath = newPath.replace(/\\/g, "\/");
112+
newPath = newPath.replace(/\\/g, "\/")
112113
}
113-
return createUrl(quote, newPath);
114+
return createUrl(quote, newPath)
114115
}
115116

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

132133
// ignore URLs with hashes/fragments, they can't be inlined
133-
var link = url.parse(value);
134+
var link = url.parse(value)
134135
if (link.hash) {
135-
return createUrl(quote, value);
136+
return createUrl(quote, value)
136137
}
137138

138139
if (basePath) {
@@ -147,7 +148,7 @@ function processInline(from, dirname, newPath, quote, value, options) {
147148
}
148149
else {
149150
var mimeType = mime.lookup(file)
150-
var stats = fs.statSync(file);
151+
var stats = fs.statSync(file)
151152
if (stats.size >= maxSize) {
152153
return createUrl(quote, newPath)
153154
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-url",
3-
"version": "2.0.2",
3+
"version": "2.1.0",
44
"description": "PostCSS plugin to rebase or inline on url().",
55
"keywords": [
66
"css",

test/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,16 @@ test("inline", function(t) {
8181
})
8282

8383
test("custom", function(t) {
84-
var opts = {url: function(url) { return url.toUpperCase(); }}
84+
var declOk = false
85+
var opts = {
86+
url: function(url, decl) {
87+
if (!declOk) {
88+
t.ok(decl, "should offer postcss decl as a 2nd parameter")
89+
declOk = true
90+
}
91+
return url.toUpperCase();
92+
}
93+
}
8594
compareFixtures(t, "custom", "should transform url through custom callback", opts)
8695

8796
t.end()

0 commit comments

Comments
 (0)