[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(crawlable-anchors): allow elements acting as anchors #15079

Merged
merged 2 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/audits/seo/crawlable-anchors.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class CrawlableAnchors extends Audit {
rawHref,
name = '',
role = '',
id,
}) => {
rawHref = rawHref.replace( /\s/g, '');
name = name.trim();
Expand All @@ -52,6 +53,9 @@ class CrawlableAnchors extends Audit {
// Ignore mailto links even if they use one of the failing patterns. See https://github.com/GoogleChrome/lighthouse/issues/11443#issuecomment-694898412
if (rawHref.startsWith('mailto:')) return;

// Ignore `<a id="something">` elements acting as an anchor.
if (rawHref === '' && id) return;

const javaScriptVoidRegExp = /javascript:void(\(|)0(\)|)/;

if (rawHref.startsWith('file:')) return true;
Expand Down
2 changes: 2 additions & 0 deletions core/gather/gatherers/anchor-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function collectAnchorElements() {
text: node.innerText, // we don't want to return hidden text, so use innerText
rel: node.rel,
target: node.target,
id: node.getAttribute('id') || '',
// @ts-expect-error - getNodeDetails put into scope via stringification
node: getNodeDetails(node),
};
Expand All @@ -66,6 +67,7 @@ function collectAnchorElements() {
text: node.textContent || '',
rel: '',
target: node.target.baseVal || '',
id: node.getAttribute('id') || '',
// @ts-expect-error - getNodeDetails put into scope via stringification
node: getNodeDetails(node),
};
Expand Down
6 changes: 6 additions & 0 deletions core/test/audits/seo/crawlable-anchors-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function runAudit({
role = '',
>
name = '',
id = '',
listeners = onclick.trim().length ? [{type: 'click'}] : [],
node = {
snippet: '',
Expand All @@ -30,6 +31,7 @@ function runAudit({
onclick,
role,
node,
id,
}],
URL: {
finalDisplayedUrl: 'http://example.com',
Expand Down Expand Up @@ -59,6 +61,10 @@ describe('SEO: Crawlable anchors audit', () => {
assert.equal(runAudit({rawHref: 'ftp://'}), 0, 'invalid FTP links fails');
});

it('allows anchors acting as an ID anchor', () => {
assert.equal(runAudit({rawHref: '', id: 'example'}), 1, 'anchor link as ID anchor');
});

it('allows anchors which use a name attribute', () => {
assert.equal(runAudit({name: 'name'}), 1, 'link with a name attribute');
});
Expand Down
1 change: 1 addition & 0 deletions types/artifacts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ declare module Artifacts {
target: string
node: NodeDetails
onclick: string
id: string
listeners?: Array<{
type: Crdp.DOMDebugger.EventListener['type']
}>
Expand Down
Loading