Remove lookarounds from autolink extension patterns (dart-lang/markdown#519)
* Remove lookarounds from autolink extension patterns
* Fix some spelling
* Remove negative lookahead from email link pattern
* Fix some review requests.
diff --git a/pkgs/markdown/CHANGELOG.md b/pkgs/markdown/CHANGELOG.md
index e6883c2..65ffbce 100644
--- a/pkgs/markdown/CHANGELOG.md
+++ b/pkgs/markdown/CHANGELOG.md
@@ -1,5 +1,7 @@
## 7.0.1-dev
+* Remove RegExp lookarounds from autolink extension patterns.
+
## 7.0.0
* **Breaking change**: `close()` of `DelimiterSyntax` and `LinkSyntax`
diff --git a/pkgs/markdown/lib/src/inline_syntaxes/autolink_extension_syntax.dart b/pkgs/markdown/lib/src/inline_syntaxes/autolink_extension_syntax.dart
index 152b9a4..a7f174f 100644
--- a/pkgs/markdown/lib/src/inline_syntaxes/autolink_extension_syntax.dart
+++ b/pkgs/markdown/lib/src/inline_syntaxes/autolink_extension_syntax.dart
@@ -13,7 +13,9 @@
static const _linkPattern =
// Autolinks can only come at the beginning of a line, after whitespace,
// or any of the delimiting characters *, _, ~, and (.
- r'(?<=^|[\s*_~(>])'
+ // Note: Disable this piece for now, as Safari does not support
+ // lookarounds. Consider re-enabling later.
+ // r'(?<=^|[\s*_~(>])'
// An extended url autolink will be recognised when one of the schemes
// http://, or https://, followed by a valid domain. See
@@ -35,12 +37,14 @@
// not be considered part of the autolink, though they may be included in
// the interior of the link. See
// https://github.github.com/gfm/#extended-autolink-path-validation.
- '(?<![?!.,:*_~])';
+ // Note: Do not use negative lookbehind, as Safari does not support it.
+ // '(?<![?!.,:*_~])'
+ r'[^\s<?!.,:*_~]';
// An extended email autolink, see
// https://github.github.com/gfm/#extended-email-autolink.
static const _emailPattern =
- r'[-_.+a-z0-9]+@(?:[-_a-z0-9]+\.)+[-_a-z0-9]*[a-z0-9](?![-_])';
+ r'[-_.+a-z0-9]+@(?:[-_a-z0-9]+\.)+[-_a-z0-9]*[a-z0-9]';
AutolinkExtensionSyntax()
: super(
@@ -55,6 +59,28 @@
if (startMatch == null) {
return false;
}
+
+ // When it is a link and it is not preceded by `*`, `_`, `~`, `(`, or `>`,
+ // it is invalid. See
+ // https://github.github.com/gfm/#extended-autolink-path-validation.
+ if (startMatch[1] != null && parser.pos > 0) {
+ final precededBy = String.fromCharCode(parser.charAt(parser.pos - 1));
+ const validPrecedingChars = {' ', '*', '_', '~', '(', '>'};
+ if (validPrecedingChars.contains(precededBy) == false) {
+ return false;
+ }
+ }
+
+ // When it is an email link and followed by `_` or `-`, it is invalid. See
+ // https://github.github.com/gfm/#example-633
+ if (startMatch[2] != null && parser.source.length > startMatch.end) {
+ final followedBy = String.fromCharCode(parser.charAt(startMatch.end));
+ const invalidFollowingChars = {'_', '-'};
+ if (invalidFollowingChars.contains(followedBy)) {
+ return false;
+ }
+ }
+
parser.writeText();
return onMatch(parser, startMatch);
}
diff --git a/pkgs/markdown/test/extensions/autolink_extension.unit b/pkgs/markdown/test/extensions/autolink_extension.unit
new file mode 100644
index 0000000..8ab4613
--- /dev/null
+++ b/pkgs/markdown/test/extensions/autolink_extension.unit
@@ -0,0 +1,4 @@
+>>> not a link
+mhttp://www.foo.com
+<<<
+<p>mhttp://www.foo.com</p>
\ No newline at end of file
diff --git a/pkgs/markdown/test/markdown_test.dart b/pkgs/markdown/test/markdown_test.dart
index 80d25fa..aa6d9ce 100644
--- a/pkgs/markdown/test/markdown_test.dart
+++ b/pkgs/markdown/test/markdown_test.dart
@@ -41,6 +41,10 @@
'extensions/unordered_list_with_checkboxes.unit',
blockSyntaxes: [const UnorderedListWithCheckboxSyntax()],
);
+ testFile(
+ 'extensions/autolink_extension.unit',
+ inlineSyntaxes: [AutolinkExtensionSyntax()],
+ );
// Inline syntax extensions
testFile(