Last active
February 5, 2022 17:37
-
-
Save LucGosso/a07c2d5dc4cade33393eb97b182cc25a to your computer and use it in GitHub Desktop.
Mailto email obfuscation helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text.RegularExpressions; | |
namespace Gosso.Mvc.Helpers | |
{ | |
public class MailToHelper | |
{ | |
private static readonly string replaceTemplate = @"#"" onclick=""window.location = 'mailto:' + ['{0}'].join(''); return false;"; | |
public static string TransformMailToLinks(string text) | |
{ | |
Regex regex = new Regex(@"mailto:([^""]+)"); | |
var matches = regex.Matches(text); | |
if (matches.Count > 0) | |
{ | |
foreach (Match match in matches) | |
{ | |
var email = match.Groups[1]?.Value; | |
var emailAfter = string.Join("','", email.ToCharArray()); | |
Regex regReplace = new Regex(match.Value.Replace("?",@"\?")); | |
text = regReplace.Replace(text, string.Format(replaceTemplate, emailAfter), 1);//only replace first occurancy | |
} | |
} | |
//now replace any email in plain text | |
regex = new Regex(@"([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)"); | |
matches = regex.Matches(text); | |
if (matches.Count > 0) | |
{ | |
foreach (Match match in matches) | |
{ | |
var email = match.Groups[1]?.Value; | |
text = text.Replace(email, email.Replace("@", @" [at] ")); | |
} | |
} | |
return text; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Gosso.Mvc.Helpers; | |
using Xunit; | |
namespace XUnitTest | |
{ | |
public class MailtoReplaceTests | |
{ | |
[Fact] | |
public void MailtoTest() | |
{ | |
var testStr = @"<p><a href=""mailto:[email protected]""> test | |
whateva <a href=""mailto:[email protected]?subject=title of subject""> whateva <a href=""https://www.secondo.uk.com/"">test</a> | |
whateva <a href=""mailto:[email protected]""> sdf</p>"; | |
var output = MailToHelper.TransformMailToLinks(testStr); | |
Assert.Contains("['l','o','u','i','s','@','t','r','e','s','.','s','e']", output); | |
} | |
[Fact] | |
public void MailtoTest2() | |
{ | |
var testStr = @"test <a href=""mailto:[email protected]""> whateva | |
whateva <a href=""mailto:[email protected]?test=yes""> whateva | |
whateva <a href=""mailto:[email protected]""> whateva"; | |
var output = MailToHelper.TransformMailToLinks(testStr); | |
Assert.StartsWith(@"test <a href=""#"" onclick=""window.location = 'mailto:' + ['l','u','c','@','f','i','r','s','t','.','s','e'].join(''); return false;"">", output); | |
} | |
[Fact] | |
public void MailtoTest3() | |
{ | |
var testStr = @"test <a href=""mailto:[email protected]"">[email protected]</a> test | |
whateva <a href=""mailto:[email protected]?test=yes""> whateva | |
whateva <a href=""mailto:[email protected]""> whateva"; | |
var output = MailToHelper.TransformMailToLinks(testStr); | |
Assert.StartsWith(@"test <a href=""#"" onclick=""window.location = 'mailto:' + ['l','u','c','@','f','i','r','s','t','.','s','e'].join(''); return false;"">", output); | |
Assert.Contains(@"luc [at] first.se</a> test", output); | |
} | |
[Fact] | |
public void Mailto_second_and_third_formatchange() | |
{ | |
var testStr = @"<p><a href=""mailto:[email protected]""> whateva | |
whateva sdfdfs secondo <a href=""mailto:[email protected]?test=yes""> test <a href=""https://www.secondo.uk.com/"">test</a> | |
tres <a href=""mailto:[email protected]""> whateva</p>"; | |
var output = MailToHelper.TransformMailToLinks(testStr); | |
Assert.Contains(@"secondo <a href=""#"" onclick=""window.location = 'mailto:'", output); | |
Assert.Contains(@"tres <a href=""#"" onclick=""window.location = 'mailto:'", output); | |
} | |
[Fact] | |
public void Mailto_second_and_third_sameAddress() | |
{ | |
var testStr = @"<p><a href=""mailto:[email protected]""> whateva </a> | |
whateva whateva secondo <a href=""mailto:[email protected]?test=yes""/> test <a href=""https://www.secondo.uk.com/"">test</a> | |
tres <a href=""mailto:[email protected]""/> sdf</a></p>"; | |
var output = MailToHelper.TransformMailToLinks(testStr); | |
Assert.Contains(@"<a href=""#"" onclick=""window.location = 'mailto:' + ['l','u','c','@','f','i','r','s','t','.','s','e'].join", output); | |
Assert.Contains(@"'l','u','c','@','f','i','r','s','t','.','s','e','?','t','e','s','t','=','y','e','s'].join('')", output); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment