From b5a62da193c67d34561b9edaef1fbf6bed3805e3 Mon Sep 17 00:00:00 2001 From: Gregg Caines Date: Thu, 6 Mar 2014 09:17:53 -0800 Subject: [PATCH] handle bad utf in urls better. --- index.js | 14 +++++++++++--- package.json | 2 +- test/index.js | 8 ++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 307d9f1..96c6e62 100644 --- a/index.js +++ b/index.js @@ -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; } @@ -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(){ diff --git a/package.json b/package.json index 9572be9..c1ff116 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "url", "uri" ], - "version": "0.3.2", + "version": "0.3.3", "bugs": { "url": "https://github.com/cainus/urlgrey/issues" }, diff --git a/test/index.js b/test/index.js index 57ca164..63dd044 100644 --- a/test/index.js +++ b/test/index.js @@ -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";