Skip to content

Commit dfb3e43

Browse files
Fix loose URL not being parsed if the text have a non loose URL (#42)
1 parent e637942 commit dfb3e43

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

lib/src/url.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,9 @@ class UrlLinkifier extends Linkifier {
2626

2727
elements.forEach((element) {
2828
if (element is TextElement) {
29-
var loose = false;
30-
var match = _urlRegex.firstMatch(element.text);
31-
32-
if (match == null && options.looseUrl) {
33-
match = _looseUrlRegex.firstMatch(element.text);
34-
loose = true;
35-
}
29+
var match = options.looseUrl
30+
? _looseUrlRegex.firstMatch(element.text)
31+
: _urlRegex.firstMatch(element.text);
3632

3733
if (match == null) {
3834
list.add(element);
@@ -55,7 +51,7 @@ class UrlLinkifier extends Linkifier {
5551

5652
var url = originalUrl;
5753

58-
if (loose || !originalUrl.startsWith(_protocolIdentifierRegex)) {
54+
if (!originalUrl.startsWith(_protocolIdentifierRegex)) {
5955
originalUrl = (options.defaultToHttps ? "https://" : "http://") +
6056
originalUrl;
6157
}

test/linkify_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,32 @@ void main() {
167167
);
168168
});
169169

170+
test('Parses both loose and not URL on the same text', () {
171+
expectListEqual(
172+
linkify('example.com http://example.com',
173+
options: LinkifyOptions(looseUrl: true)),
174+
[
175+
UrlElement('http://example.com', 'example.com'),
176+
TextElement(' '),
177+
UrlElement('http://example.com', 'example.com')
178+
],
179+
);
180+
181+
expectListEqual(
182+
linkify(
183+
'This text mixes both loose urls like example.com and not loose urls like http://example.com and http://another.example.com',
184+
options: LinkifyOptions(looseUrl: true)),
185+
[
186+
TextElement('This text mixes both loose urls like '),
187+
UrlElement('http://example.com', 'example.com'),
188+
TextElement(' and not loose urls like '),
189+
UrlElement('http://example.com', 'example.com'),
190+
TextElement(' and '),
191+
UrlElement('http://another.example.com', 'another.example.com')
192+
],
193+
);
194+
});
195+
170196
test('Parses ending period', () {
171197
expectListEqual(
172198
linkify("https://example.com/test."),

0 commit comments

Comments
 (0)