Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit bdbaf76

Browse files
authored
Remove lookarounds from autolink extension patterns (#519)
* Remove lookarounds from autolink extension patterns * Fix some spelling * Remove negative lookahead from email link pattern * Fix some review requests.
1 parent eb09fac commit bdbaf76

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 7.0.1-dev
22

3+
* Remove RegExp lookarounds from autolink extension patterns.
4+
35
## 7.0.0
46

57
* **Breaking change**: `close()` of `DelimiterSyntax` and `LinkSyntax`

lib/src/inline_syntaxes/autolink_extension_syntax.dart

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class AutolinkExtensionSyntax extends InlineSyntax {
1313
static const _linkPattern =
1414
// Autolinks can only come at the beginning of a line, after whitespace,
1515
// or any of the delimiting characters *, _, ~, and (.
16-
r'(?<=^|[\s*_~(>])'
16+
// Note: Disable this piece for now, as Safari does not support
17+
// lookarounds. Consider re-enabling later.
18+
// r'(?<=^|[\s*_~(>])'
1719

1820
// An extended url autolink will be recognised when one of the schemes
1921
// http://, or https://, followed by a valid domain. See
@@ -35,12 +37,14 @@ class AutolinkExtensionSyntax extends InlineSyntax {
3537
// not be considered part of the autolink, though they may be included in
3638
// the interior of the link. See
3739
// https://github.github.com/gfm/#extended-autolink-path-validation.
38-
'(?<![?!.,:*_~])';
40+
// Note: Do not use negative lookbehind, as Safari does not support it.
41+
// '(?<![?!.,:*_~])'
42+
r'[^\s<?!.,:*_~]';
3943

4044
// An extended email autolink, see
4145
// https://github.github.com/gfm/#extended-email-autolink.
4246
static const _emailPattern =
43-
r'[-_.+a-z0-9]+@(?:[-_a-z0-9]+\.)+[-_a-z0-9]*[a-z0-9](?![-_])';
47+
r'[-_.+a-z0-9]+@(?:[-_a-z0-9]+\.)+[-_a-z0-9]*[a-z0-9]';
4448

4549
AutolinkExtensionSyntax()
4650
: super(
@@ -55,6 +59,28 @@ class AutolinkExtensionSyntax extends InlineSyntax {
5559
if (startMatch == null) {
5660
return false;
5761
}
62+
63+
// When it is a link and it is not preceded by `*`, `_`, `~`, `(`, or `>`,
64+
// it is invalid. See
65+
// https://github.github.com/gfm/#extended-autolink-path-validation.
66+
if (startMatch[1] != null && parser.pos > 0) {
67+
final precededBy = String.fromCharCode(parser.charAt(parser.pos - 1));
68+
const validPrecedingChars = {' ', '*', '_', '~', '(', '>'};
69+
if (validPrecedingChars.contains(precededBy) == false) {
70+
return false;
71+
}
72+
}
73+
74+
// When it is an email link and followed by `_` or `-`, it is invalid. See
75+
// https://github.github.com/gfm/#example-633
76+
if (startMatch[2] != null && parser.source.length > startMatch.end) {
77+
final followedBy = String.fromCharCode(parser.charAt(startMatch.end));
78+
const invalidFollowingChars = {'_', '-'};
79+
if (invalidFollowingChars.contains(followedBy)) {
80+
return false;
81+
}
82+
}
83+
5884
parser.writeText();
5985
return onMatch(parser, startMatch);
6086
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
>>> not a link
2+
mhttp://www.foo.com
3+
<<<
4+
<p>mhttp://www.foo.com</p>

test/markdown_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ void main() async {
4141
'extensions/unordered_list_with_checkboxes.unit',
4242
blockSyntaxes: [const UnorderedListWithCheckboxSyntax()],
4343
);
44+
testFile(
45+
'extensions/autolink_extension.unit',
46+
inlineSyntaxes: [AutolinkExtensionSyntax()],
47+
);
4448

4549
// Inline syntax extensions
4650
testFile(

0 commit comments

Comments
 (0)