File tree Expand file tree Collapse file tree 2 files changed +13
-4
lines changed
Expand file tree Collapse file tree 2 files changed +13
-4
lines changed Original file line number Diff line number Diff line change 33 *
44 * Format: comma-separated key:value pairs, e.g. "key1:value1,key2:value2"
55 * - If a value is null or undefined, the key is included without a colon or value
6+ * - Backslashes in keys are escaped; other special characters in keys are not escaped
67 * - Special characters (backslash, colon, comma) in values are backslash-escaped
7- * - Keys are not escaped
88 *
99 * @param queryTags - dictionary of query tag key-value pairs
1010 * @returns serialized string, or undefined if input is empty/null/undefined
@@ -23,12 +23,13 @@ export function serializeQueryTags(
2323
2424 return keys
2525 . map ( ( key ) => {
26+ const escapedKey = key . replace ( / \\ / g, '\\\\' ) ;
2627 const value = queryTags [ key ] ;
2728 if ( value == null ) {
28- return key ;
29+ return escapedKey ;
2930 }
3031 const escapedValue = value . replace ( / [ \\ : , ] / g, ( c ) => `\\${ c } ` ) ;
31- return `${ key } :${ escapedValue } ` ;
32+ return `${ escapedKey } :${ escapedValue } ` ;
3233 } )
3334 . join ( ',' ) ;
3435}
Original file line number Diff line number Diff line change @@ -52,7 +52,15 @@ describe('serializeQueryTags', () => {
5252 expect ( serializeQueryTags ( { val : 'a\\b:c,d' } ) ) . to . equal ( 'val:a\\\\b\\:c\\,d' ) ;
5353 } ) ;
5454
55- it ( 'should not escape special characters in keys' , ( ) => {
55+ it ( 'should escape backslash in key' , ( ) => {
56+ expect ( serializeQueryTags ( { 'a\\b' : 'value' } ) ) . to . equal ( 'a\\\\b:value' ) ;
57+ } ) ;
58+
59+ it ( 'should escape backslash in key with null value' , ( ) => {
60+ expect ( serializeQueryTags ( { 'a\\b' : null } ) ) . to . equal ( 'a\\\\b' ) ;
61+ } ) ;
62+
63+ it ( 'should not escape other special characters in keys' , ( ) => {
5664 expect ( serializeQueryTags ( { 'key:name' : 'value' } ) ) . to . equal ( 'key:name:value' ) ;
5765 } ) ;
5866} ) ;
You can’t perform that action at this time.
0 commit comments