From bb82cbcace7beba907146dd670df17ecdc155eb4 Mon Sep 17 00:00:00 2001 From: isqua Date: Fri, 9 Oct 2015 22:10:42 +0300 Subject: [PATCH 1/4] Rewrite with RegExp --- index.js | 77 ++++++++++++---------------------------------------- package.json | 3 +- 2 files changed, 19 insertions(+), 61 deletions(-) diff --git a/index.js b/index.js index ccd7567..0b8fcdb 100755 --- a/index.js +++ b/index.js @@ -8,10 +8,12 @@ var postcss = require("postcss") var mime = require("mime") var url = require("url") 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") +var UrlsPatterns = [ + /(url\(\s*['"]?)([^"')]+)(["']?\s*\))/g, +] /** * Fix url() according to source (`from`) or destination (`to`) @@ -70,53 +72,6 @@ function getUrlProcessor(mode) { } } -/** - * 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 + - ")" -} - /** * @callback PostcssUrl~DeclProcessor * @param {Object} decl declaration @@ -132,27 +87,31 @@ function createUrl(urlMeta, newPath) { * @returns {PostcssUrl~DeclProcessor} */ function getDeclProcessor(result, from, to, cb, options, isCustom) { - var valueCallback = function(decl, value) { + var valueCallback = function(decl, oldUrl) { var dirname = decl.source && decl.source.input ? path.dirname(decl.source.input.file) : process.cwd() - var urlMeta = getUrlMetaData(value) - var newValue + var newUrl - if (isCustom || ! isUrlShouldBeIgnored(urlMeta.value)) { - newValue = cb(result, from, dirname, urlMeta.value, to, options, decl) + if (isCustom || ! isUrlShouldBeIgnored(oldUrl)) { + newUrl = cb(result, from, dirname, oldUrl, to, options, decl) } - return createUrl(urlMeta, newValue) + return newUrl || oldUrl } return function(decl) { - if (decl.value && decl.value.indexOf("url(") > -1) { - decl.value = reduceFunctionCall(decl.value, "url", function(value) { - return valueCallback(decl, value) - }) - } + 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 + } + }) } } diff --git a/package.json b/package.json index 5cf8d54..22130ca 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,7 @@ "mime": "^1.2.11", "mkdirp": "^0.5.0", "path-is-absolute": "^1.0.0", - "postcss": "^5.0.0", - "reduce-function-call": "^1.0.1" + "postcss": "^5.0.0" }, "devDependencies": { "eslint": "^1.1.0", From 1c3bad01ada9c083f3b5ce055d5b792d0d2fdaf4 Mon Sep 17 00:00:00 2001 From: isqua Date: Thu, 8 Oct 2015 20:58:39 +0300 Subject: [PATCH 2/4] Process urls inside AlphaImageLoader; tests --- test/fixtures/alpha-image-loader.css | 3 +++ test/fixtures/alpha-image-loader.expected.css | 3 +++ test/index.js | 7 +++++++ 3 files changed, 13 insertions(+) create mode 100644 test/fixtures/alpha-image-loader.css create mode 100644 test/fixtures/alpha-image-loader.expected.css diff --git a/test/fixtures/alpha-image-loader.css b/test/fixtures/alpha-image-loader.css new file mode 100644 index 0000000..de97733 --- /dev/null +++ b/test/fixtures/alpha-image-loader.css @@ -0,0 +1,3 @@ +body { + -filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='isqua.gif',sizingMethod=scale); +} diff --git a/test/fixtures/alpha-image-loader.expected.css b/test/fixtures/alpha-image-loader.expected.css new file mode 100644 index 0000000..ed82af6 --- /dev/null +++ b/test/fixtures/alpha-image-loader.expected.css @@ -0,0 +1,3 @@ +body { + -filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='test/fixtures/isqua.gif',sizingMethod=scale); +} diff --git a/test/index.js b/test/index.js index 607b3cf..4e062d5 100755 --- a/test/index.js +++ b/test/index.js @@ -73,6 +73,13 @@ test("rebase", function(t) { opts, {from: "test/fixtures/transform.css"}, require("postcss-import") ) + compareFixtures( + t, + "alpha-image-loader", + "should rebase in filter", + opts, + {from: "test/fixtures/here", to: "there"} + ) t.end() }) From 2d9f92966662723a3fe7958eb7813dca270b058e Mon Sep 17 00:00:00 2001 From: isqua Date: Fri, 9 Oct 2015 22:13:16 +0300 Subject: [PATCH 3/4] Process urls inside AlphaImageLoader --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 0b8fcdb..81cf04d 100755 --- a/index.js +++ b/index.js @@ -13,6 +13,7 @@ var crypto = require("crypto") var pathIsAbsolute = require("path-is-absolute") var UrlsPatterns = [ /(url\(\s*['"]?)([^"')]+)(["']?\s*\))/g, + /(AlphaImageLoader\(\s*src=['"]?)([^"')]+)(["'])/g, ] /** From 7860167f8e059202885ebe32107f825b9cdd4f62 Mon Sep 17 00:00:00 2001 From: isqua Date: Fri, 9 Oct 2015 22:32:13 +0300 Subject: [PATCH 4/4] Add a comment about UrlRegExp --- index.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/index.js b/index.js index 81cf04d..cd9e05b 100755 --- a/index.js +++ b/index.js @@ -11,6 +11,17 @@ var SvgEncoder = require("directory-encoder/lib/svg-uri-encoder.js") 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,