private static string RemoveAllTagsIf(string inputHtml, string tagName, HtmlTagFilter @if, bool preserveContents) { return(RemoveFromHtmlIf(inputHtml, "<\\s*{0}\\b[^>]*>".F(Regex.Escape(tagName)), "</\\s*{0}\\b[^>]*>".F(Regex.Escape(tagName)), @if, preserveContents)); }
private static string RemoveFromHtmlIf(string inputHtml, string startReExpr, string endReExpr, HtmlTagFilter @if, bool preserveContents) { Regex startRe = ScriptableEmailFunctions.GetRegex(startReExpr, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); Regex endRe = ScriptableEmailFunctions.GetRegex(endReExpr, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); StringBuilder sb = new StringBuilder(inputHtml.Length); int inputIndex = 0; var startMc = startRe.Matches(inputHtml); foreach (Match startMatch in startMc) { if (@if(startMatch.Value)) { // Include everything up to startMatch sb.Append(inputHtml.Substring(inputIndex, startMatch.Index - inputIndex)); inputIndex = startMatch.Index + startMatch.Length; var endMatch = endRe.Match(inputHtml, startMatch.Index + startMatch.Length); if (endMatch.Success) { if (preserveContents) { // Include everything up to endMatch sb.Append(inputHtml.Substring(inputIndex, endMatch.Index - inputIndex)); inputIndex = endMatch.Index + endMatch.Length; } else { // Do not include anything, just update inputIndex inputIndex = endMatch.Index + endMatch.Length; } } } else { // Include the match sb.Append(inputHtml.Substring(inputIndex, startMatch.Index + startMatch.Length - inputIndex)); inputIndex = startMatch.Index + startMatch.Length; } } // Include the remaining of the inputHtml sb.Append(inputHtml.Substring(inputIndex)); return(sb.ToString()); }