Skip to content

Commit 0ac3188

Browse files
authored
fix: allow all correct URLs to be formatted (singerdmx#2328)
1 parent 0b7a373 commit 0ac3188

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

lib/src/rules/insert.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,6 @@ class AutoFormatMultipleLinksRule extends InsertRule {
343343
/// This pattern is used to match a links within a text segment.
344344
///
345345
/// It works for the following testing URLs:
346-
// www.google.com
347346
// http://google.com
348347
// https://www.google.com
349348
// http://beginner.example.edu/#act
@@ -364,9 +363,9 @@ class AutoFormatMultipleLinksRule extends InsertRule {
364363
// URL generator tool (https://www.randomlists.com/urls) is used.
365364

366365
static const _oneLineLinkPattern =
367-
r'^https?:\/\/[\w\-]+(\.[\w\-]+)*(:\d+)?(\/.*)?$';
366+
r'^https?:\/\/[\w\-]+(\.[\w\-]+)*(:\d+)?([\/\?#].*)?$';
368367
static const _detectLinkPattern =
369-
r'https?:\/\/[\w\-]+(\.[\w\-]+)*(:\d+)?(\/[^\s]*)?';
368+
r'https?:\/\/[\w\-]+(\.[\w\-]+)*(:\d+)?([\/\?#][^\s]*)?';
370369

371370
/// It requires a valid link in one link
372371
RegExp get oneLineLinkRegExp => RegExp(

test/rules/insert_test.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,45 @@ void main() {
286286
reason: 'Insertion within link label updates label');
287287
});
288288
});
289+
290+
group('AutoFormatMultipleLinksRule', () {
291+
const rule = AutoFormatMultipleLinksRule();
292+
293+
final validLinks = [
294+
'http://google.com',
295+
'https://www.google.com',
296+
'http://beginner.example.edu/#act',
297+
'http://beginner.example.edu#act',
298+
'https://birth.example.net/beds/ants.php#bait',
299+
'http://example.com/babies',
300+
'https://www.example.com/',
301+
'https://attack.example.edu/?acoustics=blade&bed=bed',
302+
'https://attack.example.edu?acoustics=blade&bed=bed',
303+
'http://basketball.example.com/',
304+
'https://birthday.example.com/birthday',
305+
'http://www.example.com/',
306+
'https://example.com/addition/action',
307+
'http://example.com/',
308+
'https://bite.example.net/#adjustment',
309+
'https://bite.example.net#adjustment',
310+
'http://www.example.net/badge.php?bedroom=anger',
311+
'https://brass.example.com/?anger=branch&actor=amusement#adjustment',
312+
'https://brass.example.com?anger=branch&actor=amusement#adjustment',
313+
'http://www.example.com/?action=birds&brass=apparatus',
314+
'http://www.example.com?action=birds&brass=apparatus',
315+
'https://example.net/',
316+
];
317+
318+
test('Insert link in text', () {
319+
final delta = Delta()..insert('\n');
320+
final document = Document.fromDelta(delta);
321+
322+
for (final link in validLinks) {
323+
expect(
324+
rule.apply(document, 0, data: link, len: 0),
325+
Delta()..insert(link, {'link': link}),
326+
);
327+
}
328+
});
329+
});
289330
}

0 commit comments

Comments
 (0)