diff --git a/example/lib/main.dart b/example/lib/main.dart index 145e615..e2748df 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -3,24 +3,28 @@ import 'package:google_fonts/google_fonts.dart'; import 'package:linkfy_text/linkfy_text.dart'; void main() { - runApp(MyApp()); + runApp(const MyApp()); } final k = GlobalKey(); class MyApp extends StatelessWidget { + const MyApp({super.key}); + @override Widget build(BuildContext context) { return MaterialApp( title: 'linkfy_text Demo', scaffoldMessengerKey: k, debugShowCheckedModeBanner: false, - home: App(), + home: const App(), ); } } class App extends StatefulWidget { + const App({super.key}); + @override _AppState createState() => _AppState(); } @@ -31,7 +35,8 @@ class _AppState extends State { final List> texts = [ { - "text": "O1. This text contains a url: https://flutter.dev", + "text": + "O1. Testing hyphenated domains: https://my-website.com and http://sub-domain.example-site.com", "types": null }, { @@ -52,7 +57,7 @@ class _AppState extends State { }, { "text": - "O6. My website url: https://hello.com/GOOGLE search using: www.google.com, social media is facebook.com, additional link https://example.com/method?param=fullstackoverflow.dev, hashtag #trending & mention @dev.user +18009999999", + "O6. Testing complex URLs: https://my-complex-domain.com/path?param=value and https://sub-domain.my-site.com/test", "types": [ LinkType.phone, LinkType.email, @@ -66,7 +71,7 @@ class _AppState extends State { void showSnackbar(String msg) { k.currentState!.removeCurrentSnackBar(); k.currentState!.showSnackBar(SnackBar( - content: Text("$msg", style: textStyle), + content: Text(msg, style: textStyle), behavior: SnackBarBehavior.floating, )); } @@ -90,13 +95,13 @@ class _AppState extends State { ), for (var i = 0; i < texts.length; i++) Padding( - padding: EdgeInsets.only(top: 14), + padding: const EdgeInsets.only(top: 14), child: LinkifyText( texts[i]['text'], textAlign: TextAlign.left, linkTypes: texts[i]['types'], textStyle: textStyle, - customLinkStyles: { + customLinkStyles: const { LinkType.email: TextStyle(color: Colors.blue), LinkType.hashTag: TextStyle(color: Colors.green), LinkType.userTag: TextStyle(color: Colors.deepPurple), diff --git a/lib/src/utils/regex.dart b/lib/src/utils/regex.dart index 4693c4d..532d0fd 100644 --- a/lib/src/utils/regex.dart +++ b/lib/src/utils/regex.dart @@ -4,11 +4,13 @@ import 'package:linkfy_text/src/enum.dart'; // url regex that accept https, http, www String urlRegExp = - r'[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)'; + r'((https?://)?(www\.)?[a-zA-Z0-9@:%._\+~#=-]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*))'; -String hashtagRegExp = r'#[a-zA-Z\u00C0-\u01B4\w_\u1EA0-\u1EF9!$%^&]{1,}(?=\s|$)'; +String hashtagRegExp = + r'#[a-zA-Z\u00C0-\u01B4\w_\u1EA0-\u1EF9!$%^&]{1,}(?=\s|$)'; -String userTagRegExp = r'@[a-zA-Z\u00C0-\u01B4\w_\u1EA0-\u1EF9!$%^&]{1,}(?=\s|$)'; +String userTagRegExp = + r'@[a-zA-Z\u00C0-\u01B4\w_\u1EA0-\u1EF9!$%^&]{1,}(?=\s|$)'; String phoneRegExp = r'\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*'; String emailRegExp = @@ -49,7 +51,6 @@ RegExp constructRegExpFromLinkType(List types) { ? buffer.write("($phoneRegExp)") : buffer.write("($phoneRegExp)|"); break; - default: } } return RegExp(buffer.toString()); @@ -61,11 +62,11 @@ LinkType getMatchedType(String match) { type = LinkType.email; } else if (RegExp(urlRegExp).hasMatch(match)) { type = LinkType.url; - }else if (RegExp(phoneRegExp).hasMatch(match)) { + } else if (RegExp(phoneRegExp).hasMatch(match)) { type = LinkType.phone; } else if (RegExp(userTagRegExp).hasMatch(match)) { type = LinkType.userTag; - } else if (RegExp(hashtagRegExp).hasMatch(match)) { + } else if (RegExp(hashtagRegExp).hasMatch(match)) { type = LinkType.hashTag; } return type; diff --git a/test/utils/regex_test.dart b/test/utils/regex_test.dart index a0c07ee..6ec74da 100644 --- a/test/utils/regex_test.dart +++ b/test/utils/regex_test.dart @@ -29,6 +29,10 @@ void main() { "https://domain.com/Google?param=", "https://domain.com/Google?param=helloworld", "https://sub.domain.com/Google?param=helloworld#hash", + "https://my-domain.com", + "http://my-awesome-website.com", + "www.my-site-with-hyphens.com", + "https://sub-domain.example.com" ]; const hashtags = [ @@ -67,7 +71,8 @@ void main() { test("Should match all urls", () { for (final url in urls) { - expect(RegExp(urlRegExp).hasMatch(url), isTrue); + expect(RegExp(urlRegExp).hasMatch(url), isTrue, + reason: "Failed to match URL: $url"); expect(getMatchedType(url), equals(LinkType.url)); } });