MCPcopy Create free account
hub / github.com/cnodejs/nodeclub / _EncodeEmailAddress

Function _EncodeEmailAddress

public/libs/showdown.js:1295–1347  ·  view source on GitHub ↗
(addr)

Source from the content-addressed store, hash-verified

1293
1294
1295var _EncodeEmailAddress = function(addr) {
1296//
1297// Input: an email address, e.g. "foo@example.com"
1298//
1299// Output: the email address as a mailto link, with each character
1300// of the address encoded as either a decimal or hex entity, in
1301// the hopes of foiling most address harvesting spam bots. E.g.:
1302//
1303// <a href="&#x6D;&#97;&#105;&#108;&#x74;&#111;:&#102;&#111;&#111;&#64;&#101;
1304// x&#x61;&#109;&#x70;&#108;&#x65;&#x2E;&#99;&#111;&#109;">&#102;&#111;&#111;
1305// &#64;&#101;x&#x61;&#109;&#x70;&#108;&#x65;&#x2E;&#99;&#111;&#109;</a>
1306//
1307// Based on a filter by Matthew Wickline, posted to the BBEdit-Talk
1308// mailing list: <http://tinyurl.com/yu7ue>
1309//
1310
1311 // attacklab: why can't javascript speak hex?
1312 function char2hex(ch) {
1313 var hexDigits = '0123456789ABCDEF';
1314 var dec = ch.charCodeAt(0);
1315 return(hexDigits.charAt(dec>>4) + hexDigits.charAt(dec&15));
1316 }
1317
1318 var encode = [
1319 function(ch){return "&#"+ch.charCodeAt(0)+";";},
1320 function(ch){return "&#x"+char2hex(ch)+";";},
1321 function(ch){return ch;}
1322 ];
1323
1324 addr = "mailto:" + addr;
1325
1326 addr = addr.replace(/./g, function(ch) {
1327 if (ch == "@") {
1328 // this *must* be encoded. I insist.
1329 ch = encode[Math.floor(Math.random()*2)](ch);
1330 } else if (ch !=":") {
1331 // leave ':' alone (to spot mailto: later)
1332 var r = Math.random();
1333 // roughly 10% raw, 45% hex, 45% dec
1334 ch = (
1335 r > .9 ? encode[2](ch) :
1336 r > .45 ? encode[1](ch) :
1337 encode[0](ch)
1338 );
1339 }
1340 return ch;
1341 });
1342
1343 addr = "<a href=\"" + addr + "\">" + addr + "</a>";
1344 addr = addr.replace(/">.+:/g,"\">"); // strip the mailto: from the visible part
1345
1346 return addr;
1347}
1348
1349
1350var _UnescapeSpecialChars = function(text) {

Callers 1

_DoAutoLinksFunction · 0.85

Calls 1

char2hexFunction · 0.85

Tested by

no test coverage detected