Skip to content

Commit

Permalink
Merge pull request #2161 from acterglobal/kumar/general-fixes
Browse files Browse the repository at this point in the history
Add Link : Fixes issue related to url prefix
  • Loading branch information
gnunicorn authored Sep 10, 2024
2 parents d8fecd6 + 115a5c0 commit 84e92b5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
11 changes: 8 additions & 3 deletions app/lib/common/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ final idMatrixRegexp = RegExp(
r'matrix:roomid/(?<id>[^?]+)(\?via=(?<server_name>[^&]+))?(&via=(?<server_name2>[^&]+))?(&via=(?<server_name3>[^&]+))?',
);

final urlValidatorRegexp = RegExp(
r'^[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b[-a-zA-Z0-9()@:%_+.~#?&/=]*$',
);
bool isValidUrl(String url) {
// Regular expression to validate URLs
final RegExp urlPattern = RegExp(
r"^([a-zA-Z][a-zA-Z\d+\-.]*):\/\/([\w\-])+\.{1}([a-zA-Z]{2,63})([\w\-\._~:/?#[\]@!\$&'()*+,;=.]+)?$",
caseSensitive: false,
);
return urlPattern.hasMatch(url);
}

/// Get provider right from the context no matter where we are
extension Context on BuildContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ class _PinLinkBottomSheet extends ConsumerState<LinkBottomSheet> {
final _formKey = GlobalKey<FormState>();
final _titleController = TextEditingController();
final _linkController = TextEditingController();
final prefixText = 'https://';

@override
void initState() {
super.initState();
_titleController.text = widget.pinTitle ?? '';
_linkController.text = (widget.pinLink ?? '').replaceAll(prefixText, '');
}

@override
Expand Down Expand Up @@ -105,7 +103,7 @@ class _PinLinkBottomSheet extends ConsumerState<LinkBottomSheet> {
// Need to update change of tile
widget.onSave(
_titleController.text.trim(),
'$prefixText${_linkController.text.trim()}',
_linkController.text.trim(),
);
},
child: Text(L10n.of(context).save),
Expand Down Expand Up @@ -150,11 +148,10 @@ class _PinLinkBottomSheet extends ConsumerState<LinkBottomSheet> {
controller: _linkController,
minLines: 1,
maxLines: 1,
decoration: InputDecoration(prefixText: prefixText),
validator: (value) {
if (value == null || value.trim().isEmpty) {
return L10n.of(context).pleaseEnterALink;
} else if (!urlValidatorRegexp.hasMatch(value)) {
} else if (!isValidUrl(value)) {
return L10n.of(context).pleaseEnterAValidLink;
}
return null;
Expand Down
42 changes: 42 additions & 0 deletions app/test/features/general/url_validation_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:acter/common/utils/utils.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('URL Validation Tests', () {
test('Valid URL with http', () {
expect(isValidUrl('http://example.com'), true);
});

test('Valid URL with https', () {
expect(isValidUrl('https://example.com'), true);
});

test('Valid URL with custom schema', () {
expect(isValidUrl('custom://example.com'), true);
});

test('Valid URL with subdomains', () {
expect(isValidUrl('https://sub.example.com'), true);
});

test('Valid URL with query parameters', () {
expect(isValidUrl('https://example.com?query=flutter'), true);
});

test('Invalid URL without scheme', () {
expect(isValidUrl('example.com'), false);
});

test('Invalid URL with invalid characters', () {
expect(isValidUrl('https://example!.com'), false);
});

test('Invalid URL with missing domain', () {
expect(isValidUrl('https://.com'), false);
});

test('Empty URL string', () {
expect(isValidUrl(''), false);
});
});
}

0 comments on commit 84e92b5

Please sign in to comment.