Skip to content

Commit 891d8d6

Browse files
authored
Feat: enhance URL handling by adding custom URL validation and ensuring HTTPS in input (#5)
1 parent 421df46 commit 891d8d6

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

web/src/components/Main.svelte

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,50 @@
11
<script>
22
export let longUrl = '';
33
export let shortUrl = '';
4-
export let customUrl = ''; // New input for custom URL
4+
export let customUrl = '';
55
export let errorMessage = '';
66
export let expiry = '';
77
export let validationError = '';
88
export let isCopied = false;
9-
export let showAccordion = false; // Controls the accordion
9+
export let showAccordion = false;
1010
1111
// Function to validate the custom URL input
1212
const validateCustomUrl = (url) => {
13-
// Ensure no spaces or illegal characters (allow only alphanumeric, underscores, and dashes)
1413
const customUrlRegex = /^[a-zA-Z0-9_-]+$/;
1514
return customUrlRegex.test(url);
1615
};
1716
17+
// Function to ensure URL has https://
18+
const ensureHttps = (url) => {
19+
if (!url.startsWith('http')) {
20+
return `https://${url}`;
21+
}
22+
return url;
23+
};
24+
25+
const handleInputChange = (event) => {
26+
longUrl = ensureHttps(event.target.value);
27+
};
28+
1829
export let shortenUrl = async () => {
1930
if (!longUrl) {
2031
validationError = 'Please enter a valid URL.';
2132
return;
2233
}
34+
2335
if (customUrl && !validateCustomUrl(customUrl)) {
2436
validationError =
2537
'Custom URL contains spaces or invalid characters. Only alphanumeric, dashes, and underscores are allowed.';
2638
return;
2739
}
28-
validationError = ''; // Clear validation error
40+
validationError = '';
2941
3042
try {
3143
errorMessage = '';
32-
shortUrl = ''; // Clear previous result
44+
shortUrl = '';
3345
34-
// Mocked shortening for UI testing
3546
shortUrl = `${window.location.origin}/${customUrl || 'generated-url'}`;
3647
37-
// Collapse the accordion
3848
showAccordion = false;
3949
} catch (error) {
4050
console.error('Error shortening URL:', error);
@@ -65,6 +75,7 @@
6575
id="long-url"
6676
type="url"
6777
bind:value={longUrl}
78+
on:input={handleInputChange}
6879
placeholder="Enter your URL"
6980
class="w-full p-3 border border-gray-300 rounded-lg shadow-sm focus:ring focus:ring-blue-300 focus:outline-none"
7081
required
@@ -112,9 +123,7 @@
112123

113124
<!-- Expiry Date Input -->
114125
<div>
115-
<label for="expiry" class="block text-gray-700 font-medium mb-2"
116-
>Expiry Date & Time</label
117-
>
126+
<label for="expiry" class="block text-gray-700 font-medium mb-2">Expiry Date & Time</label>
118127
<input
119128
id="expiry"
120129
type="datetime-local"

0 commit comments

Comments
 (0)