| | | 1 | | using System.Net; |
| | | 2 | | using System.Text.RegularExpressions; |
| | | 3 | | |
| | | 4 | | namespace 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> |
| | | 11 | | public static class TrackerImportTextSanitizer |
| | | 12 | | { |
| | 8 | 13 | | private static readonly Regex CommentRegex = new(@"<!--.*?-->", RegexOptions.Singleline | RegexOptions.Compiled); |
| | 8 | 14 | | private static readonly Regex ScriptRegex = new(@"<\s*script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\s*/\s*script\s*>", Reg |
| | 8 | 15 | | private static readonly Regex StyleRegex = new(@"<\s*style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\s*/\s*style\s*>", RegexOp |
| | 8 | 16 | | private static readonly Regex BrRegex = new(@"<\s*br\b[^>]*\s*/?>", RegexOptions.IgnoreCase | RegexOptions.Compiled) |
| | 8 | 17 | | private static readonly Regex StrongOpenRegex = new(@"<\s*(?:strong|b)\b[^>]*>", RegexOptions.IgnoreCase | RegexOpti |
| | 8 | 18 | | private static readonly Regex StrongCloseRegex = new(@"</\s*(?:strong|b)\s*>", RegexOptions.IgnoreCase | RegexOption |
| | 8 | 19 | | private static readonly Regex EmOpenRegex = new(@"<\s*(?:em|i)\b[^>]*>", RegexOptions.IgnoreCase | RegexOptions.Comp |
| | 8 | 20 | | private static readonly Regex EmCloseRegex = new(@"</\s*(?:em|i)\s*>", RegexOptions.IgnoreCase | RegexOptions.Compil |
| | 8 | 21 | | private static readonly Regex TagRegex = new(@"</?[a-z][a-z0-9]*\b[^>]*>", RegexOptions.IgnoreCase | RegexOptions.Co |
| | 8 | 22 | | 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 | | { |
| | 734 | 30 | | if (string.IsNullOrWhiteSpace(value)) |
| | | 31 | | { |
| | 168 | 32 | | return value; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | // 1. Decode HTML entities first (&, ", <, >, ·, –, —, , etc.) |
| | 566 | 36 | | var text = WebUtility.HtmlDecode(value); |
| | 566 | 37 | | text = text.Replace('\u00A0', ' '); |
| | | 38 | | |
| | | 39 | | // 2. Remove HTML comments, scripts, styles |
| | 566 | 40 | | text = CommentRegex.Replace(text, string.Empty); |
| | 566 | 41 | | text = ScriptRegex.Replace(text, string.Empty); |
| | 566 | 42 | | text = StyleRegex.Replace(text, string.Empty); |
| | | 43 | | |
| | | 44 | | // 3. Convert <br> variants to newline |
| | 566 | 45 | | text = BrRegex.Replace(text, "\n"); |
| | | 46 | | |
| | | 47 | | // 4. Convert <strong> and <b> to ** |
| | 566 | 48 | | text = StrongOpenRegex.Replace(text, "**"); |
| | 566 | 49 | | text = StrongCloseRegex.Replace(text, "**"); |
| | | 50 | | |
| | | 51 | | // 5. Convert <em> and <i> to * |
| | 566 | 52 | | text = EmOpenRegex.Replace(text, "*"); |
| | 566 | 53 | | text = EmCloseRegex.Replace(text, "*"); |
| | | 54 | | |
| | | 55 | | // 6. Strip all other HTML tags |
| | 566 | 56 | | text = TagRegex.Replace(text, string.Empty); |
| | | 57 | | |
| | | 58 | | // 7. Normalize line endings to \n |
| | 566 | 59 | | text = NewlineNormalizerRegex.Replace(text, "\n"); |
| | | 60 | | |
| | 566 | 61 | | 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 | | { |
| | 6186 | 70 | | if (string.IsNullOrWhiteSpace(value)) |
| | | 71 | | { |
| | 1880 | 72 | | return value; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | // 1. Decode HTML entities first |
| | 4306 | 76 | | var text = WebUtility.HtmlDecode(value); |
| | 4306 | 77 | | text = text.Replace('\u00A0', ' '); |
| | | 78 | | |
| | | 79 | | // 2. Remove HTML comments, scripts, styles |
| | 4306 | 80 | | text = CommentRegex.Replace(text, string.Empty); |
| | 4306 | 81 | | text = ScriptRegex.Replace(text, string.Empty); |
| | 4306 | 82 | | text = StyleRegex.Replace(text, string.Empty); |
| | | 83 | | |
| | | 84 | | // 3. Convert <br> variants to space so words don't concatenate |
| | 4306 | 85 | | text = BrRegex.Replace(text, " "); |
| | | 86 | | |
| | | 87 | | // 4. Strip all HTML tags |
| | 4306 | 88 | | text = TagRegex.Replace(text, string.Empty); |
| | | 89 | | |
| | | 90 | | // 5. Replace newlines and tabs with space |
| | 4306 | 91 | | text = Regex.Replace(text, @"[\r\n\t]+", " "); |
| | | 92 | | |
| | | 93 | | // 6. Collapse multiple consecutive spaces |
| | 4306 | 94 | | text = Regex.Replace(text, @" {2,}", " "); |
| | | 95 | | |
| | 4306 | 96 | | return text; |
| | | 97 | | } |
| | | 98 | | } |