This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +28
-3
lines changed
2 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -821,17 +821,36 @@ function startingTag(element) {
821
821
822
822
/////////////////////////////////////////////////
823
823
824
+ /**
825
+ * Tries to decode the URI component without throwing an exception.
826
+ *
827
+ * @private
828
+ * @param str value potential URI component to check.
829
+ * @returns {boolean } True if `value` can be decoded
830
+ * with the decodeURIComponent function.
831
+ */
832
+ function tryDecodeURIComponent ( value ) {
833
+ try {
834
+ return decodeURIComponent ( value ) ;
835
+ } catch ( e ) {
836
+ // Ignore any invalid uri component
837
+ }
838
+ }
839
+
840
+
824
841
/**
825
842
* Parses an escaped url query string into key-value pairs.
826
843
* @returns Object.<(string|boolean)>
827
844
*/
828
845
function parseKeyValue ( /**string*/ keyValue ) {
829
846
var obj = { } , key_value , key ;
830
847
forEach ( ( keyValue || "" ) . split ( '&' ) , function ( keyValue ) {
831
- if ( keyValue ) {
848
+ if ( keyValue ) {
832
849
key_value = keyValue . split ( '=' ) ;
833
- key = decodeURIComponent ( key_value [ 0 ] ) ;
834
- obj [ key ] = isDefined ( key_value [ 1 ] ) ? decodeURIComponent ( key_value [ 1 ] ) : true ;
850
+ key = tryDecodeURIComponent ( key_value [ 0 ] ) ;
851
+ if ( isDefined ( key ) ) {
852
+ obj [ key ] = isDefined ( key_value [ 1 ] ) ? tryDecodeURIComponent ( key_value [ 1 ] ) : true ;
853
+ }
835
854
}
836
855
} ) ;
837
856
return obj ;
Original file line number Diff line number Diff line change @@ -312,6 +312,12 @@ describe('angular', function() {
312
312
expect ( parseKeyValue ( 'flag1&key=value&flag2' ) ) .
313
313
toEqual ( { flag1 : true , key : 'value' , flag2 : true } ) ;
314
314
} ) ;
315
+ it ( 'should ignore key values that are not valid URI components' , function ( ) {
316
+ expect ( function ( ) { parseKeyValue ( '%' ) ; } ) . not . toThrow ( ) ;
317
+ expect ( parseKeyValue ( '%' ) ) . toEqual ( { } ) ;
318
+ expect ( parseKeyValue ( 'invalid=%' ) ) . toEqual ( { invalid : undefined } ) ;
319
+ expect ( parseKeyValue ( 'invalid=%&valid=good' ) ) . toEqual ( { invalid : undefined , valid : 'good' } ) ;
320
+ } ) ;
315
321
} ) ;
316
322
317
323
describe ( 'toKeyValue' , function ( ) {
You can’t perform that action at this time.
0 commit comments