fix(url-tools): keep duplicate params, show fragment, add strict encoding

This commit is contained in:
a0666660 2026-10-01 15:16:46 +08:00
parent 9a1d8b10c3
commit c7499338ea
2 changed files with 13 additions and 3 deletions

View file

@ -5,7 +5,16 @@ import { isNotThrowing } from '@/utils/boolean';
import { withDefaultOnError } from '@/utils/defaults';
const encodeInput = ref('Hello world :)');
const encodeOutput = computed(() => withDefaultOnError(() => encodeURIComponent(encodeInput.value), ''));
// RFC 3986 also reserves !, ', (, ) and * which encodeURIComponent leaves as-is (#1445)
const encodeStrict = useStorage('url-encoder:strict', false);
function strictEncode(value: string) {
return encodeURIComponent(value).replace(/[!'()*]/g, ch => `%${ch.charCodeAt(0).toString(16).toUpperCase()}`);
}
const encodeOutput = computed(() =>
withDefaultOnError(() => (encodeStrict.value ? strictEncode(encodeInput.value) : encodeURIComponent(encodeInput.value)), ''),
);
const encodedValidation = useValidation({
source: encodeInput,

View file

@ -21,6 +21,7 @@ const properties: { title: string; key: keyof URL }[] = [
{ title: 'Port', key: 'port' },
{ title: 'Path', key: 'pathname' },
{ title: 'Params', key: 'search' },
{ title: 'Fragment', key: 'hash' },
];
</script>
@ -49,8 +50,8 @@ const properties: { title: string; key: keyof URL }[] = [
/>
<div
v-for="[k, v] in Object.entries(Object.fromEntries(urlParsed?.searchParams.entries() ?? []))"
:key="k"
v-for="[k, v] in urlParsed?.searchParams.entries() ?? []"
:key="`${k}=${v}`"
mb-2
w-full
flex