Fixed #490: not-closed snippet no longer makes the application crash.

This commit is contained in:
Dario Solera 2010-02-17 11:14:00 +00:00
parent cff84ba163
commit 02b6c862d6
2 changed files with 14 additions and 7 deletions

View file

@ -16,5 +16,5 @@ using System.Reflection;
// //
// You can specify all the values or you can default the Revision and Build Numbers // You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("3.0.2.501")] [assembly: AssemblyVersion("3.0.2.502")]
[assembly: AssemblyFileVersion("3.0.2.501")] [assembly: AssemblyFileVersion("3.0.2.502")]

View file

@ -48,7 +48,7 @@ namespace ScrewTurn.Wiki {
private static readonly Regex HRRegex = new Regex(@"(?<=(\n|^))(\ )*----(\ )*\n", RegexOptions.Compiled); private static readonly Regex HRRegex = new Regex(@"(?<=(\n|^))(\ )*----(\ )*\n", RegexOptions.Compiled);
//private static readonly Regex SnippetRegex = new Regex(@"\{S(\:|\|)(.+?)(\|(.+?))*}", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); //private static readonly Regex SnippetRegex = new Regex(@"\{S(\:|\|)(.+?)(\|(.+?))*}", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
private static readonly Regex SnippetRegex = new Regex(@"\{s\:(.+?)(\|.*?)*\}", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Singleline); private static readonly Regex SnippetRegex = new Regex(@"\{s\:(.+?)(\|.*?)*\}", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Singleline);
private static readonly Regex ClassicSnippetVerifier = new Regex(@"\|\ ?[a-z0-9]+\ ?\=", RegexOptions.Compiled | RegexOptions.IgnoreCase); private static readonly Regex ClassicSnippetVerifier = new Regex(@"\|\ *[\w\d]+\ *\=", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex TableRegex = new Regex(@"\{\|(\ [^\n]*)?\n.+?\|\}", RegexOptions.Compiled | RegexOptions.Singleline); private static readonly Regex TableRegex = new Regex(@"\{\|(\ [^\n]*)?\n.+?\|\}", RegexOptions.Compiled | RegexOptions.Singleline);
private static readonly Regex IndentRegex = new Regex(@"(?<=(\n|^))\:+(\ )?.+?\n", RegexOptions.Compiled); private static readonly Regex IndentRegex = new Regex(@"(?<=(\n|^))\:+(\ )?.+?\n", RegexOptions.Compiled);
private static readonly Regex EscRegex = new Regex(@"\<esc\>(.|\n|\r)*?\<\/esc\>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Singleline); private static readonly Regex EscRegex = new Regex(@"\<esc\>(.|\n|\r)*?\<\/esc\>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Singleline);
@ -786,10 +786,17 @@ namespace ScrewTurn.Wiki {
// If the snippet is malformed this can explode // If the snippet is malformed this can explode
balanced = ExpandToBalanceBrackets(sb, match.Index, match.Value); balanced = ExpandToBalanceBrackets(sb, match.Index, match.Value);
} }
catch { catch { }
balanced = match.Value;
if(balanced == null) {
// Replace brackets with escaped values so that the snippets regex does not trigger anymore
sb.Replace("{", "&#123;", match.Index, match.Length);
sb.Replace("}", "&#125;", match.Index, match.Length);
break; // Give up
} }
else {
sb.Remove(match.Index, balanced.Length); sb.Remove(match.Index, balanced.Length);
}
if(balanced.IndexOf("}") == balanced.Length - 1) { if(balanced.IndexOf("}") == balanced.Length - 1) {
// Single-level snippet // Single-level snippet
@ -808,7 +815,7 @@ namespace ScrewTurn.Wiki {
lastOpen = balanced.LastIndexOf("{"); lastOpen = balanced.LastIndexOf("{");
firstClosedAfterLastOpen = balanced.IndexOf("}", lastOpen + 1); firstClosedAfterLastOpen = balanced.IndexOf("}", lastOpen + 1);
if(firstClosedAfterLastOpen <= lastOpen) break; // Give up if(lastOpen < 0 || firstClosedAfterLastOpen <= lastOpen) break; // Give up
string internalSnippet = balanced.Substring(lastOpen, firstClosedAfterLastOpen - lastOpen + 1); string internalSnippet = balanced.Substring(lastOpen, firstClosedAfterLastOpen - lastOpen + 1);
balanced = balanced.Remove(lastOpen, firstClosedAfterLastOpen - lastOpen + 1); balanced = balanced.Remove(lastOpen, firstClosedAfterLastOpen - lastOpen + 1);