Skip to content

Commit

Permalink
Changes to link destination parsing (spec 0.29)
Browse files Browse the repository at this point in the history
* Allow spaces inside link destinations in pointy brackets
* Disallow link destination beginning with `<` unless it is inside `<..>`
  • Loading branch information
robinst committed Apr 12, 2019
1 parent e368db3 commit ccff691
Showing 1 changed file with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class InlineParserImpl implements InlineParser, ReferenceParser {
'|' +
"\\((" + ESCAPED_CHAR + "|[^)\\x00])*\\))");

private static final Pattern LINK_DESTINATION_BRACES = Pattern.compile("^(?:[<](?:[^<> \\t\\n\\\\]|\\\\.)*[>])");
private static final Pattern LINK_DESTINATION_BRACES = Pattern.compile(
"^(?:[<](?:[^<>\n\\\\\\x00]|\\\\.)*[>])");

private static final Pattern LINK_LABEL = Pattern.compile(
"^\\[(?:[^\\\\\\[\\]]|\\\\.){0,1000}\\]");
Expand Down Expand Up @@ -190,7 +191,7 @@ public int parseReference(String s) {
this.input = s;
this.index = 0;
String dest;
String title;
String title = null;
int matchChars;
int startIndex = index;

Expand All @@ -212,13 +213,15 @@ public int parseReference(String s) {
spnl();

dest = parseLinkDestination();
if (dest == null || dest.length() == 0) {
if (dest == null) {
return 0;
}

int beforeTitle = index;
spnl();
title = parseLinkTitle();
if (index != beforeTitle) {
title = parseLinkTitle();
}
if (title == null) {
// rewind before spaces
index = beforeTitle;
Expand Down Expand Up @@ -637,6 +640,9 @@ private String parseLinkDestination() {
return Escaping.unescapeString(res.substring(1, res.length() - 1));
}
} else {
if (peek() == '<') {
return null;
}
int startIndex = index;
if (parseLinkDestinationWithBalancedParens()) {
return Escaping.unescapeString(input.substring(startIndex, index));
Expand All @@ -647,12 +653,14 @@ private String parseLinkDestination() {
}

private boolean parseLinkDestinationWithBalancedParens() {
int startIndex = index;
int parens = 0;
while (true) {
char c = peek();
switch (c) {
case '\0':
return true;
case ' ':
return startIndex != index;
case '\\':
// check if we have an escapable character
if (index + 1 < input.length() && ESCAPABLE.matcher(input.substring(index + 1, index + 2)).matches()) {
Expand All @@ -676,13 +684,10 @@ private boolean parseLinkDestinationWithBalancedParens() {
parens--;
}
break;
case ' ':
// ASCII space
return true;
default:
// or control character
if (Character.isISOControl(c)) {
return true;
return startIndex != index;
}
}
index++;
Expand Down

0 comments on commit ccff691

Please sign in to comment.