refactor: simplified ajaxify.check and likely made it equally more confusing

This commit is contained in:
Julian Lam
2024-09-11 14:43:51 -04:00
parent c2600b124f
commit cf6c4c52b8

View File

@@ -32,10 +32,12 @@ ajaxify.widgets = { render: render };
* null (no action) * null (no action)
*/ */
let urlObj; let urlObj;
let pathname; let pathname = item instanceof Element ? item.getAttribute('href') : undefined;
try { try {
urlObj = new URL(item, `${document.location.origin}${config.relative_path}`); urlObj = new URL(item, `${document.location.origin}${config.relative_path}`);
({ pathname } = urlObj); if (!pathname) {
({ pathname } = urlObj);
}
} catch (e) { } catch (e) {
return false; return false;
} }
@@ -44,34 +46,38 @@ ajaxify.widgets = { render: render };
// eslint-disable-next-line no-script-url // eslint-disable-next-line no-script-url
const hrefEmpty = href => href === undefined || href === '' || href === 'javascript:;'; const hrefEmpty = href => href === undefined || href === '' || href === 'javascript:;';
if (item instanceof Element && item.getAttribute('data-ajaxify') === 'false') { if (item instanceof Element) {
if (!internalLink) { if (item.getAttribute('data-ajaxify') === 'false') {
return; if (!internalLink) {
return false;
}
return null;
} }
return null; // eslint-disable-next-line no-script-url
if (hrefEmpty(urlObj.href) || urlObj.protocol === 'javascript:' || pathname === '#' || pathname === '') {
return null;
}
} }
// Default behaviour for rss feeds if (internalLink) {
if (internalLink && pathname.endsWith('.rss')) { // Default behaviour for rss feeds
return false; if (pathname.endsWith('.rss')) {
} return false;
}
// Default behaviour for sitemap // Default behaviour for sitemap
if (internalLink && String(pathname).startsWith(config.relative_path + '/sitemap') && pathname.endsWith('.xml')) { if (String(pathname).startsWith(config.relative_path + '/sitemap') && pathname.endsWith('.xml')) {
return false; return false;
} }
// Default behaviour for uploads and direct links to API urls // Default behaviour for uploads and direct links to API urls
if (internalLink && ['/uploads', '/assets/', '/api/'].some(function (prefix) { if (['/uploads', '/assets/', '/api/'].some(function (prefix) {
return String(pathname).startsWith(config.relative_path + prefix); return String(pathname).startsWith(config.relative_path + prefix);
})) { })) {
return false; return false;
} }
// eslint-disable-next-line no-script-url
if (hrefEmpty(urlObj.href) || urlObj.protocol === 'javascript:' || pathname === '#' || pathname === '') {
return null;
} }
return true; return true;