/**
* @file Builds email and phone links at runtime to hide them from harvesters.
*
* Neither the email address nor the phone number appears whole anywhere in
* the HTML or this JavaScript. Spam bots that scan page source for `@`,
* `mailto:`, `tel:` or a ten-digit number therefore find nothing to collect.
*
* ## Authored markup
*
* ```html
* <a href="#intake" data-email-user="info"
* data-email-domain="coastalhealthcareadvocates.org">Email us</a>
*
* <a href="#intake" data-tel-area="757" data-tel-line="5740771">Call us</a>
* ```
*
* The `href` and link text are the **no-JavaScript fallback**: they send the
* visitor to the contact form instead.
*
* ## What this script turns them into
*
* - **Email link:** `href` becomes `mailto:info@…`. The link's text — or, if
* present, the text of a `[data-email-text]` child (so an icon beside the
* text survives) — becomes the address. A `<wbr>` (optional line break) is
* placed before the `@` so a long address can wrap on narrow screens.
* - **Phone link:** `href` becomes `tel:+17575740771`. The link's text — or,
* if present, a `[data-tel-text]` child's (so a leading "Call " survives) —
* becomes `(757) 574-0771`.
*
* The `data-*` attributes are removed once a link is built, so running the
* builder again over the same markup is harmless.
*
* ## Loading
*
* A classic `<script defer>` in every page's `<head>`. Deferred scripts run in
* order once parsing finishes, so the whole page, the shared header and
* footer included, is in the document by the time this runs. Markup added later (for example
* by `contact-form.js`) is handled by calling `window.chaEmailLinks(root)`.
*
* Written in ES5 inside an IIFE (immediately-invoked function expression) so
* none of its variables leak into the global scope.
*
* @module email
*/
(function () { // start a private scope that runs immediately
'use strict'; // opt into strict-mode JavaScript
/**
* Converts every obfuscated email and phone link inside `root` into a real,
* clickable `mailto:` / `tel:` link.
*
* Exposed globally as `window.chaEmailLinks`.
*
* @param {ParentNode} [root=document] Element (or document) to search within.
* @returns {void}
*/
function build(root) { // define build(root)
// --- Email links -------------------------------------------------------
(root || document).querySelectorAll('a[data-email-user]').forEach(function (a) { // each unbuilt email link
var user = a.getAttribute('data-email-user'); // the part before the @
var domain = a.getAttribute('data-email-domain'); // the part after the @
// Incomplete markup: leave the fallback link untouched.
if (!user || !domain) return;
// Build "@" from its character code so the source never contains a
// literal user@domain pattern.
var at = String.fromCharCode(64);
a.href = 'mailto:' + user + at + domain; // point the link at the full address
// Write the visible address into the [data-email-text] child if there is
// one (keeps sibling icons), otherwise into the link itself.
var label = a.querySelector('[data-email-text]') || a;
label.textContent = user; // replace the text with the user part
// <wbr> lets the browser break the line before "@" when space is tight.
label.appendChild(document.createElement('wbr'));
label.appendChild(document.createTextNode(at + domain)); // add "@domain" after it
// Mark as done so a second pass skips this link.
a.removeAttribute('data-email-user'); // drop the user attribute
a.removeAttribute('data-email-domain'); // drop the domain attribute
});
// --- Phone links -------------------------------------------------------
(root || document).querySelectorAll('a[data-tel-area]').forEach(function (a) { // each unbuilt phone link
var area = a.getAttribute('data-tel-area'); // three-digit area code
var line = a.getAttribute('data-tel-line'); // seven-digit local number
// Incomplete markup: leave the fallback link untouched.
if (!area || !line) return;
// E.164-style dial string with the US country code, e.g. tel:+17575740771.
a.href = 'tel:+1' + area + line;
// Display as "(757) 574-0771" in the [data-tel-text] child if present
// (so text such as "Call " before it survives), otherwise in the link.
var label = a.querySelector('[data-tel-text]') || a;
label.textContent = '(' + area + ') ' + line.slice(0, 3) + '-' + line.slice(3); // write the formatted number
// Mark as done so a second pass skips this link.
a.removeAttribute('data-tel-area'); // drop the area-code attribute
a.removeAttribute('data-tel-line'); // drop the local-number attribute
});
}
// Let other scripts build links in markup they inject later.
window.chaEmailLinks = build;
// Build every link already in the page.
build(document);
})(); // end of the private scope; run it now