Skip to content

Commit

Permalink
handle bad utf in urls better.
Browse files Browse the repository at this point in the history
  • Loading branch information
cainus committed Mar 6, 2014
1 parent 12ba7a2 commit b5a62da
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ UrlGrey.prototype.path = function(){
str = str.replace(/\/$/, ''); // remove all trailing slashes
args = str.split('/');
for(var i = 0; i < args.length; i++){
args[i] = encodeURIComponent(args[i]);
args[i] = this.encode(args[i]);
}
str = args.join('/');
if (str[0] !== '/'){ str = '/' + str; }
Expand All @@ -262,11 +262,19 @@ UrlGrey.prototype.rawPath = function(){
};

UrlGrey.prototype.encode = function(str){
return encodeURIComponent(str);
try {
return encodeURIComponent(str);
} catch (ex) {
return querystring.escape(str);
}
};

UrlGrey.prototype.decode = function(str){
return decodeURIComponent(str);
try {
return decodeURIComponent(str);
} catch (ex) {
return querystring.unescape(str);
}
};

UrlGrey.prototype.parent = function(){
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"url",
"uri"
],
"version": "0.3.2",
"version": "0.3.3",
"bugs": {
"url": "https://github.com/cainus/urlgrey/issues"
},
Expand Down
8 changes: 4 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,13 @@ describe("urlgrey", function(){
describe("#rawChild", function(){
it("returns a url with the given path suffix added", function(){
var url = "http://asdf.com/path?asdf=1234#frag";
urlgrey(url).rawChild('kid here')
.toString().should.equal('http://asdf.com/path/kid here');
urlgrey(url).rawChild('{kid here}')
.toString().should.equal('http://asdf.com/path/{kid here}');
});
it("returns a url with the given path suffixes added, without escaping", function(){
var url = "http://asdf.com/path?asdf=1234#frag";
urlgrey(url).rawChild('kid here', 'and here')
.toString().should.equal('http://asdf.com/path/kid here/and here');
urlgrey(url).rawChild('{kid here}', '{and here}')
.toString().should.equal('http://asdf.com/path/{kid here}/{and here}');
});
it("returns the last item in the path if there is no input", function(){
var url = "http://asdf.com/path/kid?asdf=1234#frag";
Expand Down

0 comments on commit b5a62da

Please sign in to comment.