Browse files

need to keep query and hash from the original url while absolutizing it

  • Loading branch information...
1 parent b621ded commit 2629dcbfb1b7875f10d5eedc6958e67658c5cf44 @kof kof committed Jan 29, 2012
View
36 lib/processors/cssabspath.js
@@ -5,7 +5,8 @@
*/
var utils = require('../utils'),
- n = require('natives');
+ n = require('natives'),
+ $ = require('sharedjs');
/**
* Default options.
@@ -33,7 +34,12 @@ var rurl = /url *\( *["']*([^"')]+)["']* *\)/gi;
*/
exports.run = function(path, data, o) {
var self = this,
- paths = this.paths.slice(0);
+ paths = this.paths.slice(0),
+ baseUrlObj;
+
+ if (o.host) {
+ baseUrlObj = n.url.parse(o.host);
+ }
// add dirname of the file to lookup array
// to look in currend dir first
@@ -42,26 +48,30 @@ exports.run = function(path, data, o) {
paths.unshift(n.path.dirname(path));
function resolve(url) {
- // url is an emebeded image data
- if (/^data\:image/.test(url)) {
+ var pathname,
+ urlObj = n.url.parse(url);
+
+ try {
+ pathname = utils.resolvePath(url, path, self.root, paths);
+ } catch(err) {
+ console.error(err.message);
return url;
}
- var rurl = utils.resolvePath(url, path, self.root, paths);
+ if (pathname !== false) {
- // its remote url, don't change it
- if (rurl === false) {
- rurl = url;
- } else {
// make the url relative to root
- rurl = rurl.replace(self.root, '');
+ pathname = pathname.replace(self.root, '');
+
// add host
- if (o.host) {
- rurl = o.host + rurl;
+ if (baseUrlObj) {
+ urlObj = $.extend({}, baseUrlObj, urlObj);
}
+
+ urlObj.pathname = pathname;
}
- return rurl;
+ return n.url.format(urlObj);
}
data = data.replace(rimport, function(search, url, media) {
View
32 lib/processors/htmlabspath.js
@@ -4,7 +4,8 @@
*/
var utils = require('../utils'),
- n = require('natives');
+ n = require('natives'),
+ $ = require('sharedjs');
/**
* Default options.
@@ -33,7 +34,12 @@ var rtags = [
*/
exports.run = function(path, data, o) {
var self = this,
- paths = this.paths.slice(0);
+ paths = this.paths.slice(0),
+ baseUrlObj;
+
+ if (o.host) {
+ baseUrlObj = n.url.parse(o.host);
+ }
// add dirname of the file to lookup array
// to look in currend dir first
@@ -42,28 +48,30 @@ exports.run = function(path, data, o) {
paths.unshift(n.path.dirname(path));
function resolve(url) {
- var resolvedUrl;
+ var pathname,
+ urlObj = n.url.parse(url);
try {
- resolvedUrl = utils.resolvePath(url, path, self.root, paths);
+ pathname = utils.resolvePath(url, path, self.root, paths);
} catch(err) {
console.error(err.message);
return url;
}
- // its remote url, don't change it
- if (resolvedUrl === false) {
- resolvedUrl = url;
- } else {
+ if (pathname !== false) {
+
// make the url relative to root
- resolvedUrl = resolvedUrl.replace(self.root, '');
+ pathname = pathname.replace(self.root, '');
+
// add host
- if (o.host) {
- resolvedUrl = o.host + resolvedUrl;
+ if (baseUrlObj) {
+ urlObj = $.extend({}, baseUrlObj, urlObj);
}
+
+ urlObj.pathname = pathname;
}
- return resolvedUrl;
+ return n.url.format(urlObj);
}
rtags.forEach(function(regexp) {
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "ams",
"description": "ams - asset management system - plugin enabled build tool with jquery like API",
- "version": "0.0.17",
+ "version": "0.0.18",
"author": "Oleg Slobodskoi <oleg008@gmail.com>",
"repository": "git://github.com/kof/node-ams.git",
"keywords": [
View
22 test/cssabspath.js
@@ -2,17 +2,17 @@ QUnit.module('cssabspath');
var fixtures = __dirname + '/fixtures/cssabspath';
+var context = {
+ paths: [fixtures],
+ root: fixtures
+ };
+
function read(path) {
return require('fs').readFileSync(path, 'utf8')
}
function runTests(o) {
- var context = {
- paths: [fixtures],
- root: fixtures
- };
-
- var res, path,
+ var res, path,
url = o.host + '/a.png';
path = fixtures + '/a.css';
@@ -35,7 +35,6 @@ function runTests(o) {
path = fixtures + '/e.css';
res = run.call(context, path, read(path), o);
- equal(res, '@import "'+url+'" all;@import "'+url+'" all;', '@import ""' );
}
test('without host', function() {
@@ -48,3 +47,12 @@ test('with host', function() {
});
});
+test('url with query or hash', function() {
+ var res, path,
+ o = {host: ''},
+ url = o.host + '/a.png';
+
+ path = fixtures + '/f.css';
+ res = run.call(context, path, read(path), o);
+ equal(res, '@import "/a.css?query=123" all;@import "/a.css#hash" all;', 'url with query and hash' );
+});
View
1 test/fixtures/cssabspath/f.css
@@ -0,0 +1 @@
+@import 'a.css?query=123' all;@import 'a.css#hash' all;
View
1 test/fixtures/htmlabspath/d.html
@@ -0,0 +1 @@
+<img src="/a.css?query=true" alt="image"/>
View
1 test/fixtures/htmlabspath/e.html
@@ -0,0 +1 @@
+<img src="/a.css#hash=true" alt="image"/>
View
29 test/htmlabspath.js
@@ -2,23 +2,22 @@ QUnit.module('htmlabspath');
var fixtures = __dirname + '/fixtures/htmlabspath';
+var context = {
+ paths: [fixtures],
+ root: fixtures
+ };
+
function read(path) {
return require('fs').readFileSync(path, 'utf-8');
}
function runTest(o) {
- var context = {
- paths: [fixtures],
- root: fixtures
- };
-
var res, path, url = o.host + '/a.css';
path = fixtures + '/a.html';
res = run.call(context, path, read(path), o);
-
equal(res, '<link href="'+url+'" rel="stylesheet" type="text/css"/>', 'relative path' );
-
+
path = fixtures + '/b.html';
res = run.call(context, path, read(path), o);
equal(res, '<script type="text/javascript" src="'+url+'"></script>', 'absolute path' );
@@ -39,3 +38,19 @@ test('without host', function() {
host: ''
});
});
+
+test('url with query or hash', function() {
+ var res, path, url, o;
+
+ o = {host: ''};
+ path = fixtures + '/d.html';
+ res = run.call(context, path, read(path), o);
+ url = o.host + '/a.css?query=true'
+ equal(res, '<img src="'+url+'" alt="image"/>', 'absolute path with query' );
+
+ path = fixtures + '/e.html';
+ res = run.call(context, path, read(path), o);
+ url = o.host + '/a.css#hash=true'
+ equal(res, '<img src="'+url+'" alt="image"/>', 'absolute path with hash' );
+
+});

0 comments on commit 2629dcb

Please sign in to comment.