diff --git a/site/scripts/growth-html-checks.mjs b/site/scripts/growth-html-checks.mjs index 78957926..e64977d4 100644 --- a/site/scripts/growth-html-checks.mjs +++ b/site/scripts/growth-html-checks.mjs @@ -1,3 +1,14 @@ +/** Read only exact HTML attribute names, not data-name / data-href. */ +function attributeValue(tag, wanted) { + const pattern = /(?:^|\s)([^\s=/>]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>]+)))?/g; + for (const match of tag.matchAll(pattern)) { + if (match[1].toLowerCase() === wanted) { + return match[2] ?? match[3] ?? match[4] ?? ''; + } + } + return null; +} + function renderedMarkup(html) { // Ignore comments and serialized hydration/script payloads that can contain fake tags. return html @@ -7,22 +18,25 @@ function renderedMarkup(html) { } export function hasNoindex(html, xRobotsTag = "") { // X-Robots-Tag may be repeated/comma-delimited, and can target a specific crawler. - if (/(?:^|[,\s:])noindex(?:$|[,\s])/i.test(xRobotsTag)) return true; + if (/(?:^|[,\s:])(?:noindex|none)(?:$|[,\s])/i.test(xRobotsTag)) return true; return [...renderedMarkup(html).matchAll(/]*>/gi)].some(([tag]) => { - const name = tag.match(/\bname\s*=\s*["'](robots|googlebot)["']/i); - const value = tag.match(/\bcontent\s*=\s*["']([^"']+)["']/i)?.[1] ?? ''; - return Boolean(name) && /(?:^|[,\s])noindex(?:$|[,\s])/i.test(value); + const name = attributeValue(tag, 'name')?.toLowerCase(); + const value = attributeValue(tag, 'content') ?? ''; + return (name === 'robots' || name === 'googlebot') && + /(?:^|[,\s])(?:noindex|none)(?:$|[,\s])/i.test(value); }); } export function canonicalHref(html) { - const tag = [...renderedMarkup(html).matchAll(/]*>/gi)].map(([v]) => v).find(t => /\brel\s*=\s*["']canonical["']/i.test(t)); - return tag?.match(/\bhref\s*=\s*["']([^"']+)["']/i)?.[1]?.replace(/&/g, '&') ?? null; + const tag = [...renderedMarkup(html).matchAll(/]*>/gi)] + .map(([v]) => v) + .find(t => (attributeValue(t, 'rel') ?? '').toLowerCase().split(/\s+/).includes('canonical')); + return attributeValue(tag ?? '', 'href')?.replace(/&/g, '&') ?? null; } export function hasSoftwareAnchor(html, slug, locale, origin = 'https://toolify365.com') { const markup = renderedMarkup(html); const destination = `/software/${slug}`; return [...markup.matchAll(/]*>/gi)].some(([tag]) => { - const href = tag.match(/\bhref\s*=\s*["']([^"']+)["']/i)?.[1]?.replace(/&/g, '&'); + const href = attributeValue(tag, 'href')?.replace(/&/g, '&'); if (!href) return false; try { const url = new URL(href, origin); @@ -49,7 +63,7 @@ export function hasAttributedSocialShare(html, source, path, origin = 'https://t let expected; try { expected = new URL(path, origin); } catch { return false; } return [...renderedMarkup(html).matchAll(/]*>/gi)].some(([tag]) => { - const href = tag.match(/\bhref\s*=\s*["']([^"']+)["']/i)?.[1]?.replace(/&/g, '&'); + const href = attributeValue(tag, 'href')?.replace(/&/g, '&'); if (!href) return false; try { const intent = new URL(href); diff --git a/site/scripts/growth-html-checks.test.mjs b/site/scripts/growth-html-checks.test.mjs index feb549c0..dfc88766 100644 --- a/site/scripts/growth-html-checks.test.mjs +++ b/site/scripts/growth-html-checks.test.mjs @@ -90,3 +90,32 @@ test('global publisher iframe widgets fail if HTTP 200 is accompanied by restric assert.equal(permitsPublicEmbedding('', ''), false); assert.equal(permitsPublicEmbedding('', 'frame-src *'), false); }); + + +test('exact HTML attributes prevent fake robots and canonical claims', () => { + assert.equal(hasNoindex(''), false); + assert.equal(hasNoindex(''), false); + assert.equal(hasNoindex(''), true); + assert.equal(hasNoindex(''), true); + assert.equal(hasNoindex('', 'googlebot: none'), true); + assert.equal(hasNoindex('', 'nonesuch'), false); + assert.equal(canonicalHref(''), null); + assert.equal(canonicalHref(''), null); + assert.equal(canonicalHref(''), null); + assert.equal(canonicalHref(''), '/real'); + assert.equal(canonicalHref(''), '/real'); +}); + +test('only real clickable anchors satisfy internal CTA and shared URL audits', () => { + assert.equal(hasSoftwareAnchor('Unlinked', 'json-studio', 'en'), false); + assert.equal(hasSoftwareAnchor('Linked', 'json-studio', 'en'), true); + const target = new URL('https://toolify365.com/en/tools/json-formatter'); + target.searchParams.set('utm_source', 'linkedin'); + target.searchParams.set('utm_medium', 'social'); + target.searchParams.set('utm_campaign', 'tool_share'); + const intent = new URL('https://www.linkedin.com/sharing/share-offsite/'); + intent.searchParams.set('url', target.toString()); + const encoded = intent.toString().replace(/&/g, '&'); + assert.equal(hasAttributedSocialShare(`Not a link`, 'linkedin', '/en/tools/json-formatter'), false); + assert.equal(hasAttributedSocialShare(`Share`, 'linkedin', '/en/tools/json-formatter'), true); +});