From 8264d08085adc2ab57f6598b9fc9f6e263c8b4f3 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Thu, 20 Jun 2013 13:59:46 +0100 Subject: fix(Angular.js): don't crash on invalid query parameters --- src/Angular.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'src/Angular.js') diff --git a/src/Angular.js b/src/Angular.js index 50d7d76c..839a5a77 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -821,6 +821,23 @@ function startingTag(element) { ///////////////////////////////////////////////// +/** + * Tries to decode the URI component without throwing an exception. + * + * @private + * @param str value potential URI component to check. + * @returns {boolean} True if `value` can be decoded + * with the decodeURIComponent function. + */ +function tryDecodeURIComponent(value) { + try { + return decodeURIComponent(value); + } catch(e) { + // Ignore any invalid uri component + } +} + + /** * Parses an escaped url query string into key-value pairs. * @returns Object.<(string|boolean)> @@ -828,10 +845,12 @@ function startingTag(element) { function parseKeyValue(/**string*/keyValue) { var obj = {}, key_value, key; forEach((keyValue || "").split('&'), function(keyValue){ - if (keyValue) { + if ( keyValue ) { key_value = keyValue.split('='); - key = decodeURIComponent(key_value[0]); - obj[key] = isDefined(key_value[1]) ? decodeURIComponent(key_value[1]) : true; + key = tryDecodeURIComponent(key_value[0]); + if ( isDefined(key) ) { + obj[key] = isDefined(key_value[1]) ? tryDecodeURIComponent(key_value[1]) : true; + } } }); return obj; -- cgit v1.2.3