@@ -168,23 +168,30 @@ function encodeSegment(segment: string, paramName: string): string {
168168 * @param options.allowSeparators - Percent-encode `/` and `\` instead of
169169 * rejecting them, for opaque ids (such as base64) that may legitimately
170170 * contain them. Empty values and dot segments are still rejected.
171- * @param options.preserveWhitespace - Keep spaces instead of trimming, for ids
172- * where surrounding whitespace is significant (an exact-match document id).
173- * Tabs and newlines are still removed, as the URL parser removes them from a
174- * raw URL. Blank values and dot segments are still rejected.
171+ * @param options.preserveWhitespace - Encode the value as given instead of
172+ * trimming it, for ids where surrounding whitespace is significant (an
173+ * exact-match document id). Blank values and dot segments are still rejected.
174+ * @param options.stripTabsAndNewlines - Remove tabs, carriage returns and
175+ * newlines before encoding, as the WHATWG URL parser does to a raw URL. For
176+ * call sites that once interpolated the id unencoded, so an id ending in a
177+ * newline keeps addressing the same resource.
175178 * @returns The percent-encoded segment (trimmed unless `preserveWhitespace`), safe to interpolate.
176179 * @throws If the value is not a string or a usable number, is empty, is a dot
177180 * segment, contains a path separator (unless allowed), or cannot be encoded.
178181 */
179182export function safeUrlPathSegment (
180183 value : string | number | bigint ,
181184 paramName : string ,
182- options ?: { allowSeparators ?: boolean ; preserveWhitespace ?: boolean }
185+ options ?: {
186+ allowSeparators ?: boolean
187+ preserveWhitespace ?: boolean
188+ stripTabsAndNewlines ?: boolean
189+ }
183190) : string {
184191 const raw = toGuardedString ( value , paramName )
185192 const trimmed = raw . trim ( )
186- // The WHATWG URL parser strips tabs and newlines anywhere in a URL, so an id sent raw lost them.
187- const segment = options ?. preserveWhitespace ? raw . replace ( / [ \t \r \n ] / g, '' ) : trimmed
193+ const kept = options ?. preserveWhitespace ? raw : trimmed
194+ const segment = options ?. stripTabsAndNewlines ? kept . replace ( / [ \t \r \n ] / g, '' ) : kept
188195
189196 if ( ! trimmed ) {
190197 throw new Error ( `${ paramName } is required` )
0 commit comments