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 @@ -812,17 +812,36 @@ function startingTag(element) {
812
812
813
813
/////////////////////////////////////////////////
814
814
815
+ /**
816
+ * Tries to decode the URI component without throwing an exception.
817
+ *
818
+ * @private
819
+ * @param str value potential URI component to check.
820
+ * @returns {boolean } True if `value` can be decoded
821
+ * with the decodeURIComponent function.
822
+ */
823
+ function tryDecodeURIComponent ( value ) {
824
+ try {
825
+ return decodeURIComponent ( value ) ;
826
+ } catch ( e ) {
827
+ // Ignore any invalid uri component
828
+ }
829
+ }
830
+
831
+
815
832
/**
816
833
* Parses an escaped url query string into key-value pairs.
817
834
* @returns Object.<(string|boolean)>
818
835
*/
819
836
function parseKeyValue ( /**string*/ keyValue ) {
820
837
var obj = { } , key_value , key ;
821
838
forEach ( ( keyValue || "" ) . split ( '&' ) , function ( keyValue ) {
822
- if ( keyValue ) {
839
+ if ( keyValue ) {
823
840
key_value = keyValue . split ( '=' ) ;
824
- key = decodeURIComponent ( key_value [ 0 ] ) ;
825
- obj [ key ] = isDefined ( key_value [ 1 ] ) ? decodeURIComponent ( key_value [ 1 ] ) : true ;
841
+ key = tryDecodeURIComponent ( key_value [ 0 ] ) ;
842
+ if ( isDefined ( key ) ) {
843
+ obj [ key ] = isDefined ( key_value [ 1 ] ) ? tryDecodeURIComponent ( key_value [ 1 ] ) : true ;
844
+ }
826
845
}
827
846
} ) ;
828
847
return obj ;
Original file line number Diff line number Diff line change @@ -310,6 +310,12 @@ describe('angular', function() {
310
310
expect ( parseKeyValue ( 'flag1&key=value&flag2' ) ) .
311
311
toEqual ( { flag1 : true , key : 'value' , flag2 : true } ) ;
312
312
} ) ;
313
+ it ( 'should ignore key values that are not valid URI components' , function ( ) {
314
+ expect ( function ( ) { parseKeyValue ( '%' ) ; } ) . not . toThrow ( ) ;
315
+ expect ( parseKeyValue ( '%' ) ) . toEqual ( { } ) ;
316
+ expect ( parseKeyValue ( 'invalid=%' ) ) . toEqual ( { invalid : undefined } ) ;
317
+ expect ( parseKeyValue ( 'invalid=%&valid=good' ) ) . toEqual ( { invalid : undefined , valid : 'good' } ) ;
318
+ } ) ;
313
319
} ) ;
314
320
315
321
describe ( 'toKeyValue' , function ( ) {
You can’t perform that action at this time.
0 commit comments