-
Notifications
You must be signed in to change notification settings - Fork 924
feat: allow to override link validation check, and accept mailto and other links by default #2525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
82a6673
fix: improve customLinkPrefixes doc comment, allows to override link …
EchoEllet 02d4380
revert: AutoFormatMultipleLinksRule test aginst the old valid links
EchoEllet 18b5a15
docs(changelog): document the changes with the PR link
EchoEllet 2d93ca2
chore: minor change to a TODO
EchoEllet 5d91376
chore: address https://github.com/singerdmx/flutter-quill/pull/2525#…
EchoEllet 51f3384
test: adds unit and widget tests
EchoEllet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
@internal | ||
library; | ||
|
||
import 'package:meta/meta.dart'; | ||
|
||
/// {@template link_validation_callback} | ||
/// A callback to validate whether the [link] is valid. | ||
/// | ||
/// The [link] is passed to the callback, which should return `true` if valid, | ||
/// or `false` otherwise. | ||
/// | ||
/// Example: | ||
/// | ||
/// ```dart | ||
/// validateLink: (link) { | ||
/// if (link.startsWith('ws')) { | ||
/// return true; // WebSocket links are considered valid | ||
/// } | ||
/// final regex = RegExp(r'^(http|https)://[a-zA-Z0-9.-]+'); | ||
/// return regex.hasMatch(link); | ||
/// } | ||
/// ``` | ||
/// | ||
/// Return `null` to fallback to the default handling: | ||
/// | ||
/// ```dart | ||
/// validateLink: (link) { | ||
/// if (link.startsWith('custom')) { | ||
/// return true; | ||
/// } | ||
/// return null; | ||
/// } | ||
/// ``` | ||
/// | ||
/// Another example to allow inserting any link: | ||
/// | ||
/// ```dart | ||
/// validateLink: (link) { | ||
/// // Treats all links as valid. When launching the URL, | ||
/// // `https://` is prefixed if the link is incomplete (e.g., `google.com` → `https://google.com`) | ||
/// // however this happens only within the editor level and the | ||
/// // the URL will be stored as: | ||
/// // {insert: ..., attributes: {link: google.com}} | ||
/// return true; | ||
/// } | ||
/// ``` | ||
/// | ||
/// NOTE: The link will always be considered invalid if empty, and this callback will | ||
/// not be called. | ||
/// | ||
/// {@endtemplate} | ||
typedef LinkValidationCallback = bool? Function(String link); | ||
|
||
abstract final class LinkValidator { | ||
static const linkPrefixes = [ | ||
'mailto:', // email | ||
'tel:', // telephone | ||
'sms:', // SMS | ||
'callto:', | ||
'wtai:', | ||
'market:', | ||
'geopoint:', | ||
'ymsgr:', | ||
'msnim:', | ||
'gtalk:', // Google Talk | ||
'skype:', | ||
'sip:', // Lync | ||
'whatsapp:', | ||
'http://', | ||
'https://' | ||
]; | ||
|
||
static bool validate( | ||
String link, { | ||
LinkValidationCallback? customValidateLink, | ||
RegExp? legacyRegex, | ||
List<String>? legacyAddationalLinkPrefixes, | ||
}) { | ||
if (link.trim().isEmpty) { | ||
return false; | ||
} | ||
if (customValidateLink != null) { | ||
final isValid = customValidateLink(link); | ||
if (isValid != null) { | ||
return isValid; | ||
} | ||
} | ||
// Implemented for backward compatibility, clients should use validateLink instead. | ||
// ignore: deprecated_member_use_from_same_package | ||
final legacyRegexp = legacyRegex; | ||
if (legacyRegexp?.hasMatch(link) == true) { | ||
return true; | ||
} | ||
// Implemented for backward compatibility, clients should use validateLink instead. | ||
return (linkPrefixes + (legacyAddationalLinkPrefixes ?? [])) | ||
.any((prefix) => link.startsWith(prefix)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.