From 46c1e33223426a42141989042048a7befd6cebad Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 4 Oct 2014 15:02:49 -0700 Subject: [PATCH 1/2] add escape/unescape per http://nodejs.org/api/querystring.html, fix for https://github.com/Gozala/querystring/issues/6 --- decode.js | 4 ++-- encode.js | 11 ++++++----- escape.js | 24 ++++++++++++++++++++++++ index.js | 13 +++++++++++-- test/index.js | 26 ++++++++++++++++++++++++++ unescape.js | 24 ++++++++++++++++++++++++ 6 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 escape.js create mode 100644 unescape.js diff --git a/decode.js b/decode.js index a6518b8..0c67b57 100644 --- a/decode.js +++ b/decode.js @@ -64,8 +64,8 @@ module.exports = function(qs, sep, eq, options) { vstr = ''; } - k = decodeURIComponent(kstr); - v = decodeURIComponent(vstr); + k = this.unescape(kstr); + v = this.unescape(vstr); if (!hasOwnProperty(obj, k)) { obj[k] = v; diff --git a/encode.js b/encode.js index 4f2b561..7f3deb6 100644 --- a/encode.js +++ b/encode.js @@ -38,6 +38,7 @@ var stringifyPrimitive = function(v) { }; module.exports = function(obj, sep, eq, name) { + var that = this; sep = sep || '&'; eq = eq || '='; if (obj === null) { @@ -46,19 +47,19 @@ module.exports = function(obj, sep, eq, name) { if (typeof obj === 'object') { return Object.keys(obj).map(function(k) { - var ks = encodeURIComponent(stringifyPrimitive(k)) + eq; + var ks = that.escape(stringifyPrimitive(k)) + eq; if (Array.isArray(obj[k])) { return obj[k].map(function(v) { - return ks + encodeURIComponent(stringifyPrimitive(v)); + return ks + that.escape(stringifyPrimitive(v)); }).join(sep); } else { - return ks + encodeURIComponent(stringifyPrimitive(obj[k])); + return ks + that.escape(stringifyPrimitive(obj[k])); } }).join(sep); } if (!name) return ''; - return encodeURIComponent(stringifyPrimitive(name)) + eq + - encodeURIComponent(stringifyPrimitive(obj)); + return that.escape(stringifyPrimitive(name)) + eq + + that.escape(stringifyPrimitive(obj)); }; diff --git a/escape.js b/escape.js new file mode 100644 index 0000000..c6a1363 --- /dev/null +++ b/escape.js @@ -0,0 +1,24 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +'use strict'; + +module.exports = encodeURIComponent; \ No newline at end of file diff --git a/index.js b/index.js index 99826ea..c4edc70 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,13 @@ 'use strict'; -exports.decode = exports.parse = require('./decode'); -exports.encode = exports.stringify = require('./encode'); +module.exports = (function () { + var methods = {}; + + methods.decode = methods.parse = require('./decode'); + methods.encode = methods.stringify = require('./encode'); + methods.escape = require('./escape'); + methods.unescape = require('./unescape'); + + return methods; +}()); + diff --git a/test/index.js b/test/index.js index 62eb2ac..dd68ebc 100644 --- a/test/index.js +++ b/test/index.js @@ -208,3 +208,29 @@ exports['test nested in colon'] = function(assert) { assert.deepEqual({}, qs.parse(), 'parse undefined'); }; + +exports['test stringify() with escape() override'] = function(assert) { + var original = qs.escape; + qs.escape = function (value) { return value; }; + var f = qs.stringify({a: 'a :'}); + assert.equal(f, 'a=a :' , 'stringify with escape() override"'); + qs.escape = original; +}; + +exports['test escape()'] = function(assert) { + var f = qs.escape('a :'); + assert.equal(f, 'a%20%3A', 'escape "a :"'); +}; + +exports['test parse() with unescape() override'] = function(assert) { + var original = qs.unescape; + qs.unescape = function (value) { return 'static'; }; + var f = qs.parse('a=a :'); + assert.deepEqual(f, {static: 'static'}, 'unescape "a :" with override"'); + qs.unescape = original; +}; + +exports['test unescape()'] = function(assert) { + var f = qs.unescape('a%20%3A'); + assert.equal(f, 'a :', 'unescape "a :"'); +}; diff --git a/unescape.js b/unescape.js new file mode 100644 index 0000000..e93534c --- /dev/null +++ b/unescape.js @@ -0,0 +1,24 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +'use strict'; + +module.exports = decodeURIComponent; \ No newline at end of file From 8089e0116e60af5fbba4f3bcde7864bfafef6a03 Mon Sep 17 00:00:00 2001 From: balu Date: Sun, 1 Jan 2017 23:56:47 +0100 Subject: [PATCH 2/2] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bdbc970..4567544 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "author": "Irakli Gozalishvili ", "repository": { "type": "git", - "url": "git://github.com/Gozala/querystring.git", + "url": "git://github.com/balu-/querystring.git", "web": "https://github.com/Gozala/querystring" }, "bugs": {