[go: nahoru, domu]

Skip to content

Commit

Permalink
core(crawlable-anchors): consider empty rawHref crawlable (#15406)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianaixba committed Aug 24, 2023
1 parent 614170f commit f33cf93
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
3 changes: 2 additions & 1 deletion core/audits/seo/crawlable-anchors.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class CrawlableAnchors extends Audit {
name = '',
role = '',
id,
href,
}) => {
rawHref = rawHref.replace( /\s/g, '');
name = name.trim();
Expand All @@ -61,7 +62,7 @@ class CrawlableAnchors extends Audit {
if (rawHref.startsWith('file:')) return true;
if (name.length > 0) return;

if (rawHref === '') return true;
if (href === '') return true;
if (javaScriptVoidRegExp.test(rawHref)) return true;

// checking if rawHref is a valid
Expand Down
12 changes: 8 additions & 4 deletions core/test/audits/seo/crawlable-anchors-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import CrawlableAnchorsAudit from '../../../audits/seo/crawlable-anchors.js';

function runAudit({
rawHref = '',
href = rawHref,
role = '',
onclick = '',
name = '',
Expand All @@ -26,6 +27,7 @@ function runAudit({
const {score} = CrawlableAnchorsAudit.audit({
AnchorElements: [{
rawHref,
href,
name,
listeners,
onclick,
Expand All @@ -43,7 +45,7 @@ function runAudit({

describe('SEO: Crawlable anchors audit', () => {
it('allows crawlable anchors', () => {
assert.equal(runAudit({rawHref: '#top'}), 1, 'hash fragment identifier');
assert.equal(runAudit({rawHref: '#top', href: 'https://example.com#top'}), 1, 'hash fragment identifier');
assert.equal(runAudit({rawHref: 'mailto:name@example.com'}), 1, 'email link with a mailto URI');
assert.equal(runAudit({rawHref: 'https://example.com'}), 1, 'absolute HTTPs URL');
assert.equal(runAudit({rawHref: 'foo'}), 1, 'relative URL');
Expand All @@ -59,6 +61,7 @@ describe('SEO: Crawlable anchors audit', () => {
}), 1, 'relative link which specifies a query string');

assert.equal(runAudit({rawHref: 'ftp://'}), 0, 'invalid FTP links fails');
assert.equal(runAudit({rawHref: '', href: 'https://example.com'}), 1, 'empty attribute that links to current page');
});

it('allows anchors acting as an ID anchor', () => {
Expand All @@ -82,6 +85,7 @@ describe('SEO: Crawlable anchors audit', () => {
it('handles anchor elements which use event listeners', () => {
const auditResultMixtureOfListeners = runAudit({
rawHref: '/validPath',
href: 'https://example.com/validPath',
listeners: [{type: 'nope'}, {type: 'another'}, {type: 'click'}],
});
assert.equal(auditResultMixtureOfListeners, 1, 'valid href with any event listener is a pass');
Expand All @@ -102,9 +106,9 @@ describe('SEO: Crawlable anchors audit', () => {
assert.equal(runAudit({}), 0, 'link with no meaningful attributes and no event handlers');
assert.equal(runAudit({rawHref: 'file:///image.png'}), 0, 'hyperlink with a `file:` URI');
assert.equal(runAudit({name: ' '}), 0, 'name attribute with only space characters');
assert.equal(runAudit({rawHref: ' '}), 0, 'href attribute with only space characters');
assert.equal(runAudit({rawHref: ' '}), 1, 'href attribute with only space characters');
const assertionMessage = 'onclick attribute with only space characters';
assert.equal(runAudit({rawHref: ' ', onclick: ' '}), 0, assertionMessage);
assert.equal(runAudit({rawHref: ' ', onclick: ' '}), 1, assertionMessage);
});

it('handles javascript:void expressions in the onclick attribute', () => {
Expand Down Expand Up @@ -141,7 +145,7 @@ describe('SEO: Crawlable anchors audit', () => {
];

for (const onclickVariation of expectedAuditPasses) {
const auditResult = runAudit({rawHref: '/validPath', onclick: onclickVariation});
const auditResult = runAudit({rawHref: '/validPath', href: 'https://example.com/validPath', onclick: onclickVariation});
assert.equal(auditResult, 1, `'${onclickVariation}' should pass the audit`);
}
});
Expand Down

0 comments on commit f33cf93

Please sign in to comment.