Skip to content

Commit

Permalink
No longer allow reference labels that only contain whitespaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
Knagis committed Jun 25, 2015
1 parent 3286b03 commit 7a8c040
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CommonMark/Parser/InlineMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,9 @@ public static int ParseReference(Subject subj)
if (lab == null || lab.Value.Length > Reference.MaximumReferenceLabelLength)
goto INVALID;

if (!Scanner.HasNonWhitespace(lab.Value))
goto INVALID;

// colon:
if (peek_char(subj) == ':')
subj.Position++;
Expand Down
22 changes: 21 additions & 1 deletion CommonMark/Parser/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public static int scan_spacechars(string s, int pos, int sourceLength)

for (var i = pos; i < sourceLength; i++)
{
if (s[i] != ' ' && s[i] != '\n')
if (!Utilities.IsWhitespace(s[i]))
return i - pos;
}

Expand Down Expand Up @@ -611,5 +611,25 @@ public static int scan_entity(string s, int pos, int length, out string namedEnt

return 0;
}

/// <summary>
/// Determines if the given string has non-whitespace characters in it
/// </summary>
public static bool HasNonWhitespace(Syntax.StringPart part)
{
var s = part.Source;
var i = part.StartIndex;
var l = i + part.Length;

while (i < l)
{
if (!Utilities.IsWhitespace(s[i]))
return true;

i++;
}

return false;
}
}
}
2 changes: 1 addition & 1 deletion CommonMark/Parser/ScannerCharacterMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal static class ScannerCharacterMatcher
internal static bool MatchWhitespaces(string data, ref char currentCharacter, ref int currentPosition, int lastPosition)
{
var matched = false;
while ((currentCharacter == ' ' || currentCharacter == '\n') && currentPosition < lastPosition)
while (Utilities.IsWhitespace(currentCharacter) && currentPosition < lastPosition)
{
currentCharacter = data[++currentPosition];
matched = true;
Expand Down
8 changes: 8 additions & 0 deletions CommonMark/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public static bool IsAsciiLetterOrDigit(char c)
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
}

#if OptimizeFor45
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
public static bool IsWhitespace(char c)
{
return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f';
}

/// <summary>
/// Checks if the given character is an Unicode space or punctuation character.
/// </summary>
Expand Down

0 comments on commit 7a8c040

Please sign in to comment.