Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/markdown/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
discarded are never allocated.
* Reuse HTML escape converters and skip HTML entity matching for strings without ampersands, reducing allocations during inline and code-block parsing.
* Sort footnote definitions by their original case-insensitive labels rather than URI-encoded HTML IDs, preserving first-reference order for Unicode and escaped labels.
* Preserve existing percent-encoded sequences in extended autolink destinations instead of double-encoding their percent signs; still escape invalid percent sequences.
* Skip the regular expression in `EmojiSyntax`, `AutolinkSyntax`,
`CodeSyntax`, and `ColorSwatchSyntax` when the character at the current
position cannot start a match. This makes inline parsing faster, mostly on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class AutolinkExtensionSyntax extends InlineSyntax {
: super('($_linkPattern)|($_emailPattern)', caseSensitive: false);

static final _trailingEntityPattern = RegExp(r'&[0-9a-z]+;$');
static final _percentEscapePattern = RegExp('%[0-9A-Fa-f]{2}');

static final _emailOnly = RegExp(
_emailPattern,
Expand Down Expand Up @@ -122,7 +123,7 @@ class AutolinkExtensionSyntax extends InlineSyntax {
}

final anchor = Element.text('a', text)
..attributes['href'] = Uri.encodeFull(destination);
..attributes['href'] = _encodeHref(destination);

parser
..addNode(anchor)
Expand All @@ -131,6 +132,17 @@ class AutolinkExtensionSyntax extends InlineSyntax {
return true;
}

/// Preserve existing percent escapes. Encoding the whole URI would turn
/// an already escaped percent sign into a double-encoded URL.
static String _encodeHref(String destination) {
if (!destination.contains('%')) return Uri.encodeFull(destination);
return destination.splitMapJoin(
_percentEscapePattern,
onMatch: (match) => match.match,
onNonMatch: Uri.encodeFull,
);
}

int _getConsumeLength(String text) {
var excludedLength = 0;

Expand Down
8 changes: 8 additions & 0 deletions pkgs/markdown/test/extensions/autolink_extension.unit
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ https://example.com and ftp://example.com and awww.example.com and foo+bar-baz.q
/www.a.b@c.d and /foo@bar.com and xhttp://a.b
<<<
<p>/<a href="mailto:www.a.b@c.d">www.a.b@c.d</a> and /<a href="mailto:foo@bar.com">foo@bar.com</a> and xhttp://a.b</p>
>>> percent-encoded characters are not double-encoded
https://example.com/%40foo?q=%26 and www.example.com/%e2%82%AC
<<<
<p><a href="https://example.com/%40foo?q=%26">https://example.com/%40foo?q=%26</a> and <a href="http://www.example.com/%e2%82%AC">www.example.com/%e2%82%AC</a></p>
>>> invalid percent-encoding and other characters are still encoded
https://example.com/%zz%4/%%41/fü
<<<
<p><a href="https://example.com/%25zz%254/%25%41/f%C3%BC">https://example.com/%zz%4/%%41/fü</a></p>
Loading