< Summary

Information
Class: RoleRadar.Application.Tracking.Import.TrackerImportTextSanitizer
Assembly: RoleRadar.Application
File(s): /home/runner/work/roleradar/roleradar/src/RoleRadar.Application/Tracking/Import/TrackerImportTextSanitizer.cs
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 98
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
SanitizeFreeText(...)100%22100%
SanitizePlainText(...)100%22100%

File(s)

/home/runner/work/roleradar/roleradar/src/RoleRadar.Application/Tracking/Import/TrackerImportTextSanitizer.cs

#LineLine coverage
 1using System.Net;
 2using System.Text.RegularExpressions;
 3
 4namespace RoleRadar.Application.Tracking.Import;
 5
 6/// <summary>
 7/// Sanitizes text imported from tracker files (JSON, CSV) by decoding HTML entities,
 8/// converting basic formatting tags (strong/b, em/i, br) to markdown in free text,
 9/// and stripping remaining HTML tags.
 10/// </summary>
 11public static class TrackerImportTextSanitizer
 12{
 813    private static readonly Regex CommentRegex = new(@"<!--.*?-->", RegexOptions.Singleline | RegexOptions.Compiled);
 814    private static readonly Regex ScriptRegex = new(@"<\s*script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\s*/\s*script\s*>", Reg
 815    private static readonly Regex StyleRegex = new(@"<\s*style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\s*/\s*style\s*>", RegexOp
 816    private static readonly Regex BrRegex = new(@"<\s*br\b[^>]*\s*/?>", RegexOptions.IgnoreCase | RegexOptions.Compiled)
 817    private static readonly Regex StrongOpenRegex = new(@"<\s*(?:strong|b)\b[^>]*>", RegexOptions.IgnoreCase | RegexOpti
 818    private static readonly Regex StrongCloseRegex = new(@"</\s*(?:strong|b)\s*>", RegexOptions.IgnoreCase | RegexOption
 819    private static readonly Regex EmOpenRegex = new(@"<\s*(?:em|i)\b[^>]*>", RegexOptions.IgnoreCase | RegexOptions.Comp
 820    private static readonly Regex EmCloseRegex = new(@"</\s*(?:em|i)\s*>", RegexOptions.IgnoreCase | RegexOptions.Compil
 821    private static readonly Regex TagRegex = new(@"</?[a-z][a-z0-9]*\b[^>]*>", RegexOptions.IgnoreCase | RegexOptions.Co
 822    private static readonly Regex NewlineNormalizerRegex = new(@"\r\n|\r", RegexOptions.Compiled);
 23
 24    /// <summary>
 25    /// Decodes HTML entities, converts strong/b to **text**, em/i to *text*, br to \n,
 26    /// and strips any other HTML tags from imported free text (notes, culture, reasons, event notes).
 27    /// </summary>
 28    public static string? SanitizeFreeText(string? value)
 29    {
 73430        if (string.IsNullOrWhiteSpace(value))
 31        {
 16832            return value;
 33        }
 34
 35        // 1. Decode HTML entities first (&amp;, &quot;, &lt;, &gt;, &middot;, &ndash;, &mdash;, &nbsp;, etc.)
 56636        var text = WebUtility.HtmlDecode(value);
 56637        text = text.Replace('\u00A0', ' ');
 38
 39        // 2. Remove HTML comments, scripts, styles
 56640        text = CommentRegex.Replace(text, string.Empty);
 56641        text = ScriptRegex.Replace(text, string.Empty);
 56642        text = StyleRegex.Replace(text, string.Empty);
 43
 44        // 3. Convert <br> variants to newline
 56645        text = BrRegex.Replace(text, "\n");
 46
 47        // 4. Convert <strong> and <b> to **
 56648        text = StrongOpenRegex.Replace(text, "**");
 56649        text = StrongCloseRegex.Replace(text, "**");
 50
 51        // 5. Convert <em> and <i> to *
 56652        text = EmOpenRegex.Replace(text, "*");
 56653        text = EmCloseRegex.Replace(text, "*");
 54
 55        // 6. Strip all other HTML tags
 56656        text = TagRegex.Replace(text, string.Empty);
 57
 58        // 7. Normalize line endings to \n
 56659        text = NewlineNormalizerRegex.Replace(text, "\n");
 60
 56661        return text;
 62    }
 63
 64    /// <summary>
 65    /// Decodes HTML entities and strips all HTML tags (including strong/em/br) without formatting.
 66    /// Used for titles, company names, requisition IDs, URLs, and locations so dedup keys and matching operate on clean
 67    /// </summary>
 68    public static string? SanitizePlainText(string? value)
 69    {
 618670        if (string.IsNullOrWhiteSpace(value))
 71        {
 188072            return value;
 73        }
 74
 75        // 1. Decode HTML entities first
 430676        var text = WebUtility.HtmlDecode(value);
 430677        text = text.Replace('\u00A0', ' ');
 78
 79        // 2. Remove HTML comments, scripts, styles
 430680        text = CommentRegex.Replace(text, string.Empty);
 430681        text = ScriptRegex.Replace(text, string.Empty);
 430682        text = StyleRegex.Replace(text, string.Empty);
 83
 84        // 3. Convert <br> variants to space so words don't concatenate
 430685        text = BrRegex.Replace(text, " ");
 86
 87        // 4. Strip all HTML tags
 430688        text = TagRegex.Replace(text, string.Empty);
 89
 90        // 5. Replace newlines and tabs with space
 430691        text = Regex.Replace(text, @"[\r\n\t]+", " ");
 92
 93        // 6. Collapse multiple consecutive spaces
 430694        text = Regex.Replace(text, @" {2,}", " ");
 95
 430696        return text;
 97    }
 98}