[UI] A more robust way to encode/decode URI arguments.

This commit is contained in:
Fabio Manganiello 2024-07-16 03:10:54 +02:00
parent e710a3a974
commit aa92db9850
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -16,7 +16,7 @@ export default {
.reduce((acc, obj) => {
const tokens = obj.split('=')
if (tokens[0]?.length)
acc[tokens[0]] = tokens[1]
acc[tokens[0]] = decodeURIComponent(tokens[1])
return acc
}, {})
},
@ -40,10 +40,24 @@ export default {
window.location.href = location
},
encodeValue(value) {
if (!value?.length || value === 'null' || value === 'undefined')
return ''
// Don't re-encode the value if it's already encoded
if (value.match(/%[0-9A-F]{2}/i))
return value
return encodeURIComponent(value)
},
fragmentFromArgs(args) {
return Object.entries(args)
.filter(
([key, value]) => this.encodeValue(key)?.length && this.encodeValue(value)?.length
)
.map(
([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
([key, value]) => `${this.encodeValue(key)}=${this.encodeValue(value)}`
)
.join('&')
},