From 251431a11061ddab5869f10ce6dc844eccf412e9 Mon Sep 17 00:00:00 2001 From: Alexandre Gigliotti Date: Tue, 25 Aug 2015 15:08:53 -0700 Subject: [PATCH] Fixed bug where query strings and hashes were not ignored on css url data type local paths. --- lib/inline-css-url.js | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/inline-css-url.js b/lib/inline-css-url.js index 894feaa..b7e262b 100644 --- a/lib/inline-css-url.js +++ b/lib/inline-css-url.js @@ -1,21 +1,43 @@ +var _ = require('lodash'); var datauri = require('datauri'); var isLocalPath = require('is-local-path'); var path = require('path'); var postcss = require('postcss'); -var url = require('postcss-url'); +var postcssUrl = require('postcss-url'); +var url = require('url'); +/** + * Returns url path without query string and hash if present. + * + * @param path + * + * @returns path + */ +var clean = function (path) { + path = url.parse(path); + path = _.pick(path, ['protocol', 'host', 'pathname']); + return url.format(path); +}; +/** + * Convert local url data type paths to datauris. + * + * @param css + * @param filename + * @returns {{css: (css|any), files: Array}} + */ var inline = function (css, filename) { var files = []; var basePath = path.dirname(filename); var result = postcss() - .use(url({ - url: function (url) { - if (isLocalPath(url)) { - url = path.resolve(basePath, url); - files.push(url); - url = datauri(url); + .use(postcssUrl({ + url: function (urlPath) { + if (isLocalPath(urlPath)) { + urlPath = clean(urlPath); + urlPath = path.resolve(basePath, urlPath); + files.push(urlPath); + urlPath = datauri(urlPath); } - return url; + return urlPath; } })) .process(css);