From: Jakub Pawlowicz Date: Sat, 13 Dec 2014 20:44:24 +0000 (+0000) Subject: Rewrites url rebasing and rewriting in OO style. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=b06cd9c56bf6ad55abeaa3650275d3a96229637a;p=clean-css.git Rewrites url rebasing and rewriting in OO style. --- diff --git a/lib/clean.js b/lib/clean.js index cdf75821..7498c139 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -140,7 +140,7 @@ function minify(context, data) { var freeTextProcessor = new FreeTextProcessor(options.sourceMap); var urlsProcessor = new UrlsProcessor(context, options.sourceMap); - var urlRebase = new UrlRebase(options, context); + var urlRebase = new UrlRebase(context); var selectorsOptimizer = new SelectorsOptimizer(options, context); var stringifierClass = options.sourceMap ? SourceMapStringifier : Stringifier; diff --git a/lib/images/url-rebase.js b/lib/images/url-rebase.js index 789d9b18..1068b730 100644 --- a/lib/images/url-rebase.js +++ b/lib/images/url-rebase.js @@ -3,36 +3,40 @@ var path = require('path'); var UrlRewriter = require('./url-rewriter'); -module.exports = function UrlRebase(options, context) { - var process = function(data) { - var rebaseOpts = { - absolute: !!options.root, - relative: !options.root && !!options.target, - fromBase: options.relativeTo - }; +function UrlRebase(outerContext) { + this.outerContext = outerContext; +} - if (!rebaseOpts.absolute && !rebaseOpts.relative) - return data; +UrlRebase.prototype.process = function (data) { + var options = this.outerContext.options; - if (rebaseOpts.absolute && !!options.target) - context.warnings.push('Both \'root\' and output file given so rebasing URLs as absolute paths'); + var rebaseOpts = { + absolute: !!options.root, + relative: !options.root && !!options.target, + fromBase: options.relativeTo + }; - if (rebaseOpts.absolute) - rebaseOpts.toBase = path.resolve(options.root); + if (!rebaseOpts.absolute && !rebaseOpts.relative) + return data; - if (rebaseOpts.relative) { - var target = fs.existsSync(options.target) && fs.statSync(options.target).isDirectory() ? - options.target : - path.dirname(options.target); + if (rebaseOpts.absolute && !!options.target) + this.outerContext.warnings.push('Both \'root\' and output file given so rebasing URLs as absolute paths'); - rebaseOpts.toBase = path.resolve(target); - } + if (rebaseOpts.absolute) + rebaseOpts.toBase = path.resolve(options.root); - if (!rebaseOpts.fromBase || !rebaseOpts.toBase) - return data; + if (rebaseOpts.relative) { + var target = fs.existsSync(options.target) && fs.statSync(options.target).isDirectory() ? + options.target : + path.dirname(options.target); - return UrlRewriter.process(data, rebaseOpts); - }; + rebaseOpts.toBase = path.resolve(target); + } - return { process: process }; + if (!rebaseOpts.fromBase || !rebaseOpts.toBase) + return data; + + return new UrlRewriter(rebaseOpts).process(data); }; + +module.exports = UrlRebase; diff --git a/lib/images/url-rewriter.js b/lib/images/url-rewriter.js index a13842e4..69c24432 100644 --- a/lib/images/url-rewriter.js +++ b/lib/images/url-rewriter.js @@ -1,61 +1,65 @@ var path = require('path'); var url = require('url'); -module.exports = { - process: function(data, options) { - var tempData = []; - var nextStart = 0; - var nextEnd = 0; - var cursor = 0; - - for (; nextEnd < data.length;) { - nextStart = data.indexOf('url(', nextEnd); - if (nextStart == -1) - break; - - nextEnd = data.indexOf(')', nextStart + 4); - if (nextEnd == -1) - break; - - tempData.push(data.substring(cursor, nextStart)); - var url = data.substring(nextStart + 4, nextEnd); - if (!/\/\*|\*\//.test(url)) - url = url.replace(/['"]/g, ''); - - tempData.push('url(' + this._rebased(url, options) + ')'); - cursor = nextEnd + 1; - } - - return tempData.length > 0 ? - tempData.join('') + data.substring(cursor, data.length) : - data; - }, - - _rebased: function(resource, options) { - var specialUrl = resource[0] == '/' || - resource[0] == '#' || - resource.substring(resource.length - 4) == '.css' || - resource.indexOf('data:') === 0 || - /^https?:\/\//.exec(resource) !== null || - /__\w+__/.exec(resource) !== null; - var rebased; - - if (specialUrl) - return resource; - - if (/https?:\/\//.test(options.toBase)) - return url.resolve(options.toBase, resource); - - if (options.absolute) { - rebased = path - .resolve(path.join(options.fromBase, resource)) - .replace(options.toBase, ''); - } else { - rebased = path.relative(options.toBase, path.join(options.fromBase, resource)); - } - - return process.platform == 'win32' ? - rebased.replace(/\\/g, '/') : - rebased; +function UrlRewriter(options) { + this.options = options; +} + +UrlRewriter.prototype.process = function (data) { + var tempData = []; + var nextStart = 0; + var nextEnd = 0; + var cursor = 0; + + for (; nextEnd < data.length;) { + nextStart = data.indexOf('url(', nextEnd); + if (nextStart == -1) + break; + + nextEnd = data.indexOf(')', nextStart + 4); + if (nextEnd == -1) + break; + + tempData.push(data.substring(cursor, nextStart)); + var url = data.substring(nextStart + 4, nextEnd); + if (!/\/\*|\*\//.test(url)) + url = url.replace(/['"]/g, ''); + + tempData.push('url(' + rebase(url, this.options) + ')'); + cursor = nextEnd + 1; } + + return tempData.length > 0 ? + tempData.join('') + data.substring(cursor, data.length) : + data; }; + +function rebase(resource, options) { + var specialUrl = resource[0] == '/' || + resource[0] == '#' || + resource.substring(resource.length - 4) == '.css' || + resource.indexOf('data:') === 0 || + /^https?:\/\//.exec(resource) !== null || + /__\w+__/.exec(resource) !== null; + var rebased; + + if (specialUrl) + return resource; + + if (/https?:\/\//.test(options.toBase)) + return url.resolve(options.toBase, resource); + + if (options.absolute) { + rebased = path + .resolve(path.join(options.fromBase, resource)) + .replace(options.toBase, ''); + } else { + rebased = path.relative(options.toBase, path.join(options.fromBase, resource)); + } + + return process.platform == 'win32' ? + rebased.replace(/\\/g, '/') : + rebased; +} + +module.exports = UrlRewriter; diff --git a/lib/imports/inliner.js b/lib/imports/inliner.js index 20b291f7..67afa16d 100644 --- a/lib/imports/inliner.js +++ b/lib/imports/inliner.js @@ -254,7 +254,7 @@ function inlineRemoteResource(importedFile, mediaQuery, context) { res.on('end', function() { var importedData = chunks.join(''); if (context.rebase) - importedData = UrlRewriter.process(importedData, { toBase: importedUrl }); + importedData = new UrlRewriter({ toBase: importedUrl }).process(importedData); importedData = context.sourceTracker.store(importedUrl, importedData); importedData = rebaseMap(importedData, importedUrl); @@ -302,11 +302,12 @@ function inlineLocalResource(importedFile, mediaQuery, context) { var importRelativeTo = path.dirname(fullPath); var importedData = fs.readFileSync(fullPath, 'utf8'); if (context.rebase) { - importedData = UrlRewriter.process(importedData, { + var rewriter = new UrlRewriter({ relative: true, fromBase: importRelativeTo, toBase: context.baseRelativeTo }); + importedData = rewriter.process(importedData); } importedData = context.sourceTracker.store(path.resolve(context.relativeTo, fullPath), importedData);