refactor: migrate OC.*QueryString from jQuery

Use native `URLSearchParams` to remove jQuery and deprecate it.

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
This commit is contained in:
Ferdinand Thiessen 2026-01-16 01:36:39 +01:00
parent df2afa9565
commit 29a59c3822
No known key found for this signature in database
GPG key ID: 7E849AE05218500F
4 changed files with 62 additions and 79 deletions

View file

@ -64,7 +64,7 @@ import Plugins from './plugins.js'
import {
build as buildQueryString,
parse as parseQueryString,
} from './query-string.js'
} from './query-string.ts'
import { getRequestToken } from './requesttoken.ts'
import {
linkToRemoteBase,
@ -186,9 +186,7 @@ export default {
*/
getLanguage,
/**
* Query string helpers
*/
// Query string helpers
buildQueryString,
parseQueryString,

View file

@ -1,75 +0,0 @@
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import $ from 'jquery'
/**
* Parses a URL query string into a JS map
*
* @param {string} queryString query string in the format param1=1234&param2=abcde&param3=xyz
* @return {Record<string, string>} map containing key/values matching the URL parameters
*/
export function parse(queryString) {
let pos
let components
const result = {}
let key
if (!queryString) {
return null
}
pos = queryString.indexOf('?')
if (pos >= 0) {
queryString = queryString.substr(pos + 1)
}
const parts = queryString.replace(/\+/g, '%20').split('&')
for (let i = 0; i < parts.length; i++) {
// split on first equal sign
const part = parts[i]
pos = part.indexOf('=')
if (pos >= 0) {
components = [
part.substr(0, pos),
part.substr(pos + 1),
]
} else {
// key only
components = [part]
}
if (!components.length) {
continue
}
key = decodeURIComponent(components[0])
if (!key) {
continue
}
// if equal sign was there, return string
if (components.length > 1) {
result[key] = decodeURIComponent(components[1])
} else {
// no equal sign => null value
result[key] = null
}
}
return result
}
/**
* Builds a URL query from a JS map.
*
* @param {Record<string, string>} params map containing key/values matching the URL parameters
* @return {string} String containing a URL query (without question) mark
*/
export function build(params) {
if (!params) {
return ''
}
return $.map(params, function(value, key) {
let s = encodeURIComponent(key)
if (value !== null && typeof (value) !== 'undefined') {
s += '=' + encodeURIComponent(value)
}
return s
}).join('&')
}

View file

@ -0,0 +1,28 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { expect, test } from 'vitest'
import { build, parse } from './query-string.js'
test.for([
['foo', { foo: '' }],
['foo&bar', { foo: '', bar: '' }],
['foo=1', { foo: '1' }],
['foo=1&bar=1+1', { foo: '1', bar: '1 1' }],
['foo=1&bar=1%201', { foo: '1', bar: '1 1' }],
['?foo=1&bar=1%201', { foo: '1', bar: '1 1' }],
] as const)('Parse URL query: $0', ([input, output]) => {
expect(parse(input)).toStrictEqual(output)
})
test.for([
[{ foo: '' }, 'foo='],
[{ foo: '', bar: '' }, 'foo=&bar='],
[{ foo: '1' }, 'foo=1'],
[{ foo: '1', bar: '1 1' }, 'foo=1&bar=1+1'],
[{ foo: 'ümlaut' }, 'foo=%C3%BCmlaut'],
] as const)('Build URL query: $0', ([input, output]) => {
expect(build(input)).toStrictEqual(output)
})

View file

@ -0,0 +1,32 @@
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
/**
* Parses a URL query string into a JS map
*
* @param queryString - Query string in the format param1=1234&param2=abcde&param3=xyz
* @return Object containing key/values matching the URL parameters
* @deprecated 33.0.0 - Use `URLSearchParams` instead
*/
export function parse(queryString: string): Record<string, string> {
const params = new URLSearchParams(queryString)
return Object.fromEntries(params.entries())
}
/**
* Builds a URL query from a JS map.
*
* @param params - Object containing key/values matching the URL parameters
* @return String containing a URL query (without question) mark
* @deprecated 33.0.0 - Use `URLSearchParams` instead
*/
export function build(params: Record<string, string>): string {
if (!params) {
return ''
}
const search = new URLSearchParams(params)
return search.toString()
}