Skip to content

Commit

Permalink
feat(Url Encoder): enhance encoding options
Browse files Browse the repository at this point in the history
Handles RFC3986 - URIComponent ; RFC5987 (Link - Content-Disposition) and others
Fix CorentinTh#1445
  • Loading branch information
sharevb committed Jan 26, 2025
1 parent bf2f63c commit f1adc66
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/tools/url-encoder/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Link } from '@vicons/tabler';
import { defineTool } from '../tool';
import { translate } from '@/plugins/i18n.plugin';

export const tool = defineTool({
name: translate('tools.url-encoder.title'),
name: 'Encode/decode url formatted strings',
path: '/url-encoder',
description: translate('tools.url-encoder.description'),
description: 'Encode to url-encoded format (also known as "percent-encoded") or decode from it.',
keywords: ['url', 'encode', 'decode', 'percent', '%20', 'format'],
component: () => import('./url-encoder.vue'),
icon: Link,
Expand Down
80 changes: 77 additions & 3 deletions src/tools/url-encoder/url-encoder.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,82 @@
<script setup lang="ts">
import { useCopy } from '@/composable/copy';
import { useQueryParamOrStorage } from '@/composable/queryParams';
import { useValidation } from '@/composable/validation';
import { isNotThrowing } from '@/utils/boolean';
import { withDefaultOnError } from '@/utils/defaults';
const encodings = [
{ value: 'URIComponent', label: 'URIComponent' },
{ value: 'RFC3986URIComponent', label: 'RFC3986 - URIComponent' },
{ value: 'RFC5987', label: 'RFC5987 (Link - Content-Disposition)' },
{ value: 'URI', label: 'URI' },
{ value: 'RFC3986URI', label: 'RFC3986 - URI' },
];
const encoding = useQueryParamOrStorage({ name: 'enc', storageName: 'url-encode:enc', defaultValue: 'URIComponent' });
function encodeRFC3986URIComponent(str: string) {
return encodeURIComponent(str).replace(
/[!'()*]/g,
(c: string) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,
);
}
function encodeRFC5987ValueChars(str: string) {
return (
encodeURIComponent(str)
// The following creates the sequences %27 %28 %29 %2A (Note that
// the valid encoding of "*" is %2A, which necessitates calling
// toUpperCase() to properly encode). Although RFC3986 reserves "!",
// RFC5987 does not, so we do not need to escape it.
.replace(
/['()*]/g,
c => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,
)
// The following are not required for percent-encoding per RFC5987,
// so we can allow for a little better readability over the wire: |`^
.replace(/%(7C|60|5E)/g, (str, hex) =>
String.fromCharCode(Number.parseInt(hex, 16)),
)
);
}
function encodeRFC3986URI(str: string) {
return encodeURI(str)
.replace(/%5B/g, '[')
.replace(/%5D/g, ']')
.replace(
/[!'()*]/g,
c => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,
);
}
function encode(str: string) {
if (encoding.value === 'URIComponent') {
return encodeURIComponent(str);
}
if (encoding.value === 'RFC3986URIComponent') {
return encodeRFC3986URIComponent(str);
}
if (encoding.value === 'RFC5987') {
return encodeRFC5987ValueChars(str);
}
if (encoding.value === 'URI') {
return encodeURI(str);
}
if (encoding.value === 'RFC3986URI') {
return encodeRFC3986URI(str);
}
return str;
}
const encodeInput = ref('Hello world :)');
const encodeOutput = computed(() => withDefaultOnError(() => encodeURIComponent(encodeInput.value), ''));
const encodeOutput = computed(() => withDefaultOnError(() => encode(encodeInput.value), ''));
const encodedValidation = useValidation({
source: encodeInput,
rules: [
{
validator: value => isNotThrowing(() => encodeURIComponent(value)),
validator: value => isNotThrowing(() => encode(value)),
message: 'Impossible to parse this string',
},
],
Expand All @@ -23,7 +88,7 @@ const decodeInput = ref('Hello%20world%20%3A)');
const decodeOutput = computed(() => withDefaultOnError(() => decodeURIComponent(decodeInput.value), ''));
const decodeValidation = useValidation({
source: decodeInput,
source: encodeInput,
rules: [
{
validator: value => isNotThrowing(() => decodeURIComponent(value)),
Expand All @@ -37,6 +102,15 @@ const { copy: copyDecoded } = useCopy({ source: decodeOutput, text: 'Decoded str

<template>
<c-card title="Encode">
<c-select
v-model:value="encoding"
label="Encoding Kind:"
label-position="left"
:options="encodings"
/>

<n-divider />

<c-input-text
v-model:value="encodeInput"
label="Your string :"
Expand Down

0 comments on commit f1adc66

Please sign in to comment.