From 5b28120014db6b41a11b4d62ea80105836587e44 Mon Sep 17 00:00:00 2001 From: GoalSmashers Date: Tue, 3 Sep 2013 09:46:15 +0200 Subject: [PATCH] Moves relative urls processing while importing to a separate module. --- lib/images/relative-urls.js | 30 ++++++++++++++++++++++++++++++ lib/imports/inliner.js | 31 +++---------------------------- 2 files changed, 33 insertions(+), 28 deletions(-) create mode 100644 lib/images/relative-urls.js diff --git a/lib/images/relative-urls.js b/lib/images/relative-urls.js new file mode 100644 index 00000000..f74b0657 --- /dev/null +++ b/lib/images/relative-urls.js @@ -0,0 +1,30 @@ +var path = require('path'); + +module.exports = { + process: function(data, fromBase, toBase) { + 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).replace(/['"]/g, ''); + if (url[0] != '/' && url.indexOf('data:') !== 0 && url.substring(url.length - 4) != '.css') + url = path.relative(toBase, path.join(fromBase, url)).replace(/\\/g, '/'); + tempData.push('url(' + url + ')'); + cursor = nextEnd + 1; + } + + return tempData.length > 0 ? + tempData.join('') + data.substring(cursor, data.length) : + data; + } +}; diff --git a/lib/imports/inliner.js b/lib/imports/inliner.js index 5adf6f24..4ff50869 100644 --- a/lib/imports/inliner.js +++ b/lib/imports/inliner.js @@ -1,6 +1,8 @@ var fs = require('fs'); var path = require('path'); +var RelativeUrls = require('../images/relative-urls'); + module.exports = function Inliner() { var process = function(data, options) { var tempData = []; @@ -64,7 +66,7 @@ module.exports = function Inliner() { var importedData = fs.readFileSync(fullPath, 'utf8'); var importRelativeTo = path.dirname(fullPath); - importedData = rebaseRelativeURLs(importedData, importRelativeTo, options._baseRelativeTo); + importedData = RelativeUrls.process(importedData, importRelativeTo, options._baseRelativeTo); var inlinedData = process(importedData, { root: options.root, @@ -77,33 +79,6 @@ module.exports = function Inliner() { inlinedData; }; - var rebaseRelativeURLs = function(data, fromBase, toBase) { - 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).replace(/['"]/g, ''); - if (url[0] != '/' && url.indexOf('data:') !== 0 && url.substring(url.length - 4) != '.css') - url = path.relative(toBase, path.join(fromBase, url)).replace(/\\/g, '/'); - tempData.push('url(' + url + ')'); - cursor = nextEnd + 1; - } - - return tempData.length > 0 ? - tempData.join('') + data.substring(cursor, data.length) : - data; - }; - return { // Inlines all imports taking care of repetitions, unknown files, and circular dependencies process: process -- 2.34.1