Skip to content

Commit 6e9256c

Browse files
committed
fix #2188
1 parent 1d036d3 commit 6e9256c

1 file changed

Lines changed: 38 additions & 19 deletions

File tree

Zero-K.info/ForumParser/ParserExtensions.cs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ namespace ZeroKWeb.ForumParser
77
{
88
public static class ParserExtensions
99
{
10-
public static LinkedListNode<Tag> NextNodeOfType<T>(this LinkedListNode<Tag> node) {
10+
public static LinkedListNode<Tag> NextNodeOfType<T>(this LinkedListNode<Tag> node)
11+
{
1112
while (node != null && !(node.Value is T)) node = node.Next;
1213
return node;
1314
}
1415

1516

16-
public static string GetOriginalContentWhileCondition(this LinkedListNode<Tag> node, Antlr.Runtime.Misc.Func<LinkedListNode<Tag>, bool> whileCondition) {
17+
public static string GetOriginalContentWhileCondition(this LinkedListNode<Tag> node, Antlr.Runtime.Misc.Func<LinkedListNode<Tag>, bool> whileCondition)
18+
{
1719
var sb = new StringBuilder();
1820
while (node != null && whileCondition(node))
1921
{
@@ -23,25 +25,30 @@ public static string GetOriginalContentWhileCondition(this LinkedListNode<Tag> n
2325
return sb.ToString();
2426
}
2527

26-
public static string GetOriginalContentUntilTag<T>(this LinkedListNode<Tag> node) {
28+
public static string GetOriginalContentUntilTag<T>(this LinkedListNode<Tag> node)
29+
{
2730
return node.GetOriginalContentWhileCondition(x => !(x.Value is T));
2831
}
2932

30-
public static string GetOriginalContentUntilNode(this LinkedListNode<Tag> startNode, LinkedListNode<Tag> endNode) {
33+
public static string GetOriginalContentUntilNode(this LinkedListNode<Tag> startNode, LinkedListNode<Tag> endNode)
34+
{
3135
return startNode.GetOriginalContentWhileCondition(x => x != endNode);
3236
}
3337

34-
public static string GetOriginalContentUntilWhiteSpaceOrEndline(this LinkedListNode<Tag> node) {
38+
public static string GetOriginalContentUntilWhiteSpaceOrEndline(this LinkedListNode<Tag> node)
39+
{
3540
return node.GetOriginalContentWhileCondition(x => !(x.Value is SpaceTag) && !(x.Value is NewLineTag));
3641
}
3742

38-
public static LinkedListNode<Tag> TranslateWhile(this LinkedListNode<Tag> startNode, TranslateContext context, Antlr.Runtime.Misc.Func<LinkedListNode<Tag>, bool> condition) {
43+
public static LinkedListNode<Tag> TranslateWhile(this LinkedListNode<Tag> startNode, TranslateContext context, Antlr.Runtime.Misc.Func<LinkedListNode<Tag>, bool> condition)
44+
{
3945
var n = startNode;
4046
while (n != null && condition(n)) n = n.Value.Translate(context, n);
4147
return n;
4248
}
4349

44-
public static LinkedListNode<Tag> FirstNode(this LinkedListNode<Tag> startNode, Antlr.Runtime.Misc.Func<LinkedListNode<Tag>, bool> condition) {
50+
public static LinkedListNode<Tag> FirstNode(this LinkedListNode<Tag> startNode, Antlr.Runtime.Misc.Func<LinkedListNode<Tag>, bool> condition)
51+
{
4552
while (startNode != null)
4653
{
4754
if (condition(startNode)) return startNode;
@@ -50,7 +57,8 @@ public static LinkedListNode<Tag> FirstNode(this LinkedListNode<Tag> startNode,
5057
return null;
5158
}
5259

53-
public static LinkedListNode<Tag> FirstNodeReverse(this LinkedListNode<Tag> startNode, Antlr.Runtime.Misc.Func<LinkedListNode<Tag>, bool> condition) {
60+
public static LinkedListNode<Tag> FirstNodeReverse(this LinkedListNode<Tag> startNode, Antlr.Runtime.Misc.Func<LinkedListNode<Tag>, bool> condition)
61+
{
5462
while (startNode != null)
5563
{
5664
if (condition(startNode)) return startNode;
@@ -60,41 +68,52 @@ public static LinkedListNode<Tag> FirstNodeReverse(this LinkedListNode<Tag> star
6068
}
6169

6270

63-
public static IEnumerable<LinkedListNode<T>> AsEnumerable<T>(this LinkedListNode<T> startNode) {
71+
public static IEnumerable<LinkedListNode<T>> AsEnumerable<T>(this LinkedListNode<T> startNode)
72+
{
6473
return new LinkedListNodeEnumerator<T>(startNode);
6574
}
6675

67-
public static IEnumerable<LinkedListNode<T>> AsReverseEnumerable<T>(this LinkedListNode<T> startNode) {
76+
public static IEnumerable<LinkedListNode<T>> AsReverseEnumerable<T>(this LinkedListNode<T> startNode)
77+
{
6878
return new LinkedListNodeBackEnumerator<T>(startNode);
6979
}
7080

7181

72-
public static LinkedListNode<Tag> TranslateUntilNode(this LinkedListNode<Tag> startNode, TranslateContext context, LinkedListNode<Tag> endNode) {
82+
public static LinkedListNode<Tag> TranslateUntilNode(this LinkedListNode<Tag> startNode, TranslateContext context, LinkedListNode<Tag> endNode)
83+
{
7384
return startNode.TranslateWhile(context, x => x != endNode);
7485
}
7586

7687
static Regex linkMatcher = new Regex("^(mailto|spring|http|https|ftp|ftps|zk)\\://[^\\\"']+$", RegexOptions.Compiled);
7788

78-
public static bool IsValidLink(this string content) {
89+
public static bool IsValidLink(this string content)
90+
{
7991
if (string.IsNullOrEmpty(content)) return false;
8092
return linkMatcher.IsMatch(content);
8193
}
8294

8395
public static bool IsValidLinkOrRelativeUrl(this string content, bool imageOnly)
8496
{
8597
if (string.IsNullOrEmpty(content)) return false;
98+
if (!linkMatcher.IsMatch(content)) return false;
99+
86100
Uri parsed;
87-
if (!linkMatcher.IsMatch(content) || Uri.TryCreate(content, UriKind.Relative, out parsed)) return false;
88-
if (imageOnly)
101+
if (Uri.TryCreate(content, UriKind.RelativeOrAbsolute, out parsed) && parsed != null)
89102
{
90-
Uri.TryCreate(content, UriKind.RelativeOrAbsolute, out parsed);
91-
// for proper check do HEAD request and check response, but this needs some cache
92-
if (!(parsed.AbsolutePath.EndsWith(".jpg") || parsed.AbsolutePath.EndsWith(".gif") || parsed.AbsolutePath.EndsWith(".png") || parsed.AbsolutePath.EndsWith(".jpeg"))) return false;
103+
if (imageOnly)
104+
{
105+
// for proper check do HEAD request and check response, but this needs some cache
106+
if (!(parsed.AbsolutePath.EndsWith(".jpg") || parsed.AbsolutePath?.EndsWith(".gif") == true || parsed.AbsolutePath.EndsWith(".png") ||
107+
parsed.AbsolutePath.EndsWith(".jpeg"))) return false;
108+
}
109+
110+
return true;
93111
}
94-
return true;
112+
return false;
95113
}
96114

97-
public static char ToLower(this char high) {
115+
public static char ToLower(this char high)
116+
{
98117
if (high >= 'A' && high <= 'Z') return (char)(high - 'A' + 'a');
99118
return high;
100119
}

0 commit comments

Comments
 (0)