Quote and unquote url parameters, to handle funky chars.
Showing
1 changed file
with
17 additions
and
4 deletions
... | @@ -11,6 +11,20 @@ define(function(require) { | ... | @@ -11,6 +11,20 @@ define(function(require) { |
11 | 11 | ||
12 | //var module = require('module'); | 12 | //var module = require('module'); |
13 | 13 | ||
14 | function unquoteUrlValue(value) { | ||
15 | value = value.replace(/\+/g, ' '); | ||
16 | value = value.replace(/%([0-9a-fA-F]{2})/g, function(match, hexValue) { | ||
17 | return String.fromCharCode(parseInt(hexValue, 16)); | ||
18 | }); | ||
19 | return value; | ||
20 | } | ||
21 | function quoteUrlValue(value) { | ||
22 | // RFC-3986 reserves these chars | ||
23 | return encodeURIComponent(value).replace(/[!'()*]/g, function(c) { | ||
24 | return '%' + c.charCodeAt(0).toString(16); | ||
25 | }); | ||
26 | } | ||
27 | |||
14 | var QueryTextFields = Backbone.Model.extend({ | 28 | var QueryTextFields = Backbone.Model.extend({ |
15 | initialize: function(data, options) { | 29 | initialize: function(data, options) { |
16 | }, | 30 | }, |
... | @@ -76,8 +90,7 @@ define(function(require) { | ... | @@ -76,8 +90,7 @@ define(function(require) { |
76 | if (keyFieldValue) { | 90 | if (keyFieldValue) { |
77 | var key = keyFieldValue[1]; | 91 | var key = keyFieldValue[1]; |
78 | var field = keyFieldValue[2]; | 92 | var field = keyFieldValue[2]; |
79 | var value = keyFieldValue[3]; | 93 | var value = unquoteUrlValue(keyFieldValue[3]); |
80 | value = value.replace(/(\+|%20)/g, ' '); | ||
81 | var impl = formNameMap[key]; | 94 | var impl = formNameMap[key]; |
82 | if (impl) { | 95 | if (impl) { |
83 | if (impl instanceof QueryTextField) { | 96 | if (impl instanceof QueryTextField) { |
... | @@ -135,10 +148,10 @@ define(function(require) { | ... | @@ -135,10 +148,10 @@ define(function(require) { |
135 | var serializedQuery = _.map(result, function(value, key) { | 148 | var serializedQuery = _.map(result, function(value, key) { |
136 | if (_.isArray(value)) { | 149 | if (_.isArray(value)) { |
137 | return _.map(value, function(item, index) { | 150 | return _.map(value, function(item, index) { |
138 | return key + '=' + item; | 151 | return key + '=' + quoteUrlValue(item); |
139 | }).join('&'); | 152 | }).join('&'); |
140 | } else { | 153 | } else { |
141 | return key + '=' + value; | 154 | return key + '=' + quoteUrlValue(value); |
142 | } | 155 | } |
143 | }).join('&'); | 156 | }).join('&'); |
144 | this.set('serializedQuery', serializedQuery); | 157 | this.set('serializedQuery', serializedQuery); | ... | ... |
-
Please register or sign in to post a comment