Moves relative urls processing while importing to a separate module.
authorGoalSmashers <jakub@goalsmashers.com>
Tue, 3 Sep 2013 07:46:15 +0000 (09:46 +0200)
committerGoalSmashers <jakub@goalsmashers.com>
Tue, 3 Sep 2013 07:48:14 +0000 (09:48 +0200)
lib/images/relative-urls.js [new file with mode: 0644]
lib/imports/inliner.js

diff --git a/lib/images/relative-urls.js b/lib/images/relative-urls.js
new file mode 100644 (file)
index 0000000..f74b065
--- /dev/null
@@ -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;
+  }
+};
index 5adf6f2..4ff5086 100644 (file)
@@ -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