-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspamspan.js
66 lines (57 loc) · 2.37 KB
/
spamspan.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
--------------------------------------------------------------------------
(c) 2007 Lawrence Akka
- jquery version of the spamspan code (c) 2006 SpamSpan (www.spamspan.com)
This program is distributed under the terms of the GNU General Public
Licence version 2, available at http://www.gnu.org/licenses/gpl.txt
--------------------------------------------------------------------------
*/
(function ($) { //Standard backdrop jQuery wrapper. See http://drupal.org/update/modules/6/7#javascript_compatibility
// load SpamSpan
Backdrop.behaviors.spamspan = {
attach: function(context, settings) {
// get each span with class spamspan
$("span.spamspan", context).each(function (index) {
// Replace each <spam class="t"></spam> with .
if ($('span.t', this).length) {
$('span.t', this).replaceWith('.');
}
// For each selected span, set mail to the relevant value, removing spaces
var _mail = ($("span.u", this).text() +
"@" +
$("span.d", this).text())
.replace(/\s+/g, '');
// Build the mailto URI
var _mailto = "mailto:" + _mail;
if ($('span.h', this).length) {
// Find the header text, and remove the round brackets from the start and end
var _headerText = $("span.h", this).text().replace(/^ ?\((.*)\) ?$/, "$1");
// split into individual headers, and return as an array of header=value pairs
var _headers = $.map(_headerText.split(/, /), function (n, i) {
return (n.replace(/: /, "="));
});
var _headerstring = _headers.join('&');
_mailto += _headerstring ? ("?" + _headerstring) : '';
}
// Find the anchor content, and remove the round brackets from the start and end
var _anchorContent = $("span.a", this).html();
if (_anchorContent) {
_anchorContent = _anchorContent.replace(/^ ?\((.*)\) ?$/, "$1");
}
// create the <a> element, and replace the original span contents
//check for extra <a> attributes
var _attributes = $("span.e", this).html();
var _tag = "<a></a>";
if (_attributes) {
_tag = "<a " + _attributes.replace("<!--", "").replace("-->", "") + "></a>";
}
$(this).after(
$(_tag)
.attr("href", _mailto)
.html(_anchorContent ? _anchorContent : _mail)
.addClass("spamspan")
).remove();
});
}
};
}) (jQuery);